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

Hello monks!!

do you believe in ghosts? i have the following code:
@ident=`whois ibm.com|grep ID`; my ($id) = grep(/ID/,@ident);

i have 2 identical Linux boxes.

if i execute this lines in box number 1 $id contains, as expected, the line containing the ID...

BUT if i execute this lines in box number 2 $id contains number 1, that corresponds to a grep in scalar context!!

in both servers the result of: `whois ibm.com|grep ID` is identical!

Why grep behaves as scalar context when it should behave as list conext? Thanks in advance! Pedrete

Replies are listed 'Best First'.
Re: grep in list context behaves as scalar context
by hippo (Archbishop) on Jun 17, 2015 at 22:11 UTC
    i have 2 identical Linux boxes.

    Maybe you do or maybe they are not actually identical after all. Anyway, to avoid contamination by anything else going on in your script try running this one-liner on both boxes.

    perl -e '($z) = grep (/b/, "foo", "bar"); print "z is $z\n";'

    If box 2 says "z is 1" instead of "z is bar" then start comparing things between the two boxes: perl versions, environment vars, shell versions, etc.

      Thanks a lot hippo!! your one-liner showed same results on both boxes... so something else is contaminating my script, blood and soul... Thanks again! Pedrete.