smturner1 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to understand why this piece of code is not creating the directory, which should be created C:/Desktop/$website$old_ip$TIMESTAMP. I also would like to know how I can place a dot between the vars C:/Desktop/$website.$old_ip.$TIMESTAMP.
use strict; use warnings; use diagnostics; use autodie; use File::Spec; use File::Path qw( make_path remove_tree ); #Set command line arguments my ($website, $old_ip, $new_ip) = @ARGV; #Set vars my $TIMESTAMP = strftime("%Y%m%d%H%M", localtime); + my $volume = 'C:/'; + my $ARCHIVE = File::Spec->catpath($volume, qw(Desktop), $websit +e, $old_ip, $TIMESTAMP); my @WEBSITES = qw( three five calnet-test ); my $ARCHIVE = File::Spec->catpath($volume, qw(Desktop), $website, $ol +d_ip, $TIMESTAMP); #Creates the new dir in archive sub initialize { make_path ($ARCHIVE); return; } initialize;

Replies are listed 'Best First'.
Re: Why can't I create a directory with File::Spec
by kcott (Archbishop) on Feb 07, 2014 at 05:51 UTC

    G'day smturner1,

    The code you posted should be giving warnings about my $ARCHIVE = ... appearing twice in the same scope. E.g.

    $ perl -Mwarnings -e 'my $x = 1; my $x = 2;' "my" variable $x masks earlier declaration in same scope at -e line 1.

    So, either that's not your actual code or you're hiding messages from us. Does autodie, for instance, provide feedback you're not showing?

    You should check the File::Spec documentation. Your catpath() code does not match the documented syntax, i.e.

    $full_path = File::Spec->catpath( $volume, $directory, $file );

    See join for placing dots between your values.

    Use a print statement to see the actual value of $ARCHIVE before calling make_path().

    -- Ken

      Ken, thank you for catching the error. I added it in the post, but it is not in the code I wrote. Please see new code:

      use strict; use warnings; use diagnostics; use autodie; use File::Spec; use File::Path qw( make_path remove_tree ); #Set command line arguments my ($website, $old_ip, $new_ip) = @ARGV; #Set vars my $volume = 'C:/'; + my $ARCHIVE = File::Spec->catpath($volume, qw(Desktop), $website, $ +old_ip, $TIMESTAMP); my $TIMESTAMP = strftime("%Y%m%d%H%M", localtime); my @WEBSITES = qw( three five calnet-test ); #Creates the new dir in archive sub initialize { make_path ($ARCHIVE); return; } initialize;
Re: Why can't I create a directory with File::Spec
by Anonymous Monk on Feb 07, 2014 at 07:13 UTC