[comp.lang.perl] File copy operation in Perl

rboudrie@xenna.encore.com (Rob Boudrie) (09/25/90)

I am writing a perl utiltiy which will need to execute various file copy
applications.  My options appear to be limited to :

-> Open the file from file as well as the to file and do a read/write
   pairs in a loop (clumsy!)

-> create a temporary script file and invoke it with "exec"

Does anyone know of a way for Perl to do a fiel copy operation and return
to the perl application?  I tired using exec("cp fromfile tofile");, but the
flow did not return to perl after executing the copy command.

Please mail me any suggestions you may have.
Thanks,
Rob Boudrie
rboudrie@encore.com
Robert Boudrie        rboudrie@encore.com       Encore Computer, Marlborough MA

merlyn@iwarp.intel.com (Randal Schwartz) (09/26/90)

In article <12796@encore.Encore.COM>, rboudrie@xenna (Rob Boudrie) writes:
| I am writing a perl utiltiy which will need to execute various file copy
| applications.  My options appear to be limited to :
| 
| -> Open the file from file as well as the to file and do a read/write
|    pairs in a loop (clumsy!)
| 
| -> create a temporary script file and invoke it with "exec"
| 
| Does anyone know of a way for Perl to do a fiel copy operation and return
| to the perl application?  I tired using exec("cp fromfile tofile");, but the
| flow did not return to perl after executing the copy command.

Not exec (which disappears your Perl interpreter)... you want "system".

	system "cp fromfile tofile";

will work.  There's also:

	open(F,"fromfile") || die "fromfile: $!";
	open(T,">tofile") || die "tofile: $!";
	while (read(F,$buf,8192)) {
		print T $buf;
	}
	close(F); close(T);

Why do you call this clumsy?  If you don't like it, hide it in a
subroutine.

	sub copyfile {
		local(*F,*T,$buf);
		($F,$T) = @_;
		open(F) || die "$F: $!";
		open(T,">$T") || die "$T: $!";
		while (read(F,$buf,8192)) {
			print T $buf;
		}
		close(F);
		close(T);
	}

	&copyfile("fromfile","tofile");

(Yeah, it's bad practice to die in a sub, but you get the idea.)

print "Just another Perl [book] hacker,"
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/