Cal Poly | NetApp
Posts tagged perl
Avoiding ‘mkdir -p’ in Perl by using File::Path::make_path()
Jul 7th
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.
![[Google]]( http://ericosgood.com/wp-content/plugins/easy-adsenser/google-light.gif)
Recent Comments