Thread: Perl
View Single Post
Old 01-31-2006, 11:12 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
redhead is on a distinguished road
How about this:
Code:
#!/usr/bin/perl
use File::Copy;
move($oldfile, $newfile)
    or die "move failed: $!";
OR if you want all the things done by hand, and not use some fancy library function
Code:
#!/usr/bin/perl
open(IN,  "< $oldfile")                     or die "can't open $oldfile: $!";
open(OUT, "> $newfile")                     or die "can't open $newfile: $!";

$blksize = (stat IN)[11] || 16384;          # preferred block size?
while ($len = sysread IN, $buf, $blksize) {
    if (!defined $len) {
        next if $! =~ /^Interrupted/;       # ^Z and fg
        die "System read error: $!\n";
    }
    $offset = 0;
    while ($len) {          # Handle partial writes.
        defined($written = syswrite OUT, $buf, $len, $offset)
            or die "System write error: $!\n";
        $len    -= $written;
        $offset += $written;
    };
}

close(IN);
close(OUT);
unlink($oldfile) or die "Can't unlink $oldfile: $!";
Naturaly you can change the $oldfile/$newfile with whatever you'd parsed as arguments.. I assume you know how to fetch arguments parsed to your program.
Else read up on the Perl cookbook, happy reading
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote