Date: Fri, 1 Feb 2002 12:47:14 -0700 From: z thompson <cublai@lastamericanempire.com> To: "F. Xavier Noria" <fxn@isoco.com> Cc: freebsd-questions@freebsd.org Subject: Re: Perl question... Message-ID: <20020201124714.A79890@titus.lastamericanempire.com> In-Reply-To: <20020201192621.348fcfe4.fxn@isoco.com>; from fxn@isoco.com on Fri, Feb 01, 2002 at 07:26:21PM %2B0100 References: <DC32C8CEB3F8D311B6B5009027DE5AD503D207F8@stlmail.dra.com> <20020201095025.A79152@titus.lastamericanempire.com> <20020201192621.348fcfe4.fxn@isoco.com>
next in thread | previous in thread | raw e-mail | index | archive | help
* F. Xavier Noria <fxn@isoco.com> [020201 11:32]: > On Fri, 1 Feb 2002 09:50:25 -0700 > z thompson <cublai@lastamericanempire.com> wrote: > > : * Eric Six <erics@sirsi.com> [020201 09:00]: > > : > I have about 400 primary and 300 secondary DNS records that I have migrated > : > from a bind4 server. I need to add a '$TTL value;' to the first line of all > : > my zone files... > : > > : > I have found ways to append lines to the file, but not to create a new one > : > at the very beginning. Also, any ideas on how to automate doing this to all > : > the files in each dir? > : > > : > : With temporary files... > : > : my $old_file = 'some_file'; # original file > : my $tmp_file = '>some_file.tmp'; # a temp file > : my $new_line = '$TTL value;'; # stuff to add to file > > <snipped solution> > > Recursion in a directory tree is somewhat tricky. It is generally > accepted that the best (and portable) way to accomplish that kind of > work in Perl is to delegate the recursion to the standard module > File::Find like this: > > # untested > use File::Find; > > my $header = 'append this header to the top'; > > find(\&callback, '/root/dir/one', '/root/dir/two'); > > sub callback { > return unless /some filter regexp/; > open FILE, $_ or die $!; > my $contents; > {local $/; $contents = <FILE>;} > close FILE; > open FILE, ">$_" or die $!; > print FILE $header, "\n"; > print FILE $contents; > close FILE; > } Indeed, there are standard ways to traverse a directory structure in Perl. However, I didn't see anything specifically about recursion which is why I said for "a small number of directories." File::Find is somewhat overkill in my opinion for 2 or 3 dirs with no subdirs. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020201124714.A79890>