Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

_ doing something new to me?

by cosmicperl (Chaplain)
on Feb 28, 2009 at 01:28 UTC ( [id://747060]=perlquestion: print w/replies, xml ) Need Help??

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

Hi All,
  I've been reading through some Perl code. I've come across a bit that uses _ in a way I'm not familiar with and I can't find docs on it:-
my $filename = shift; return 'link' if (-l $filename); return 'file' if (-f _); return 'dir' if (-d _);
I can see what is happening. But I don't know why? I don't understand why the _ doesn't have a sigil?

Would someone be kind enough to fill me in?


Lyle

Replies are listed 'Best First'.
Re: _ doing something new to me?
by Lawliet (Curate) on Feb 28, 2009 at 01:32 UTC

    It is basically the same thing as the last "thing" tested with the previous test operator but without having to re-test it (which is apparently expensive). I remember reading that in a book.

    Update: I think it is called 'chaining' tests.

    Update2:

    To save the overhead of a low-level stat() system call, Perl caches the previously-examined inode in a special variable called simply '_'; so we can do tests like this:
    if ( -e $file && -r _ && -x _) { # $file exists and is both readable and executable ... }

    And you didn't even know bears could type.

Re: _ doing something new to me?
by hbm (Hermit) on Feb 28, 2009 at 01:52 UTC
Re: _ doing something new to me?
by missingthepoint (Friar) on Feb 28, 2009 at 02:27 UTC

    Answering the question:

    Normal filehandle names in Perl match /[a-z_][a-z0-9_]*/, so it's a legal name, akin to a special variable like $", but a filehandle instead of a scalar. Consider print STDERR "foo\n"... no sigil.

    Not answering the question but informative:

    It's known as the 'magical underscore filehandle', and it exists for efficiency reasons.

    Generally, OS's store a file's metadata all bunched together, in what C programmers would call a 'structure'. Whenever a piece of Perl code requests a part of a file's metadata (e.g. checking its permissions, or checking its size), a system call is made (under *nix, usually called stat() or a derivative like stat64()) that returns the file's complete metadata structure. In the example you posted this is obviously wasteful, so whenever a file test operator is used Perl keeps the returned metadata structure around in memory, in case further tests need to be performed against the same file.

    You can learn a lot by using a system call tracer against your Perl code, like strace under Linux. For instance:

    # file1.pl my $size = -s $ARGV[0]; my $time = -M $ARGV[0];
    $ strace perl file1.pl
    # file2.pl my $size = -s $ARGV[0]; my $time = -M _;
    $ strace perl file2.pl

    ... and compare the number of calls to stat() or fstat64() or whatever.


    That that is is that that is not is not
Re: _ doing something new to me?
by cosmicperl (Chaplain) on Feb 28, 2009 at 02:05 UTC
    Thank you both :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-05-19 05:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found