Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

command move and qq{}

by steph_bow (Pilgrim)
on Mar 21, 2008 at 15:50 UTC ( [id://675447]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I have some problems with the command move and qq{}

For example, the following command works well

my $cmda=qq{mkdir dir_M1_${week}_comptage_${comptage}_${index}};
but this command does not work
my $cmdc2= qq{mv M3.*txt ../../../dir_first_action_${week}/dir_M3_${we +ek}_comptage_${comptage}_${index}};

Could you please explain why ? Thanks a lot

update : Sorry guys: I forgot the mv for the second line of code

Replies are listed 'Best First'.
Re: command move and qq{}
by FunkyMonk (Chancellor) on Mar 21, 2008 at 16:03 UTC
    my ( $week, $comptage, $index ) = qw{ W C I }; my $cmdc2= qq{mv M3.*txt ../../../dir_first_action_${week}/dir_M3_${we +ek}_comptage_${comptage}_${index}}; print $cmdc2; #Output: #mv M3.*txt ../../../dir_first_action_W/dir_M3_W_comptage_C_I

    How does it "not work"?

    Are you expecting the unix command mv to run? If so, you need to use qx{} or backticks, not qq{}. See Quote Like Operators in perlop.

    Update: Forgot to mention backticks

Re: command move and qq{}
by doc_faustroll (Scribe) on Mar 21, 2008 at 19:12 UTC

    Here is a suggestion, if not a specific answer. In the general case of system calls, system is generally preferable. as in:

    my $command = qq{syscall $interp}; run_command_string($command); sub run_command_string { my $command = shift; $log->info("attempt to run the following command:"); $log->info("$command"); system $command; if ( $? == -1 ) { $log->error("failed to execute: $!\n"); } elsif ( $? & 127 ) { $log->error("child died with signal $? $!"); } else { $log->info("child exited with value $?"); } }
    However, in the specific case of a move, I prefer File::Copy and for mkdir I prefer File::Path.
    use File::Copy; use File::Path; #my $filename, $file2 declared somewhere move( "$filename", "$file2" ) or die "move failed: $!"; #check File::Path's mkpath for use
Re: command move and qq{}
by nefigah (Monk) on Mar 22, 2008 at 04:49 UTC

    As an observation, it would seem to make sense that since you can pick any delimiter for a qq "quote," if you are going to be using curly braces within the string, why not pick something other than curly braces as the delimiter? (It seems you aren't having to escape them, but for readability if nothing else)

    Just a comment because I think being able to choose delimiters is a cool feature :)


    I'm a peripheral visionary... I can see into the future, but just way off to the side.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-16 23:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found