SPUG: Yet another silly question
Jason Lamport
jason at strangelight.com
Mon Jan 21 13:47:26 CST 2002
At 8:10 am -0800 1/21/02, Pommert, Daniel wrote:
>I have found that mkpath will check to see if the directory is there before
>it tries to create it. So, checking is not necessary when using mkpath.
Is this behaviour in the documentation for File::Path, or is this
simply something you've observed? I personally consider it poor
programming practice to rely on undocumented behaviour, even in
trivial cases such as this. (And even if it is part of the formal
spec, an extra check rarely hurts.)
Sanity Checks are Good Things.
-jason
>
>So, the code below would better be written:
>
>#!/usr/bin/perl -w
>#
>$dir = "some/directory/path";
>$base = "/usr/people/moonbeam";
>
>use File::Path;
>
>if ( ! -f "$base/$dir" ) {
> mkpath("$base/$dir", 0, 0755 ) or
> die "Cannot make $dir/$base - $!\n";
>}
>else {
> print STDERR, "$base/$dir already exists as a file!\n";
>}
>
>-----Original Message-----
>From: William Julien [mailto:moonbeam at catmanor.com]
>Sent: Saturday, January 19, 2002 2:48 PM
>To: sweetsue at sweethomes.com
>Cc: spug-list at pm.org
>Subject: Re: SPUG: Yet another silly question
>
>
>>
>>Ok, I am trying to figure out the coding for checking to see if a directory
>>exists and if it doesn't, create it. I've gotten out my "Perl For Dummies"
>>book and did a lot of searching and am still stumped. Anyone have a quick
>>and easy answer for me?
>>
>>Thanks!
>>
>>Susanne Bullo - Sweet Homes
>>
>
>Maybe something like this:
>
>#!/usr/bin/perl -w
>#
>$dir = "some/directory/path";
>$base = "/usr/people/moonbeam";
>
>use File::Path;
>
>if ( ! -f "$base/$dir" ) {
> if ( ! -d "$base/$dir" ) {
> mkpath("$base/$dir", 0, 0755 ) or
> die "Cannot make $dir/$base - $!\n";
> }
>}
>
>Here is the perldoc on File::Path:
>
>NAME
> File::Path - create or remove directory trees
>
>SYNOPSIS
> use File::Path;
>
> mkpath(['/foo/bar/baz', 'blurfl/quux'], 1, 0711);
> rmtree(['foo/bar/baz', 'blurfl/quux'], 1, 1);
>
>
>DESCRIPTION
> The "mkpath" function provides a convenient way to create
> directories, even if your "mkdir" kernel call won't create
> more than one level of directory at a time. "mkpath"
> takes three arguments:
>
> o the name of the path to create, or a reference to a
> list of paths to create,
>
> o a boolean value, which if TRUE will cause "mkpath" to
> print the name of each directory as it is created
> (defaults to FALSE), and
>
> o the numeric mode to use when creating the directories
> (defaults to 0777)
>
> It returns a list of all directories (including intermedi-
> ates, determined using the Unix '/' separator) created.
>
>William
>
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> 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://zipcon.net/spug/
>
>
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> 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://zipcon.net/spug/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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://zipcon.net/spug/
More information about the spug-list
mailing list