[Pdx-pm] mkdir -p ?

Austin Schutz tex at off.org
Fri Aug 24 14:49:58 PDT 2007

> Which would you rather work with?
> 	use File::Path;
> 	mkpath($path) or die "Can't mkpath $path";
> Or
> 	# Calling system() as a list at least avoids the shell.
> 	unless( system("/bin/mkdir", "-p", $path) == 0 ) {
>             die "Can't mkdir -p $path";
>         }

	Actually I'd rather use the second, given that mkpath will die
on failure, so your error message will fail. Presumably you wouldn't do
that in your actual code, you were merely exemplifying why packaged code
is handy.
	It's also handy to know why something fails.

use File::Path;
eval { mkpath($path); };
die "Can't mkpath $path: $@" if $@;

	Even better to use something like Error and throw exceptions.

	And here you thought something trivial like making a directory was
going to be simple in Perl. :-)


More information about the Pdx-pm-list mailing list