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

"-e" and "-d" switches

by mosh (Scribe)
on Mar 24, 2005 at 13:43 UTC ( #442039=perlquestion: print w/replies, xml ) Need Help??

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

Hi All,
What's the differences between the two switches "-d" and "-e" :
if($cfg{'RootDir'} and not -d($cfg{'RootDir'}) ) if($cfg{'FileName'} and not -e($cfg{'FileName'}) )

What does it means those switches?

Thanks,
Mosh.

Replies are listed 'Best First'.
Re: "-e" and "-d" switches
by dragonchild (Archbishop) on Mar 24, 2005 at 13:47 UTC
    -d checks to see if something is a directory. -e checks to see if something exists. q.v. Filetest operators for more info.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: "-e" and "-d" switches
by brian_d_foy (Abbot) on Mar 24, 2005 at 13:49 UTC

    These are the file test operators (which are different from the command line switches that look the same). They're documented in perlfunc.

    In short, -d tests if its argument is a directory, and -e tests if its argument exists.

    In your code, it looks like the if() ensures that there is a true value for those hash keys and that the file or directory does not already exist. The next part probably creates them.

    --
    brian d foy <bdfoy@cpan.org>
Re: "-e" and "-d" switches
by artist (Parson) on Mar 24, 2005 at 13:48 UTC
    -d => If it is a directory
    -e => If it exists
Re: "-e" and "-d" switches
by manav (Scribe) on Mar 24, 2005 at 14:14 UTC
    As an addendum,
    -d implies -e ,whereas
    -e does not imply -d


    Manav
      Not quite.
      • A successful -d implies a successful -e.
      • A failed -e implies a failed -d.
      However ...
      • A failed -d says nothing about the success/failure of -e
      • A successful -e says nothing about the success/failure of -d

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

        Manav wrote:

        • -d implies -e
        • -e does not imply -d

        dragonchild reasponded:

        Not quite.

        Actually in a formal logic sense your (dragonchild's) four statements are equivalent to manav's two. Or indeed the single statement:

        • -d implies -e
        -d implies -e
        It was a positive implication.
        ! -e implies ! -d
        is negative implication.

        -e does not imply -d
        probably should have been
        -e does not necessarily imply -d



        Manav
      Thanks Guys !!
Re: "-e" and "-d" switches
by ambs (Pilgrim) on Mar 25, 2005 at 18:34 UTC

    The '-x' switches (perldoc -f -x) test various file properties.

    In your case, -d "filename" tests if "filename" is a directory. -e "filename" just checks if it exists. For example, -f "filename" will tell you if the file is a plainfile.

    You can use these switches to get other information from files like age or last change.

    Alberto Simões

    Python's syntax succeeds in combining the mistakes of Lisp and Fortran. I do not contrue that as progress.

    -- Larry Wall

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (1)
As of 2021-12-05 23:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (31 votes). Check out past polls.

    Notices?