Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Subtleties of files and directories

by DeusVult (Scribe)
on Mar 23, 2001 at 01:21 UTC ( [id://66469]=perlquestion: print w/replies, xml ) Need Help??

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

Is a directory a file? Do the file operators of perl treat them that way? For example,
my $dir = "directory"; # this dir exists my $no_dir = "no.such.directory"; # this one does not print "$dir exists" if ( -e $dir ); print "$dir is a directory" if ( -d $dir ); print "$no_dir exists" if ( -e $no_dir ); print "$no_dir is a directory" if ( -d $no_dir );

My first question is with the first line. Will perl consider $dir to exist and do so for all platforms (that's the important part, otherwise I'd just test it, but I need access to the inner secrets here)?

Secondly, is it defined to check if a meaningless word is a directory? Will the last line cause no errors (again, on all platforms)?

And finally, on a related note, what happens if I write

use File::Path; rmtree( [$name], 0, 0 );

and $name is the name of a file, not a directory?

Thanks in advance.

Some people drink from the fountain of knowledge, others just gargle.

Replies are listed 'Best First'.
(tye)Re: Subtleties of files and directories
by tye (Sage) on Mar 23, 2001 at 01:56 UTC

    -e (and -d) will be true for directories but -f won't (yes, for all operating systems).

    Reading the actual code of sub rmtree in File::Path shows that if it is passed something that isn't a directory and the third argument to rmtree() is false, then it tries hard to delete it.

            - tye (but my friends call me "Tye")
Re: Subtleties of files and directories
by clintp (Curate) on Mar 23, 2001 at 01:53 UTC
    In general no. File operators and directory operators are not compatable with each other. You can test things out and see for yourself. Hey, what's the worst that could happen? Don't test it out as root though. :)

    It might work for some things, but don't rely on it.*

    *And at one point in Unix's history they were compatable to a large extent. It was entirely possible to say "ex /tmp" and have a directory listing pulled up in your editor. (SysVR3 anyone?)

      >*And at one point in Unix's history they were compatable to a large extent. It was entirely possible to say "ex /tmp" and have a directory listing pulled up in your editor. (SysVR3 anyone?)

      Actually, emacs still does this. Try saying emacs /tmp instead, using dired-mode.

Re: Subtleties of files and directories
by SparkeyG (Curate) on Mar 23, 2001 at 01:48 UTC
    While I'm not completly sure, the line  print "$dir exists" if ( -e $dir ); should work on all platforms. This is because of perl's unix roots, (gosh are those still showing :)
    In unix, anything within the filesystem is a file, it just depends if it s a file file, a directory file, a socket file, etc. etc.

    On your second question, a quick read through  perldoc File::Path leads me to beleive that your code will not work. And further read through the code, perldoc -m File::Path, leads me to believe the opposite, see Tye's post.

    --SparkeyG

    --Update: Corrected after reading Tye's post and further investigating the code.

Log In?
Username:
Password:

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

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

    No recent polls found