|P is for Practical|
Re: How to Put a Loop in my 1st Programby sifukurt (Hermit)
|on Nov 21, 2001 at 19:42 UTC ( #126781=note: print w/replies, xml )||Need Help??|
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.
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:
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".
In Section Seekers of Perl Wisdom