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".
___________________
Kurt
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.