Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Help with file test operators

by justame (Initiate)
on Mar 26, 2015 at 19:41 UTC ( [id://1121438]=perlquestion: print w/replies, xml ) Need Help??

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

I using perl v5.8.4 on a HPUX platform, i have a script that needs to be able to determine if a file is for today or not.
My thought was to use the file test operators in perl, so i did a small test script posting the file and various test operators.
My problem is that every file has the same test results.

foreach my $logfile ( <./report*> ) { chomp($logfile); chomp(my @ll = `ll $logfile`); print "\n\n--------------------------------\n@ll\n"; printf ("%22s %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f\n", $logfile, -M + _, -A _, -C _, -r _, -w _, -O _); }

file test returns the same for all files.

---------------------------------------------------------- -rw-rw-r-- 1 not_me mygroup 50555 Mar 25 16:44 ./report.txt ./report.txt 0.99 0.00 0.99 1.00 1.00 1.00 ---------------------------------------------------------- -rw-rw-r-- 1 me mygroup 38322 Oct 22 19:34 ./report.txt. +bak ./report.txt.bak 0.99 0.00 0.99 1.00 1.00 1.00 ---------------------------------------------------------- -rw-rw-r-- 1 me mygroup 38322 Oct 22 19:34 ./report.txt. +sav ./report.txt.sav 0.99 0.00 0.99 1.00 1.00 1.00 ---------------------------------------------------------- -rw-r--r-- 1 not_me mygroup 38322 Oct 22 19:09 ./report.txts +av ./report.txtsav 0.99 0.00 0.99 1.00 1.00 1.00 ---------------------------------------------------------- -rw------- 1 not_me mygroup 38322 Oct 22 19:09 ./report.txts +av2 ./report.txtsav2 0.99 0.00 0.99 1.00 1.00 1.00 ---------------------------------------------------------- -rw-rw-r-- 1 me mygroup 41397 Jul 31 2014 ./report_7_30 +.txt ./report_7_30.txt 0.99 0.00 0.99 1.00 1.00 1.00

Any Ideas why this is happening?

Replies are listed 'Best First'.
Re: Help with file test operators
by choroba (Cardinal) on Mar 26, 2015 at 19:44 UTC
    You didn't tell Perl what file to run the tests on. Replace the first _ with $logfile.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      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!

        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)
        
        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.

      thank you choroba, that did it

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-03-19 06:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found