Re: Using regular expressions

From: Gunnar Hjalmarsson (noreply_at_gunnar.cc)
Date: 11/25/04


To: beginners@perl.org
Date: Thu, 25 Nov 2004 01:39:20 +0100


[ Please do not quote the whole message you are responding to, but only
what's needed to give context. Also, please type your reply *below* the
part(s) of the message you are commenting on. ]

Michael Kraus wrote:
> John W. Krahn wrote:
>>
>> You should *always* verify that the files were opened correctly.
>>
>> open INFILE, '<', $filename or die "Cannot open $filename: $!";
>> open OUTFILE, '>', "$filename.$$" or die "Cannot open $filename.$$: $!";
>>
>>> for my $line (<INFILE>) {
>>
>> You imply above that you will read the file "a line at a time" however
>> using a for loop will read the whole file into a list in memory. You
>> need to use a while loop to read a line at a time.
>>
>> while ( my $line = <INFILE> ) {

<snip>

>> You should verify that rename worked correctly
>>
>> rename "$filename.$$", $filename or die "Cannot rename $filename.$$: $!";
>
> The examples I've given are just that examples... Error checking
> functionality is up the end programmer... (And you are quite right, you
> should check the status of operations.) This is a beginers list, let's
> keep it friendly, eh?

Yes, this is a beginner level list, which makes it extra important to
apply good programming practices in posted example code. Pointing out
shortcomings in that respect is not unfriendly, it's useful advice.

> ... and FWIW ...
>
> for my $line (<INFILE>) {
> # stuff
> }
>
> is equivalent to:
>
> while (my $line = <INFILE>) {
> # stuff
> }

No, it's not. John explained why.

Please read up on the topic, e.g. in "perldoc perlsyn", before objecting
again.

> If you are going to correct me, at least get it right and do it in a
> friendly manner! ;) :P :)

???

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


Relevant Pages

  • Re: Power Measurent: Watts Vs. Volts
    ... I would think in context my ... > that to which I am responding, but in this case ... Real newsreaders quote context by default. ...
    (sci.electronics.basics)
  • Re: OT: They said it couldnt be done
    ... Except Robert Allison. ... I've posted his quote several times. ... How did I take this statement out of context? ... I did not stop responding as proved by my post, several hours after the one I'm replying to. ...
    (rec.outdoors.rv-travel)
  • Re: Webtv bullying - Its not just class, its ELITISM
    ... >Since you failed to quote the post you are responding ... your comment has no context. ... Is it your intention ...
    (alt.autos.toyota)
  • Re: RH grows bigger and bigger
    ... Are you responding to something? ... then its useful to quote some ... context. ... and not every newsserver recieves ...
    (comp.os.linux.misc)
  • Re: Webtv bullying - Its not just class, its ELITISM
    ... Since you failed to quote the post you are responding ... your comment has no context. ... Is it your intention ...
    (alt.autos.toyota)