Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

mkdir die

by zakzebrowski (Curate)
on Oct 19, 2001 at 23:38 UTC ( #120122=perlquestion: print w/replies, xml ) Need Help??
zakzebrowski has asked for the wisdom of the Perl Monks concerning the following question:

I have a block of code:
eval { mkdir $DIR_LOCATION, 0777 || die "sigh"; }; if ($@) { print "Never gets here to display error message."; }
Anyone knows why this code isn't dieing correctly? (Perl version 5.005_3 for solaris)? This is not a *huge* problem (most of the time I will be creating the directory), but I would to at least log the dir wasn't created for idiot checking.
Thanks
Zak


----
Zak

Replies are listed 'Best First'.
Re: mkdir die
by blakem (Monsignor) on Oct 19, 2001 at 23:46 UTC
    try
    mkdir $DIR_LOCATION, 0777 or die "sigh";
    Your original code was being parsed as:
    mkdir $DIR_LOCATION, (0777 || die "sigh");
    Update: I got pulled into a meeting before I could elaborate, but others have now explained it nicely.

    -Blake

Re: mkdir die
by DrManhattan (Chaplain) on Oct 20, 2001 at 00:19 UTC

    (Original reply went to a duplicate node)

    Use 'or' rather than '||'. The precedence of 'or' is much lower, so everything to the left of it will be evaluated, rather than the first expression directly to the left. Explicitly parenthesized, your command looks like this:
    mkdir $DIR_LOCATION, (0777 || die "sigh");
    Which evaluates to
    mkdir $DIR_LOCATION, 0777;
    Using 'or', it's like this:
    (mkdir $DIR_LOCATION, 0777) or (die "sigh");
    The entire mkdir() expression is evaluated.

    -Matt

Re: mkdir die
by RhetTbull (Curate) on Oct 20, 2001 at 00:24 UTC
    I posted this on of the duplicate copies of this node but it got reaped so here it is again. As blakem pointed out, it's a matter of operator precedence. This gets interpreted as mkdir $DIR_LOCATION, (0777 || die "sigh"); Use parens on your mkdir call:
    eval { mkdir ($DIR_LOCATION, 0777) || die "sigh"; }; if ($@) { print "Never gets here to display error message."; }
    blakem's solution works too since or has lower precedence than || but I personally prefer parens and ||. Either way works fine -- do whicher you like better. --RT
      Thanks everyone. Forgot about the ()'s & precedence... Using opera for my web browser and for some reason it reposted the question for the duplicate nodes... sorry about that. Anyway, thanks for helping me get it to work!

      ----
      Zak

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://120122]
Approved by root
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2018-02-18 09:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (253 votes). Check out past polls.

    Notices?