SPUG: Yet another silly question

Ben Reser ben at reser.org
Sat Jan 19 17:08:33 CST 2002

On Sat, Jan 19, 2002 at 02:22:46PM -0800, Susanne Bullo wrote:
>    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?

my $mynewdirpath = 'perltest1';
unless (mkdir($mynewdirpath) || ($! eq 'File exists' && -d $mynewdirpath)) {
  die "Was unable to create directory $mynewdirpath";

That's how I'd do it.  Or you can use the file test flags before you
make the directory (strongly discouraged because of race conditions.
i.e. someone could make the directory before you and hose you).

my $mynewdirpath = 'perltest2';
unless (-d $mynewdirpath) {
  mkdir($mynewdirpath) || die "Was unable to create directory $mynewdirpath";

Ben Reser <ben at reser.org>

"I wish it need not have happened in my time," said Frodo.
"So do I," said Gandalf, "and so do all who live in such times. But
that is not for them to decide.  All we have to decide is what to do
with the time that is given us."

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