From owner-freebsd-questions Fri Feb 1 11:42:38 2002 Delivered-To: freebsd-questions@freebsd.org Received: from titus.lastamericanempire.com (dsl081-101-239.den1.dsl.speakeasy.net [64.81.101.239]) by hub.freebsd.org (Postfix) with ESMTP id 2442237B41A for ; Fri, 1 Feb 2002 11:41:53 -0800 (PST) Received: by titus.lastamericanempire.com (Postfix, from userid 1001) id 38AF123012; Fri, 1 Feb 2002 12:47:14 -0700 (MST) Date: Fri, 1 Feb 2002 12:47:14 -0700 From: z thompson To: "F. Xavier Noria" Cc: freebsd-questions@freebsd.org Subject: Re: Perl question... Message-ID: <20020201124714.A79890@titus.lastamericanempire.com> References: <20020201095025.A79152@titus.lastamericanempire.com> <20020201192621.348fcfe4.fxn@isoco.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5.1i In-Reply-To: <20020201192621.348fcfe4.fxn@isoco.com>; from fxn@isoco.com on Fri, Feb 01, 2002 at 07:26:21PM +0100 Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG * F. Xavier Noria [020201 11:32]: > On Fri, 1 Feb 2002 09:50:25 -0700 > z thompson wrote: > > : * Eric Six [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 > > > > 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 = ;} > 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