Hi Harry, On Sun, 02 Nov 2014 17:22:10 -0500 Harry Putnam <rea...@newsguy.com> wrote:
> Intermittent programming can lead to brain bleed off. > > In the real script the command is something only found on solaris or > any other zfs filesystem so I just used the first simple command I > could think of. But the aim is to use the output of a command to > perform a string match. I left that out too, because even when > reduced to its most basic code `tst' still fails in the same way. > > This little example below prints this error .. forever. > > Use of uninitialized value $_ in print at ./tst line 11. > > My first impulse was that I had the `dash' with the pipe operator on > the wrong side... but changing it does the same thing. > > What is wrong with this example: > In addition to what Ken said: > cat tst: > > #!/usr/local/bin/perl > > use strict; > use warnings; > > my $cmd = 'ls /'; > > open my $ch, '-|', "$cmd" or die "Can't open $cmd: $!"; > 1. No need to wrap «$cmd» in double quotes here: http://perl-begin.org/tutorials/bad-elements/#vars_in_quotes 2. You should use the list form: my @cmd = qw(ls /); open my $ch, '-|', @cmd or die "Can't open <<@cmd>>: $!"; (Untested!). > while ($ch) { > print; > } > It is a good idea to avoid using $_ in production code: http://perl-begin.org/tutorials/bad-elements/#overuse_dollar_underscore So write it as: while (my $l = <$ch>) { print $l; } Regards, Shlomi Fish P.S: I should note that perl-begin.org is a site I originated and maintain. -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ Apple Inc. is Evil - http://www.shlomifish.org/open-source/anti/apple/ I’d do Windows-- , but this may result in an integer underflow. — an Israeli Linuxer. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/