in reply to Perl Bug?

Please take the time and reduce your code to the bare minimum. The problem was completely unrelated to ps -ef, it was completely unrelated to the actual platform, it was completely unrelated to the weird inclusion of other scripts you did.

The problem can be reduced to the following code:

use strict; my $PLATFORM = 'solaris'; foreach($PLATFORM) { (/solaris/ || /tru64/ || /linux/ || /aix/) and do { my $i=1; print "\$PLATFORM is '$PLATFORM'\n"; open (PSTEST, $0); while (<PSTEST>){ print "Line # ".$i."\n".$_; print "Platform now is '$PLATFORM'\n"; $i++; }; print "All done!\n"; close PSTEST; print "Platform now is '$PLATFORM'\n"; }; };

Please note the somewhat more instructive debugging output. Your problem stems from the spurious foreach ($PLATFORM) loop, which sets $_ as an alias to $PLATFORM and then clobbers it in the while loop. You can circumvent the problem by removing the spurious foreach loop which will execute only once anyway.

use strict; my $PLATFORM = 'solaris'; #foreach($PLATFORM) { $_ = $PLATFORM; (/solaris/ || /tru64/ || /linux/ || /aix/) and do { my $i=1; print "\$PLATFORM is '$PLATFORM'\n"; open (PSTEST, $0); while (<PSTEST>){ print "Line # ".$i."\n".$_; print "Platform now is '$PLATFORM'\n"; $i++; }; print "All done!\n"; close PSTEST; print "Platform now is '$PLATFORM'\n"; }; #};

You should really really really rewrite the code as it is an optical mess and many of the code and employed techniques are quite bizarre.

Replies are listed 'Best First'.
Re^2: Perl Bug?
by Knom (Beadle) on Nov 22, 2005 at 11:29 UTC
    Corion: Kudos for solving that one! However, the code is straight out of the Perl Cookbook for implementing a SWITCH construct. Any suggestions for replacing foreach?

      Please reread my reply. I already posted how you can remove the foreach which is unnecessary. As an alternative, you could use a different variable than $_ in your inner while-loop:

      while (defined (my $line = <PSTEST>)){ print "Line # ".$i."\n".$line; print "Platform now is '$PLATFORM'\n"; $i++; };
        Neat trick. I'll take it. Thanks!