Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: Help with file test operators

by LanX (Saint)
on Mar 26, 2015 at 19:51 UTC ( [id://1121443]=note: print w/replies, xml ) Need Help??


in reply to Re: Help with file test operators
in thread Help with file test operators

for those puzzled ... from CB :)

choroba: answered.
LanX: what does -M _ mean?
Corion: "_" is the last-stat'ted filehandle
Corion: perlvar should have it, and/or stat

update

the special filehandle _ doesn't seem to be documented in perlvar , don't know why.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)

PS: Je suis Charlie!

Replies are listed 'Best First'.
Re^3: Help with file test operators
by jeffa (Bishop) on Mar 26, 2015 at 20:48 UTC

    Try

    perldoc -f -X

        If any of the file tests (or either the "stat" or "lstat" 
        operator) is 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. (This doesn't work
        with "-t", and you need to remember that lstat() and "-l" leave
        values in the stat structure for the symbolic link, not the real
        file.) (Also, if the stat buffer was filled by an "lstat" call,
        "-T" and "-B" will reset it with the results of "stat _").
        Example:
    
            print "Can do.\n" if -r $a || -w _ || -x _;
    
            stat($filename);
            print "Readable\n" if -r _;
            print "Writable\n" if -w _;
            print "Executable\n" if -x _;
            print "Setuid\n" if -u _;
            print "Setgid\n" if -g _;
            print "Sticky\n" if -k _;
            print "Text\n" if -T _;
            print "Binary\n" if -B _;
    

    UPDATE:
    "... the point is that it's not obvious ..."

    Just showing you where the information is currently found. If you want to make it more obvious you know exactly how to get that ball rolling on your own. Good luck.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      > perldoc -f -X

      true, but the point is that it's not obvious to look in stat or perlfunc if one notices that _ behaves differently to other barewords.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      PS: Je suis Charlie!

Re^3: Help with file test operators
by Laurent_R (Canon) on Mar 26, 2015 at 22:38 UTC
    the special filehandle _ doesn't seem to be documented in perlvar , don't know why.
    Hmm, this may be nitpicking, but I am not sure it can be called a "special filehandle", it is not really a filehandle. IMHO, it is more like a filename (and file path). But I do not know what to call it either. Perhaps we could call this a special alias to the last file submitted to a file test operator or to stat.

    One thing is sure. I have been knowing about this possibility for several years, and I usually recognize it when I see it, but whenever I would need to use it (well, not so often, but it happens once in a while), I don't remember what it was exactly, and I spend time finding again the right piece of documentation. The documentation could really be improved on this, with the right cross-references to make it easier to find.

    The good thing about this thread is that I believe I'll remember it from now on.

    Je suis Charlie.
      > but I am not sure it can be called a "special filehandle",

      Yeah, but that's how it is called in the docs.

      And you will see that it behaves like an instance of IO::File - like other filehandles do.

      DB<100> _->test Can't locate object method "test" via package "IO::File" at (eval 16)[ +multi_perl5db.pl:644] line 2.

      And this was the point which made me stumble when I tried to declare a package _; some weeks ago.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      PS: Je suis Charlie!

      One of the main reasons that I prefer paper documents is that it so easy to add a note or reference in the margin. You have just identified a new note for my copy of "Perl in a Nutshell". --thanks
      Bill

        One of the main reasons that I prefer paper documents is that it so easy to add a note or reference in the margin. You have just identified a new note for my copy of "Perl in a Nutshell". --thanks

        You know :) notes are great ( ... What does _ mean? , About Special Underscore Filehandle ), but you could also perlbug and update for perlvar to mention _ ... cause its like a note :)

        As I recall, Project Xanadu tried to address this in a way that did not require the original document to have either links or anchors - and without making a local copy of the original.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found