Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

What does _ mean?

by bichonfrise74 (Vicar)
on Feb 02, 2009 at 18:48 UTC ( [id://740779]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I want to ask what this means?
if ( -d(_) ) { print $0; }
It checks the directory for something? I do not understand or even know what _ means?

Replies are listed 'Best First'.
Re: What does _ mean?
by ikegami (Patriarch) on Feb 02, 2009 at 18:50 UTC

    Read the docs for -d

    If any of the file tests (or either the stat or lstat operators) are given the special filehandle consisting of a solitary underline, then the stat structure of the previous file test (or stat operator) is used, saving a system call.

    A few notes and examples follow.

Re: What does _ mean?
by merlyn (Sage) on Feb 04, 2009 at 15:18 UTC
    It means I've confused yet another generation of budding Perl programmers with a microoptimization that I called for back in the Perl 3 alpha days, and probably can be safely ignored now.

    There are very few pieces of Perl that are uniquely mine. This is one of them. Sorry. :)

      So how much more expensive is -e($file) && -f(_) than -e($file) && -f($file)?


      When you're up to your ass in alligators, it's difficult to remember that your original purpose was to drain the swamp.

        strace -tt -f -e '$file="filename";-e($file) && -f($file)'

        (as well as the alternative) says it's exactly one stat system call more expensive, unsurprisingly. Which on the system I tested it on was something like 0.0001 seconds but that will obviously vary.

        All dogma is stupid.
        Four characters. Six, if you also rip out the parentheses. :-)

        Seriously, it's not just about saving one system call or half a dozen source code chars (altough I am all in favour of both) -- -d $filename && -x _ && -k _ && -u _ is simpler, cleaner, and more expressive than the alternative IMHO.

        BTW, I happen to like even more the perl6ish $filename ~~ :d & :x & :k & :u.

        []s, HTH, Massa (κς,πμ,πλ)
Re: What does _ mean?
by pileofrogs (Priest) on Feb 02, 2009 at 18:58 UTC

    I think it means you might want to smack the writer of said code up the head and give them a copy of the PBP book.


    Heh... As I read it, the whole book was about writing clear code. "Don't use $_" etc... (Yes, my statement in support of clarity was totally unclear... sigh...)

      And what does PBP say? To make unnecessary calls to stat (I hope not)?
        I looked up '_' in PBP's index and it referred me to (underscore). Looked up 'underscore', subtopic 'best practice' : p.473:

        Use underscores to improve the readability of long numbers.

        No reference to the file operations in any of the other subtopics in the index either.

        This signature will be ready by Easter

        It doesn't specifically address this, but I'd apply the general principle of commenting any obscure usage. Of course, one man's obscure is another man's clear.

        I ran into this exact thing in an old program I had to modify and added this comment after figuring out what it meant:

        ######### FINALLY FIGURED OUT WHAT _ IS!!! # QUOTE: # # If any of the file tests (or either the stat() or lstat() operators +) are given # the special filehandle consisting of a solitary underline, then the +stat structure # of the previous file test (or stat operator) is used, saving a syste +m call. # # Isn't that something? All I gotta do now figure out what or where i +s the previous # test??

      the whole book was about writing clear code.

      How is "_" unclear? It has one very specific use.

        How about obscure?

      What would be the "best practice" in this case? How would this be re-written?
        pileofrogs is probably confusing the "_" file test operation argument with implicit usage of the "$_" variable, which is discouraged by some. "_" used appropriately as a file test argument is fine.

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-20 13:17 GMT
Find Nodes?
    Voting Booth?

    No recent polls found