MESSAGE
DATE | 2011-12-14 |
FROM | Ruben Safir
|
SUBJECT | Re: [NYLXS - HANGOUT] A simple perl script
|
On 12/14/2011 07:27 PM, Elfen Magix wrote: > I need a simple thing that goes through a file to find "Chapter [and a number]". Then it must increment that number by 1 and resave the file without altering nothing else. > > I know I've seen this, and even wrote one years ago myself, but I cant for the life of me remember how! > > Thanks. > How can I copy a file? (contributed by brian d foy)
Use the "File::Copy" module. It comes with Perl and can do a true copy across file systems, and it does its magic in a portable fashion.
use File::Copy;
copy( $original, $new_copy ) or die "Copy failed: $!";
If you can't use "File::Copy", you'll have to do the work yourself: open the original file, open the destination file, then print to the destination file as you read the original. You also have to remember to copy the permissions, owner, and group to the new file.
perlfaq5
How do I traverse a directory tree? (contributed by brian d foy)
The "File::Find" module, which comes with Perl, does all of the hard work to traverse a directory structure. It comes with Perl. You simply call the "find" subroutine with a callback subroutine and the directories you want to traverse:
use File::Find;
find( \&wanted, -at-directories );
sub wanted { # full path in $File::Find::name # just filename in $_ ... do whatever you want to do ... }
The "File::Find::Closures", which you can download from CPAN, provides many ready-to-use subroutines that you can use with "File::Find".
The "File::Finder", which you can download from CPAN, can help you create the callback subroutine using something closer to the syntax of the "find" command-line utility:
use File::Find; use File::Finder;
my $deep_dirs = File::Finder->depth->type('d')->ls->exec('rmdir','{}');
find( $deep_dirs->as_options, -at-places );
The "File::Find::Rule" module, which you can download from CPAN, has a similar interface, but does the traversal for you too:
use File::Find::Rule;
my -at-files = File::Find::Rule->file() ->name( '*.pm' ) ->in( -at-INC );
|
|