Hi Manfred, On Sun, Mar 4, 2012 at 8:56 AM, Manfred Lotz <manfred.l...@arcor.de> wrote:
> 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"); > > Please use a lexical variable for your foreach(s) loop: > foreach ( @cmds ) { say sub1($_); }; > foreach ( @cmds ) { say sub2($_); }; > like this: foreach my $value1 ( @cmds ) { say sub1($value1); }; foreach my $value2 ( @cmds ) { say sub2($value2); }; > > 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? > > Nothing really, but after your first foreach loop, there is **nothing** left in your array @cmds for the second foreach loop to iterate over. > -- > Manfred > > > > > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > > -- Tim