John W. Krahn wrote:
Manfred Lotz wrote:
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
);

Why shell out to run 'ls' when you can get the file names directly in perl?

my @files = <*.sh>;


'ls' was just an example. I could have taken an rsync or other command.




mcmd($l);


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

Why shell out to run 'find' when you can get the directory names directly in perl?

my @var_dirs = grep -d, </var/*>;



Same as above. Just an example.



mcmd($test);


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

perldoc -q quoting


You are saying I can omit the double quotes? Seems to work.



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

sub mcmd {
    my $A = shift;

$A =~ s/\$[\w]+/getvar "$&"/ge;

That is usually (better) written as:

     $A =~ s/(\$\w+)/ getvar $1 /ge;



Ok, got it.




    printf "Issuing command: %s\n",$A;

That is usually (better) written as:

     print "Issuing command: $A\n";


Aaah, ok.



Thanks for your help.


--
Manfred


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


Reply via email to