Cal Poly | NetApp
Avoiding ‘mkdir -p’ in Perl by using File::Path::make_path()
Using “ (back ticks) to implement the unix ‘mkdir -p’ is a “ghetto hack” and can now be circumvented by using File::Path::make_path. Here is some example code that implements that functionality:
###############################################################################
# Subroutine: mkdir
#
# Function that emulates unix’s “mkdir -p”
#
# Arguments: Scalar String – the path in question
#
# Returns: Scalar – either an integer representing the number of new directories created or an error message
sub mkdir($) {
my $path = shift;
my $err_msg;
# attempt a ‘mkdir -p’ on the provided path and catch any errors returned
my $mkdir_out = make_path( $path, { error => \my $err } );
# catch and return the error if there was one
if (@$err) {
for my $diag (@$err) {
my ( $file, $message ) = %$diag;
$err_msg .= $message;
}
print “$err_msg”;
} else {
print “$mkdir_out”;
}
} ## end sub mkdir($)
this subroutine will will try to create the path specified and will either print an int representing the number of new directories created or an error message.
| Print article | This entry was posted by eosgood on July 7, 2010 at 9:39 am, and is filed under Programming. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
![[Google]]( http://ericosgood.com/wp-content/plugins/easy-adsenser/google-light.gif)
about 1 month ago
Amiable dispatch and this post helped me alot in my college assignement. Gratefulness you seeking your information.
about 1 month ago
Sure thing, feel free to post questions too – Im knowledgable with Perl, Java and C
about 1 month ago
it was very interesting to read ericosgood.com
I want to quote your post in my blog. It can?
And you et an account on Twitter?
about 1 month ago
Yeah that’s cool – send me link to it when you have posted it and as long as you site my website as the source that is fine
about 1 month ago
I would like to exchange links with your site ericosgood.com
Is this possible?
about 1 month ago
Sounds good to me. What is your website?
about 2 weeks ago
Amiable post and this post helped me alot in my college assignement. Thank you seeking your information.