Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

incorrect syntax when using mkpath

by steph_bow (Pilgrim)
on Mar 25, 2008 at 11:06 UTC ( [id://676092]=perlquestion: print w/replies, xml ) Need Help??

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

Dea Monks,

I have an incorrect syntax when using mkpath but I cannot correct my error. I want to create the directories but it does not seem to work. Could you help me ? Thanks

Here is my code :

#! usr/bin/perl -w use strict; use File::Copy; use File::Path; my $date = $ARGV[0]; my $comptage = $ARGV[1]; my $C = $comptage; # index : input ou output my $index = $ARGV[2]; my $I = $index; my $week = $ARGV[3]; my $W = $week; print STDOUT "the week is ${W}\n"; my $cmda=qq{mkpath('M1_fa_${W}/M1_${W}_comptage_${C}_${I}_first_action +')}; my $cmdb=qq{mkpath('M2_fa_${W}/M2_${W}_comptage_${C}_${I}_first_action +')}; my $cmdc=qq{mkpath('M3_fa_${W}/M3_${W}_comptage_${C}_${I}_first_action +')}; system($cmda); system($cmdb); system($cmdc);

I get the error message

sh -c: line 0: syntax error near unexpected token ' 'M3_fa_W42/M3_W42_ +comptage_60_output_first_action' ' <code>
update: added some elements to take into account Corion's remark

Replies are listed 'Best First'.
Re: incorrect syntax when using mkpath
by moritz (Cardinal) on Mar 25, 2008 at 11:22 UTC
    mkpath is not a command on your system, but a perl function:
    use File::Path qw(mkpath); ... mkpath("M1_fa_$W/M1_${W}__comptage_${C}_${I}_first_action"); ...

    If you want to use system for some other reason (usually not recommended), the unix equivalent is mkdir -p.

    (BTW you might want to use sprintf to create the file names, that could be much easier than your interpolation).

Re: uncorrect syntax when using mkpath
by Corion (Patriarch) on Mar 25, 2008 at 11:14 UTC

    The syntax is OK:

    X:\>perl -c tmp.pl tmp.pl syntax OK X:\>type tmp.pl #! usr/bin/perl -w use strict; use File::Copy; use File::Path; my $date = $ARGV[0]; my $comptage = $ARGV[1]; my $C = $comptage; # index : input ou output my $index = $ARGV[2]; my $I = $index; my $week = $ARGV[3]; my $W = $week; print STDOUT "the week is ${W}\n"; my $cmda=qq{mkpath('M1_fa_${W}/M1_${W}_comptage_${C}_${I}_first_action +')}; my $cmdb=qq{mkpath('M2_fa_${W}/M2_${W}_comptage_${C}_${I}_first_action +')}; my $cmdc=qq{mkpath('M3_fa_${W}/M3_${W}_comptage_${C}_${I}_first_action +')};

    Maybe you want to be more explicit about where your problem is, that is, what your program should do, what it does, and how that differs from what you expect.

    Maybe it is already enough for you to read the quotes section in perlop, Quote and Quote-like Operators.

Re: incorrect syntax when using mkpath
by Pancho (Pilgrim) on Mar 25, 2008 at 13:11 UTC

    ... and to add to Moritz comment, to form the path in a way that is more portable see the File::Spec module in CPAN, specifically the catfile method...

    Finally, you probably have this module already, depending on what version of Perl you are running.

    Pancho
      corelist File::Spec File::Spec was first released with perl 5.00405

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://676092]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-20 10:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found