Mr. Shawn H. Corey wrote:
On Sun, 2008-10-05 at 16:54 +0200, Manfred Lotz wrote:
my $DRYRUN="--dry-run";
my $cmd = q(rsync $DRYRUN -avh \
                  -exclude bla1  \
                  -exclude bla2  \
                  src tgtdir
};


system($cmd);

my $Dry_Run = '--dry-run';

my $Command_Template = q{rsync %s -avh
                         -exclude bla1  \
                         -exclude bla2  \
                         src tgtdir
};
my $command = sprintf $Command_Template, $Dry_Run;
my $status = system( $command );
die "system( $command ) failed\n" if $status;

Variables in all caps usually constants or special Perl variables.



I like to have it more generic, i.e. I like to have all variables substituted which are defined.

I tried this (Here all variables with pattern /$[\w]+/ will be substituted if they are known):


#! /usr/bin/perl -w


use strict;



my $file_pat = "*.sh";

my $l = q(ls $file_pat |
   while read f; do
      file  $f
   done
);


mcmd($l);



my $dir = "/var";
my $test = q(find $dir -type d -maxdepth 1);

mcmd($test);




sub getvar {
        my $A = shift;
        my $B = eval "$A";

        if ( defined $B  ) {
                return $B;
        } else {
                return $A;
        }
}


sub mcmd {
        my $A = shift;

        $A =~ s/\$[\w]+/getvar "$&"/ge;   
        printf "Issuing command: %s\n",$A;
        system($A);
}





This seems to work. In this example $f will not be touched because it is not defined.


Because I am a real Perl newbie I'd like to know if the code is "ok", i.e. not too un-idiomatic.




--
Thanks,
Manfred


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to