Hi there,
I've got a strange problem with $_. It seems I do not understand
something which might be basic. Anyway, here is a minimal example.
#! /usr/bin/perl
use strict;
use warnings;
use 5.010;
sub sub1 {
my $cmd = shift;
print "Sub3: Issuing [$cmd]\n";
open my $fh, '-|', "$cmd 2>&1" or die "open: $!";
while ( <$fh> ) { print; }
close $fh;
return $? >> 8;
}
sub sub2 {
my $cmd = shift;
say "\nSub1 $cmd";
return 1;
}
my @cmds = ( "uname -a", "date");
foreach ( @cmds ) { say sub1($_); };
foreach ( @cmds ) { say sub2($_); };
Running this gives:
Sub3: Issuing [uname -a]
Linux hogwart 3.0.0-14-generic-pae #23~lucid1-Ubuntu SMP Thu Dec 8
15:03:12 UTC 2011 i686 GNU/Linux 0
Sub3: Issuing [date]
Sun Mar 4 08:54:18 CET 2012
0
Use of uninitialized value $cmd in concatenation (.) or string
at ./foreach1.pl line 20.
Sub1
1
Use of uninitialized value $cmd in concatenation (.) or string
at ./foreach1.pl line 20.
Sub1
1
What am I doing wrong here?
--
Manfred
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/