Thanks for any help.
>I've looked through several Perl books but can't seem to find how
>to use the Unix "cp" and "mv" commands from within Perl. Is there
>a way? What I'd like to do is be able to copy several files and
>"move" a particular file to another subdirectory and overwrite
>an existing file at an appropriate place in the Perl script.
The general tactic is to use "system", e.g.
system "cp file1 file2 ... directory";
If you want to "move" a file to a subdirectory, you can do
rename($file, "$subdirectory/$file") || die "rename failed: $!\n";
for other types of move (such as to another hard disk partition, the
simplest way is again to use system().
It's *possible* to write perl code to copy a file. Something like
open(IN, $source) || die "can't open $source: $!\n";
open(OUT, ">$destination") || die "can't create $destination: $!\n";
$bufsize = (stat IN)[11] || 8192;
syswrite(OUT, $buf, length $buf) while sysread(IN, $buf, $bufsize);
close IN;
close OUT;
would work, at least for the simple case of copying a regular file to
a disk that doesn't get full. But it wouldn't buy you any extra speed,
since the dominant factor is typically I/O speed anyway.
You can generalize the code above to handle more interesting cases (e.g.
copying directories recursively). By the time it does everything the
real "cp" does, you'll have learned quite a lot about Perl and Unix.
For practical purposes, system("cp") works just fine.
--
Hope this helps,
HansM
: Thanks for any help.
The `` allow the use of system commands with interpolation.
`cp $file1 $newfilename`;
Shelly> The `` allow the use of system commands with interpolation.
Shelly> `cp $file1 $newfilename`;
But this does more work than it needs to. You are setting up a pipe
to capture the STDOUT of the command, and then throwing the result
away.
Probably you meant to say:
system "cp $file1 $newfilename";
which is a little easier on Perl to perform.
print "Just another Perl hacker," # but not what the media calls "hacker!" :-)
# legal fund: $3125.03 collected, $55879.50 spent; email fu...@stonehenge.com for details
--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <mer...@stonehenge.com> Snail: (Call) PGP-Key: (finger mer...@ora.com)
Web: <A HREF="http://www.teleport.com/~merlyn/">My Home Page!</A>
It also means that you'll see the stdout, if any. So I second the
vote for using system of `` unless you really want to do further
processing on the output.
--
Danny Faught -- Convex -- Operating System Demolitions Specialist
Magister artis ingeniique largitor venter.
This does not seem to work, we want to extract a number from a variable
within a perl script and use it as an argument to a command line program.
Any help would be appreciated.
>In article n...@acmez.gatech.edu, gt8...@prism.gatech.edu (Shelly Hayes) writes:
>> Eric Hilding (er...@garlic.com) wrote:
>> : I've looked through several Perl books but can't seem to find how
>> : to use the Unix "cp" and "mv" commands from within Perl. Is there
>> : a way? What I'd like to do is be able to copy several files and
>> : "move" a particular file to another subdirectory and overwrite
>> : an existing file at an appropriate place in the Perl script.
>> The `` allow the use of system commands with interpolation.
>> `cp $file1 $newfilename`;
Why do so may people use `` when what they want is system:
system "cp $file1 $newfilename";
The difference is that system sends any output produced by cp where it
belongs, whereas `` drops it on the floor. That is, if you do
$cwd = `pwd`;
chop $cwd;
then the output of `pwd` is stored in the variable. But if you simply do
`pwd`;
then pwd is run and its output goes nowhere.
Well, I guess you could do
print `date`;
when what you really want is
system "date";
But this tactic is inefficient and it suffers from output being shown
later than expected due to stdio buffering.
>This does not seem to work, we want to extract a number from a variable
>within a perl script and use it as an argument to a command line program.
That should work, using either `` or system.
>Any help would be appreciated.
If you post a piece of perl code that doesn't do what you think it should do,
we may be able to diagnose the problem.
> In article n...@acmez.gatech.edu, gt8...@prism.gatech.edu (Shelly Hayes)
writes:
> > The `` allow the use of system commands with interpolation.
> >
> > `cp $file1 $newfilename`;
> >
> >
>
>
>
> This does not seem to work, we want to extract a number from a variable
> within a perl script and use it as an argument to a command line program.
> Any help would be appreciated.
Be more specific. That method should work fine. If $x=2, and you want to
provide the argument '2' to some command line program, you should be able
to:
`command-line-program $x`;
Note that this method won't print output to STDOUT, it returns output, so
you can capture it with $variable=`command-line-program`;
If you don't care about saving the output from command-line-program (or if
you want it to go to STDOUT:
system("command-line-program $x");
>In article n...@acmez.gatech.edu, gt8...@prism.gatech.edu (Shelly Hayes) writes:
>> Eric Hilding (er...@garlic.com) wrote:
>> : I've looked through several Perl books but can't seem to find how
>> : to use the Unix "cp" and "mv" commands from within Perl. Is there
>> : a way? What I'd like to do is be able to copy several files and
>> : "move" a particular file to another subdirectory and overwrite
>> : an existing file at an appropriate place in the Perl script.
>>
>> : Thanks for any help.
>>
>> : er...@garlic.com
>>
>> The `` allow the use of system commands with interpolation.
>>
>> `cp $file1 $newfilename`;
>>
>>
>This does not seem to work, we want to extract a number from a variable
>within a perl script and use it as an argument to a command line program.
>Any help would be appreciated.
It works for me. Are you sure you're using the backticks `` and not
the single quote characters '' ?
Beverly J. Brown
b...@shore.net
bev...@datacube.com