SPUG: Re: Using mkpath

Yitzchak Scott-Thoennes sthoenna at efn.org
Fri Mar 1 11:42:26 CST 2002

In article <014701c1c132$3a75f8a0$2088ddd1 at aciwin>,
"Richard Anderson" <richard at richard-anderson.org> 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: $!";

Three points:

1) The system "mkdir" version would ignore errors, so an equivalent
mkpath should too.

2) The above code is definitely wrong.  If mkpath has an error, it
will die and your die call will not be triggered.  The only case
where mkpath returns false is if the directory already exists.
Dieing with "Can't create directory" in this case seems a little
absurd.  The only way to check if mkpath had an error is with eval.

3) eval BLOCK has little overhead.  I think you are thinking of eval EXPR.

>richard at richard-anderson.org
>----- Original Message -----
>From: "Yitzchak Scott-Thoennes" <sthoenna at efn.org>
>To: <spug-list at pm.org>
>Cc: <richard at richard-anderson.org>
>Sent: Thursday, February 28, 2002 9:22 PM
>Subject: Re: SPUG: Re: HTTP::Cookies question
>> In article <00a801c1c0a2$303a5a10$2088ddd1 at aciwin>,
>> "Richard Anderson" <richard at richard-anderson.org> wrote:
>> >On an unrelated note, it is more portable and less error prone to use
>> >use File::Path;
>> >mkpath($newdir)
>> >
>> >than
>> >system "mkdir -p $newdir"
>> >since on some OSes mkdir does not have the -p option.
>> To be equivalent, you need to make that:
>> eval { mkpath($newdir) }
>> since mkpath croaks on errors.

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     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