Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: How to Put a Loop in my 1st Program

by sifukurt (Hermit)
on Nov 21, 2001 at 19:42 UTC ( #126781=note: print w/replies, xml ) Need Help??

in reply to How to Put a Loop in my 1st Program

I tried to follow your methodology as closely as possible. There are easier and/or more efficient ways of doing what you're trying to do, but personally, I'd worry about style after you're a little more comfortable with the language as a whole. Not to discount style, mind you, but as with a spoken language, you don't worry about writing a sonnet or a haiku until you've learned how to write a sentence or paragraph.
#!/usr/local/bin/perl use strict; use vars qw( $addthis $tothis $newrecord ); open (TEXT, "general.txt") || die "Couldn't open file: $|\n"; $addthis = <TEXT>; close (TEXT); undef $/; open (FILES,"files.txt") || die "Could not open file: $! \n"; while ( <FILES> ) { open ( DATA, $_ ) || die "Could not open file: $|\n"; $tothis = <DATA>; close (DATA); $newrecord = $addthis . $tothis; open ( DATA, "> $_" ) || die "Could not write to file: $|\n"; print $_, "\n"; print DATA $newrecord; close (DATA); } close (FILES);
Since you're treating all of the files as individual strings, you can do the undef $/; business. $/ is the input record separator, and since we aren't limiting the scope (i.e., putting undef $/; inside a subroutine, for example), it is a global change and will apply to all subsequently read files. And in this case, I think a 'while' loop is one of the easier ways to setup the looping. I don't want this to sound like a shameless plug, but I wrote the module File::Butler, and it was specifically designed to handle this sort of thing. And since File::Butler does all of the grunt work for you, you don't need to worry about most of it. Using File::Butler, you could re-write the above as this:
#!/usr/local/bin/perl use strict; use File::Butler; use vars qw( @newstuff @files ); Butler( "general.txt", "read", \@newstuff ); Butler( "files.txt", "read", \@files ); foreach my $file ( @files ) { Butler( $file, "prepend", \@newstuff ); }
I hope this helps. Welcome to the Perl Fraternity, btw. We'll teach you the secret handshake later. :-)

Update: Pursuant to discussion with tilly on the use of our vs. use vars, I've modified the above code to use vars rather than our as I had done originally. I wasn't aware that our differed so much in theory and practice.

Update2: Eeeeeek! Hofmator is absolutely correct. I had undef $/; in the wrong place. Thanks for catching that, Hofmator. I moved it down a few lines, and it will work correctly now. I'm now performing undef $/; after we've read in "files.txt".


Replies are listed 'Best First'.
Re: Re: How to Put a Loop in my 1st Program
by Hofmator (Curate) on Nov 23, 2001 at 00:44 UTC

    Since you're treating all of the files as individual strings, you can do the undef $/; business.

    I don't think so, you want to read the filenames stored in 'files.txt' line by line - which your solution doesn't do. So I'd go for something like this:

    open(FH, 'filename') or die $!; my $text = do {local $/; <FH>}; close FH;

    For a complete treatmeant of many possibilities and discussions on how to slurp a file read the very informative Slurp a file.

    -- Hofmator

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://126781]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (1)
As of 2023-06-04 03:24 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (17 votes). Check out past polls.