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

hi,,,thanks guys for the wisdom about my earlier post. also, i learnt from my mistake... pardon me for that. but anyway... im getting into trouble with the special underscore filehandle.

my delimma was all about the special underscore filehandle "_".

codes from the book "Llama book" as follows:

#!/perl/bin/perl use strict; my @original_files = qw/ fred barney betty wilma pebbles dino bamm-bam +m /; my @big_old_files; # The ones we want to put on back +up tapes foreach (@original_files) { push @big_old_files, $_ if (-s) > 100_000 and -A _ > 90; # More efficient than before }

the program logic seems to be very clear, right? an excerpt explanation from the book goes this way:

"We used the default of $_ for the first test; this is as more efficient (except perhaps for the programmer), and it gets the data from the operating system. The second test uses the magic _ filehandle. For this test, the data left around after getting the file's size are used, which are what we want."

the explanation entry point was from the lines of file test "if (-s) > 100_000 and -A _ > 90;". actually it runs very well, what i don't understand is from that "_" after "-A". it says that the value of the special filehandle was the data left around after getting the file's size are used "(-s) > 100_000".

QUESTION NO. 1: what are these data that was left after getting the file size? is it the filename? or what?

on my wild understanding of this (but im not that sure) is when we use the file test "-s", maybe the function under "-s" uses the stat function as a sub function. because we all knew that the stat function returns the number of links, file size, accessed time, modified time, time created, etc. of a certain file. so when "-A _" was executed the accessed time "-A" gets the data from the special filehandle "_" in which the special filehandle has those data assigned from "-s" except for the file size because that data was pulled when "-s" was executed.

QUESTION NO. 2: was my wild undersderstanding correct? if not, then what would be the right thing?

QUESTION NO. 3: i never tried using the special filehandle righteously on my own yet, does the special filehandle be used only on one line or could it be used down the other codes of my program?

because when i tried to print on the screen the values that accompanied the special filehandle, i get nothing... the codes were as follows:

#!/perl/bin/perl use strict; my @original_files = qw/ fred /; my @big_old_files; foreach (@original_files) { if ((-s) > 0 and -A _ > 0) { push @big_old_files, $_; print _; } }

maybe i was wrong of printing with it because in the first place it is only a FILEHANDLE... but then where are the data left be found? maybe that special filehandle is only an identifier for a struct (like in C)... oh! i just don't get it exactly. TO BROAD TO GUESS, TO NARROW TO FIND THE CLUE...

that's all folks... i hope you could get me out into trouble. thanks again in advance... i hope my format fits well... keep deep and dark!

From: PerlPhi

Replies are listed 'Best First'.
Re: About Special Underscore Filehandle
by merlyn (Sage) on May 04, 2007 at 17:51 UTC
    As the inventor of the underscore handle, let me explain. It seemed silly that if you wanted to see if a file was both readable and writable, that you'd ask the operating system twice for the same exact information (the result of calling stat on the filename). So, I suggested to Larry that there be some way to "cache" the info. He created the underscore handle to represent "whatever we stat'ed last, we'll just remember".

    So, what it remembers is "the most recent stat", which includes almost everything you want to know about the file: owner, permissions, size, timestamps, etc.

    Does that help?

      oww! tnx...it helps a lot. but another thing that i just want to clarify. was the special underscore filehandle only used on a file test? or does it have other usage?

      From: PerlPhi

Re: About Special Underscore Filehandle
by liverpole (Monsignor) on May 04, 2007 at 17:50 UTC
    Hi PerlPhi,

    In question 1, the thing that's left around is the results of doing a stat of the file.  An implicit stat is done when you use "-s" on a file, but a stat gives you a number of other pieces of information, which you already have in-memory.

    For question 2, yes, your understanding is correct.

    For question 3, you are misusing "_" as you speculated.  You can't print it out, you can only use it in conjunction with one of the -x tests.  However, you could do the following to print the file when it matches the tests -s and -A:

    #!/perl/bin/perl use strict; my @original_files = qw/ fred /; my @big_old_files; foreach (@original_files) { if ((-s) > 0 and -A _ > 0) { push @big_old_files, $_; print "$_\n"; # Display the file name if it passes -s and -A +tests. } }

    Note, too, that when you say "(-s)", you're implicitly saying "(-s $_)".


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: About Special Underscore Filehandle
by Fletch (Bishop) on May 04, 2007 at 18:10 UTC

    Under the covers Perl is calling the stat(2) system call (or your platform's analogue to it). This system call returns a buffer of information containing several items relating to the file it was called on (such as the size, owner, modification and access times, etc.). When you use the special underscore filehandle, you're telling Perl "hey, don't make another call to the OS I just want another value from what you got last time".