Perl command to copy one file into another

B

Bill

while ( <INPUT1> ) {
# print "Reading $ARGV[0]\n";
print OUTPUT $_;
}

The copy statment, copy("file1","file2") or die "Copy failed: $!"; is
supposed to replace the above while statement.

I get this error with the copy statment ...
C:\PERL1\OUTPUT7>bbbb5.pl output5.txt output6.txt output7.txt
Undefined subroutine &main::copy called at C:\PERL1\OUTPUT7\bbbb5.pl line
24.

This is the script code ...

open (INPUT1, "$ARGV[0]") or die "Cannot open $ARGV[0]";
open (OUTPUT, "> $ARGV[2]");

#print "First input file is: $ARGV[0]\n";
#print "Second input file is: $ARGV[1]\n";
#print "The output file is: $ARGV[2]\n\n";

# This half of the code dumps everything in 'file1' to the output 'file3'
# while ( <INPUT1> ) {
# print "Reading $ARGV[0]\n";
# print OUTPUT $_;
# }

File :: COPY;
copy("$ARGV[0]","$ARGV[2]") or die "Copy failed: $!";

# This half of the code compares 'file2' to 'file1' and writes out
# any line that doesn't match to the output 'file3'

open (INPUT1, "$ARGV[1]") or die "Cannot open $ARGV[1]";

while ( <INPUT1> ) {
$match = 0;

$a = $_;
open (INPUT2, "$ARGV[0]") or die "Cannot open $ARGV[0]";
while ( <INPUT2> ) {
$b = $_;
if ($a eq $b) {
$match = 1;
last;
}
}

if ($match == 0) {
print OUTPUT $a;
}
}
 
D

David Efflandt

while ( <INPUT1> ) {
# print "Reading $ARGV[0]\n";
print OUTPUT $_;
}

The copy statment, copy("file1","file2") or die "Copy failed: $!"; is
supposed to replace the above while statement.

I get this error with the copy statment ...
C:\PERL1\OUTPUT7>bbbb5.pl output5.txt output6.txt output7.txt
Undefined subroutine &main::copy called at C:\PERL1\OUTPUT7\bbbb5.pl line
24.

Read 'perldoc File::Copy' or example at bottom more carefully.
You forgot the 'use' in 'use File::Copy;' (and also have extra spaces in
it). Therefore, no copy sub has been defined.
This is the script code ...

open (INPUT1, "$ARGV[0]") or die "Cannot open $ARGV[0]";
open (OUTPUT, "> $ARGV[2]");

#print "First input file is: $ARGV[0]\n";
#print "Second input file is: $ARGV[1]\n";
#print "The output file is: $ARGV[2]\n\n";

# This half of the code dumps everything in 'file1' to the output 'file3'
# while ( <INPUT1> ) {
# print "Reading $ARGV[0]\n";
# print OUTPUT $_;
# }

File :: COPY;
copy("$ARGV[0]","$ARGV[2]") or die "Copy failed: $!";

# This half of the code compares 'file2' to 'file1' and writes out
# any line that doesn't match to the output 'file3'

open (INPUT1, "$ARGV[1]") or die "Cannot open $ARGV[1]";

while ( <INPUT1> ) {
$match = 0;

$a = $_;
open (INPUT2, "$ARGV[0]") or die "Cannot open $ARGV[0]";
while ( <INPUT2> ) {
$b = $_;
if ($a eq $b) {
$match = 1;
last;
}
}

if ($match == 0) {
print OUTPUT $a;
}
}

ko said:
The File::Copy module.

use File::Copy;
copy("file1","file2") or die "Copy failed: $!";

'perldoc File::Copy' gives you all the details

HTH - keith
 
M

Michael P. Broida

Bill said:
File :: COPY;
copy("$ARGV[0]","$ARGV[2]") or die "Copy failed: $!";

I don't -think- the first statement above will work.
Try:
use File::Copy;

No extra spaces, no changes to uppercase, and the word
"use" is important. I would also suggest putting THAT
line near the beginning of your entire script, but I'm
not sure it matters beyond style/readability.

If that still doesn't work... read on.

If you know EXACTLY what kind of system it will run on,
(Windows, Linux, etc) you can use whatever command you
would give to a shell or commandprompt.

Use the ` command in perl to run the shell command:

$result = `copy a.txt b.txt`;

NOTE: The characters before "copy" and before the
semi-colon are both backticks (key left of the "one"
key on common US keyboards; lowercase tilde "~").

$result will contain whatever output the shell returned
(error messages, success message, whatever). Probably
nothing if it was successful.

Note that that method is not necessarily "portable".

Mike
 
T

Tad McClellan

Michael P. Broida said:
Use the ` command in perl to run the shell command:

$result = `copy a.txt b.txt`;

$result will contain whatever output the shell returned ^^^^^^^^^^^^^^^^^^^^
(error messages,
^^^^^^^^^^^^^^


bacticks do not capture STDERR.
 
B

Bill

Abigail said:
I actually prefer to shell out to copy files (but not by using backticks,
but by using system), because "cp" copies files in the way I expect, while
File::Copy doesn't. File::Copy doesn't respect execute bits on files,
"cp" does.

Yeah, I have a wrapper around File::Copy that does this. Seems to me
someone should write a rmscopy for Unix for that package.
 
M

Michael P. Broida

Abigail said:
Michael P. Broida (michael.p.broida@boeing_oops.com) wrote on MMMDCLXXXII
September MCMXCIII in <URL:() $result = `copy a.txt b.txt`;
()
() $result will contain whatever output the shell returned
() (error messages, success message, whatever). Probably
() nothing if it was successful.

Eh, no. $result will contain whatever the command wrote to
standard *output*. Error messages typically go to standard *error*
and that's is not collected by backticks - you'd first have to
merge them into the output stream.

Oops, you're exactly correct. I wasn't specific enough.
() Note that that method is not necessarily "portable".

I actually prefer to shell out to copy files (but not by using backticks,
but by using system), because "cp" copies files in the way I expect, while
File::Copy doesn't. File::Copy doesn't respect execute bits on files,
"cp" does.

I agree that using a shell command can get you "better"
results because it uses the "correct" method for that system.
But if you're on Windows, "cp" doesn't do ANYTHING (unless
you have Cygwin or similar installed). That's all I was
referring to when I said it's not portable: you might have
to change it for a different system.

Thanks!
Mike
 
M

Michael P. Broida

Tad said:
^^^^^^^^^^^^^^

bacticks do not capture STDERR.

You're correct. I didn't think it through far
enough when I was writing that post.

Thanks!
Mike
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top