How to execute tar inside a perl script ?

M

Matthew Lincoln

When I code in a perl script

tar czvf "/usr/local/myname/$tarfile" ./*.php

I got the following error:

Search pattern not terminated at /usr/local/myname/bin/myscript.pl line 20.

If I code:

tar czvf "/usr/local/myname/$tarfile" ./*.php ./templates/

then I got this error:

Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE .php ./ at /usr/local/myname/bin/myscript.pl line 20.

So how do I correctly execute a tar command from within a perl script ?

Matthew
 
J

Joost Diepenmaat

This is a correct, but it's prob worth expanding on this slightly to get
you going, from within your code, try the following:

my $tar_cmd = "tar czvf "/usr/local/myname/$tarfile" ./*.php" ;

That's not valid perl. You probably mean

my $tar_cmd = "tar czvf /usr/local/myname/$tarfile ./*.php" ;

Joost.
 
B

Ben Morrow

Quoth Joost Diepenmaat said:
That's not valid perl. You probably mean

my $tar_cmd = "tar czvf /usr/local/myname/$tarfile ./*.php" ;

No, he probably meant

my $tar_cmd = qq{tar czvf "/usr/local/myname/$tarfile" ./*.php};

as the "" are important. It would be *much* better to use the list form
of system: it avoids all these nasty quoting issues, including the one
you haven't noticed yet when you have a file 'My Script.php'.

system tar => czvf => "/usr/local/myname/$tarfile", <*.php>;

(handling the return value left as an exercise for the reader, as it's a
pain :).)

Ben
 
J

Joost Diepenmaat

No, he probably meant

my $tar_cmd = qq{tar czvf "/usr/local/myname/$tarfile" ./*.php};

Which won't work, because it won't interpolate ./*.php. Which sort of
proves both our points :)
as the "" are important. It would be *much* better to use the list form
of system: it avoids all these nasty quoting issues, including the one
you haven't noticed yet when you have a file 'My Script.php'.

system tar => czvf => "/usr/local/myname/$tarfile", <*.php>;

Yes, that would work, and more reliably than all the crap above. (though
I don't like => to quote strings except in very idiomatic constructs).

Joost.
 
B

Ben Morrow

Quoth Joost Diepenmaat said:
Which won't work, because it won't interpolate ./*.php. Which sort of
proves both our points :)

Yes it will. That is, ./*.php will be passed literally to the shell,
which will then expand it before calling tar. This is why $tarfile needs
two levels of quoting, with qq{} (for Perl) and "" (for the shell).

Ben
 
J

Josef Moellers

Ben said:
Yes it will. That is, ./*.php will be passed literally to the shell,
which will then expand it before calling tar. This is why $tarfile needs
two levels of quoting, with qq{} (for Perl) and "" (for the shell).

(Stomping the ground with the foot) No it won't:

The list form of system will bypass the shell:
josef@bounty:/usr/lib> perl
system("ls", "*");
ls: *: No such file or directory

Hence, no expansion will take place.

Josef
 
M

Martijn Lievaart

(Stomping the ground with the foot) No it won't:

The list form of system will bypass the shell: josef@bounty:/usr/lib>
perl
system("ls", "*");
ls: *: No such file or directory

Hence, no expansion will take place.

It's no list, it's a single string with wildcard characters, hence it
will be passed to the shell and it will be expanded.

# perl -e 'system qq{ls *}'
<snip listing of files>

M4
 
J

Josef Moellers

Martijn said:
It's no list, it's a single string with wildcard characters, hence it
will be passed to the shell and it will be expanded.

# perl -e 'system qq{ls *}'
<snip listing of files>

Sorry, you're right, I mixed up both examples in Ben's reply and didn't
notice the globbing operator in this section:

It would be *much* better to use the list form
of system: it avoids all these nasty quoting issues, including the one
you haven't noticed yet when you have a file 'My Script.php'.

system tar => czvf => "/usr/local/myname/$tarfile", <*.php>;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top