Just because this is a beginners list:

{ local $/;  $entire_file = <FILE>; }

works because (please correct me if I am wrong)

{         # blocks get their own variable scope this is done so that we 
          # can muck with $\ without affecting surrounding code
local $/; # this is easier to see if you write it as local($\) = undef;
          # it sets the record seperator to undef which puts the <FH>
          # operator into a special "slurp" mode.  The local is there to
          # insure that when we leave this block that $\ will get its
          # old value back
$entire_file = <FILE>; 
}         # block ends so $\ gets its old value back

On 02 Jun 2001 00:46:39 -0400, Jeff Pinyan wrote:
> On Jun 2, David Gilden said:
> 
> >Is one of these preferred over the other?
> >
> >        while(<FROMFILE>){
> >        push(@everyline, $_);
> >     }
> >
> >$longstring = join("",@everyline);
> >@oldentries = split(/<!--NEWENTRY-->/,$longstring);
> 
> It's kinda silly to make an array, just to join the elements together
> later.  Use a string.
> 
> >open(FROMFILE,$filename);
> >
> >    while (<FROMFILE>){
> >   $longstring .=  $_;
> >}
> >
> >@entries = split ('<!--NEWENTRY-->', $longstring);
> 
> Even better, you can slurp the ENTIRE file into a string all at once:
> 
>   { local $/;  $entire_file = <FILE>; }
> 
> >@entries = split (/<!--NEWENTRY-->/, $longstring);
> 
> The first argument to split() *is* a regex, and NOT just a string.  You
> should use the /.../ notation whenever possible (unless you're doing the
> magical split on ' ').
> 
> But here's an even cooler solution to your problem.
> 
>   # define a "record" as that which ends in "<!--NEWENTRY-->"
>   $/ = "<!--NEWENTRY-->";
> 
>   while (<FILE>) {
>     chomp;  # remove "<!--NEWENTRY-->" from the end
>     # deal with $_ as you wish
>   }
> 
> -- 
> Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
> Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
> Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
> Acacia Fraternity, Rensselaer Chapter.         Brother #734
> **      Manning Publications, Co, is publishing my Perl Regex book      **

-- 
Today is Pungenday, the 7th day of Confusion in the YOLD 3167
You are what you see.


Reply via email to