Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

A simple backticks question

by return0 (Acolyte)
on Mar 17, 2014 at 16:00 UTC ( [id://1078618]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks! I was wondering if I can seek your wisdom as to how exactly the backticks work when in a foreach() iterator. is an anonymous array created? for instance, besides making a throw away array @files that I never modify, like:
my @files = `ls -l`
Does Perl make something similar when I just do:
foreach(`ls -l`){ ... }
and make an array that's simply not visible? Or is that too abstract? I was just wondering about it all as I usually do. I hope all is well and thank you in advance! :) ~return0

Replies are listed 'Best First'.
Re: A simple backticks question
by BrowserUk (Patriarch) on Mar 17, 2014 at 16:27 UTC

    The for loop iterates a (potentially huge) list returned by the backticks.

    If you want to avoid creating that list, you can use open thusly:

    open CMD, '-|', 'ls -l' or die $!; while( my $fname = <CMD> ) { chomp $fname; #process the file names one at a time } close CMD;

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: A simple backticks question
by kennethk (Abbot) on Mar 17, 2014 at 16:33 UTC

    The high order concept here is Context. The two major contexts are list and scalar. For example, calling an array in list context returns a list of the values in the array; calling an array in scalar context returns the length of the array.

    The parentheses following foreach are an indicator that you are calling your arguments in list context. From `STRING`:

    In list context, returns a list of lines (however you've defined lines with $/ or $INPUT_RECORD_SEPARATOR), or an empty list if the command failed.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: A simple backticks question
by no_slogan (Deacon) on Mar 17, 2014 at 16:18 UTC
    Technically, the backtick operator returns a list, which is subtly different from an array. (You can't use it as the first argument to push, for example.) Arrays are automatically converted to lists when evaluated in list context.
      Not fully correct, the backticks are context sensitive, in scalar context `ls -l` will return a concat of all lines. (a list array¹ in scalar context returns the number of lines)

      backtick/qx in perlop

      In scalar context, it comes back as a single (potentially multi- line) string, or undef if the command failed. In list context, returns a list of lines (however you’ve defined lines with $/ or $INPUT_RECORD_SEPARATOR), or an empty list if the command failed.

      Cheers Rolf

      ( addicted to the Perl Programming Language)

      updates

      ¹) corrected, thanks to no_slogan. It very much depends on what the command within backticks returns.

        Thanks for the correction, but again, not exactly correct. An array in scalar context evaluates to its number of elements. There is no such thing as a list in scalar context.
Re: A simple backticks question
by return0 (Acolyte) on Mar 17, 2014 at 19:17 UTC
    Thank you all :) You guys are great!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1078618]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-28 17:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found