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>;


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/*>;


mcmd($test);


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

perldoc -q quoting


    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;


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

That is usually (better) written as:

     print "Issuing command: $A\n";


    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.


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

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


Reply via email to