MESSAGE
DATE | 2011-12-14 |
FROM | Ruben Safir
|
SUBJECT | Re: [NYLXS - HANGOUT] A simple perl script
|
From owner-hangout-outgoing-at-mrbrklyn.com Wed Dec 14 22:45:09 2011 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: by www2.mrbrklyn.com (Postfix) id 2298EC9C3F; Wed, 14 Dec 2011 22:45:09 -0500 (EST) Delivered-To: hangout-outgoing-at-www2.mrbrklyn.com Received: by www2.mrbrklyn.com (Postfix, from userid 28) id 11A33FF99F; Wed, 14 Dec 2011 22:45:08 -0500 (EST) Delivered-To: hangout-at-mrbrklyn.com Received: from mail-qy0-f172.google.com (mail-qy0-f172.google.com [209.85.216.172]) by www2.mrbrklyn.com (Postfix) with ESMTP id 1EECEC9C3F for ; Wed, 14 Dec 2011 22:45:07 -0500 (EST) Received: by qcsf15 with SMTP id f15so1159017qcs.17 for ; Wed, 14 Dec 2011 19:46:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=Af6nGW/VEczFnXPfFCbTHwlt25e1r1xs9OLsG7Y8Myg=; b=gWMFqkbftPyjPik5rPETvtNbtGujTF+3WO3BDWkDMK9PqgSK7eithNACMAapKadKnu RqVigsFm/9b/bG1MBvFVmdz3A61ACKI7GqI3aHPozjox2CSOzUJX25TgUA0gOTgLXj4G w4pkJ4JZ8E6IgPSRhC8gc/HdQn1QI5Gio5NVQ= Received: by 10.224.199.134 with SMTP id es6mr2793518qab.2.1323920807536; Wed, 14 Dec 2011 19:46:47 -0800 (PST) Received: from [10.0.0.24] (www2.mrbrklyn.com. [96.57.23.82]) by mx.google.com with ESMTPS id ed2sm10089860qab.15.2011.12.14.19.46.46 (version=SSLv3 cipher=OTHER); Wed, 14 Dec 2011 19:46:46 -0800 (PST) Message-ID: <4EE96DA4.2050105-at-gmail.com> Date: Wed, 14 Dec 2011 22:46:44 -0500 From: Ruben Safir User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111101 SUSE/3.1.16 Thunderbird/3.1.16 MIME-Version: 1.0 To: hangout-at-mrbrklyn.com CC: Elfen Magix Subject: Re: [NYLXS - HANGOUT] A simple perl script References: <20111214070833.GA11058-at-panix.com> <1323908859.93530.YahooMailNeo-at-web38006.mail.mud.yahoo.com> In-Reply-To: <1323908859.93530.YahooMailNeo-at-web38006.mail.mud.yahoo.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Sender: owner-hangout-at-mrbrklyn.com Precedence: bulk Reply-To: hangout-at-mrbrklyn.com
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 );
|
|