SPUG: Re: Using mkpath (was: HTTP::Cookies question)

dancerboy dancerboy at strangelight.com
Fri Mar 1 12:41:52 CST 2002

At 7:03 am -0800 3/1/02, Richard Anderson wrote:
>No need to use eval, which introduces unneeded complexity and slows
>execution speed. File::Path::mkpath returns the number of directories
>successfully created, so the right way to call it is:
>use File::Path qw(mkpath);
>unless (mkpath($nameOfNewDir)) {
>     die "Can't create directory $nameOfNewDir: $!";


1.  The whole point of using eval{ } is so that the script WON'T die 
if mkpath() fails.

2.  In the above code, if mkpath() actually fails, your die 
instruction will never get executed (because mkpath() will have 
already croak'ed before Perl finishes evaluating the unless() clause).

3.  mkpath() returns the directories actually *created* -- therefore, 
the above code will die if $nameOfNewDir already exists (and since 
this isn't an error condition, $! won't be set to anything 
meaningful).  To be accurate, you ought to rewrite the die message 
something like this:

     unless (mkpath($nameOfNewDir)) {
         die "No new directories created. ($nameOfNewDir already exists?)";

But I doubt if this is really the program behaviour that you want.

4.  eval{ } does not slow down program execution significantly. 
(Don't confuse eval{ } with eval() -- the latter form takes a string, 
and *is* slower, because it needs to compile the string.  The former 
form takes a code block and simply tells the compiler to handle fatal 
errors inside that code block a non-fatal way.)  Moreover, eval{ } 
*is* the only mechanism for suppressing/capturing fatal errors.[*]


[*] At least, it's the only *reasonable* method.  I mean, you could 
fork off a child process and execute the code in the child (and 
getting the result by opening pipes to the child's STDERR, e.g. using 
IPC::Open3), or save the code into a file and execute it through qx{ 
}, or use any number of other utterly pathological techniques...

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org

More information about the spug-list mailing list