Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Files and -f

by Purdy (Hermit)
on Dec 01, 2001 at 01:06 UTC ( #128743=note: print w/replies, xml ) Need Help??

in reply to Files and -f

Methinks your logic is a little off... if I understand you correctly, you want to test to see if the file exists (with either a '1' or not in the filename. And if so, open it and do whatever. Otherwise, move on (or give an alert or something). The '-f' expression tests to see if the file is a regular file (on top of it existing), but that's not really what's hanging you up, I bet.

my $txtfile = $prefix . ".txt"; my $txtfile1 = $prefix . "1.txt"; if ( -f $txtfile ) { # do stuff } elsif ( -f $txtfile1 ) { # do stuff } else { # no file was found }

You may want to even share whatever 'stuff' you have to do:

if ( -f $txtfile || -f $txtfile1 ) { if ( -f $txtfile ) { open(FILE, "$txtfile"); } else { open(FILE, "$txtfile1"); } # do stuff with FILE close (FILE); } else { # no file was found }

Disclaimer: Code is untested and there are probably better ways to streamline the code - aiming for readability.


Replies are listed 'Best First'.
Re: Re: Files and -f
by mndoci (Scribe) on Dec 01, 2001 at 01:53 UTC
    Duh!!, Me that is. You are, of course, quite right.
    Thanks for the tip.


    "What you do in this world is a matter of no consequence. The question is, what can you make people believe that you have done?"-Sherlock Holmes in 'A study in scarlet'

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2019-11-22 21:07 GMT
Find Nodes?
    Voting Booth?
    Strict and warnings: which comes first?

    Results (113 votes). Check out past polls.