Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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.


Replies are listed 'Best First'.
Re: mkdir die
by blakem (Monsignor) on Oct 19, 2001 at 23:46 UTC
    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.


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.


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!


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2021-04-19 06:28 GMT
Find Nodes?
    Voting Booth?

    No recent polls found