Knom has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: Perl Bug?
by Aristotle (Chancellor) on Nov 18, 2005 at 10:22 UTC

    From MJD’s file of good advice:

    #11907 Looking for a compiler bug is the strategy of LAST resort. LAST resort.

    #11943 Ah yes, and you are the first person to have noticed this bug since 1987. Sure.

    As a first, second and third rule of thumb, always assume that the problem is in your code. All my mysterious bugs have always been my own bugs. As for what the problem is – you show so little code that noone will be able to do anything but guess. Post more code.

    Makeshifts last the longest.

      I've updated the code. Please re-review at your earliest conveinence. I'm afraid I was a tad hasty with my first post, as I was short on time. Sorry!
Re: Perl Bug?
by blazar (Canon) on Nov 18, 2005 at 12:36 UTC
    What are your "globally scoped varbinds"? The portion of code you show seems to be ok, although I'd rewrite it as
    open my $ps, '-|', 'ps -eo pid,comm,args' or die "Can't run 'ps': $!\n";
    What about some more? I'm sure I would have no problems reading the output of it like thus:
    my $output = do { open my $ps, '-|', 'ps -eo pid,comm,args' or die "Can't run 'ps': $!\n"; local $/; <$ps>; };
    Also, you may have chosen ps(1) solely as an example, but you may also be interested in Proc::ProcessTable.
Re: Perl Bug?
by Corion (Patriarch) on Nov 22, 2005 at 10:45 UTC

    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.

      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++; };
Re: Perl Bug?
by marto (Cardinal) on Nov 18, 2005 at 10:19 UTC
    Please ignore, this is utter nonsense.
    Thanks to Aristotle for quickly pointing this out, please read my sensible reply.

    Knom,

    Perhaps you want to use system or exec rather than open.

    Hope this helps.


    Martin

      How? Those will not let him capture output, will they?

      Makeshifts last the longest.

        Gadzooks!

        Sorry Aristotle, not enough tea this morning. I seem to have somehow stupidly misinterpreted the whole question.

        Knom, as Aristotle has so wisely said, you need to post your code, here is a quick example:
        #!/usr/bin/perl open (PSTEST, "/bin/ps -eaf|"); $i=1; while (<PSTEST>){ print "Line # ".$i." ".$_; $i++; } close PSTEST;

        Martin