Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re^2: do {$i++} until vs. $i++ until

by hipowls (Curate)
on Mar 25, 2008 at 07:32 UTC ( #676066=note: print w/replies, xml ) Need Help??

in reply to Re: do {$i++} until vs. $i++ until
in thread do {$i++} until vs. $i++ until

The disadvantage of that idiom is that the scope of $line must extend outside the block where is it used. Declaring $line inside the block results in a syntax error

use strict; do { my $line = <STDIN>; ... } until $line eq ".\n"; # Syntax error __END__ Global symbol "$line" requires explicit package name
unlike a while(condition) {} the condition is not in the same scope as the block. It can be written as
use strict; INPUT: while ( my $line = <STDIN> ) { last INPUT if $line eq ".\n"; ... }
which will restrict $line to the while block

Update: Code updated

Replies are listed 'Best First'.
Re^3: do {$i++} until vs. $i++ until
by ikegami (Pope) on Mar 25, 2008 at 08:36 UTC

    Those two snippets aren't equivalent. Aside from the fact that the latter also exits if <STDIN> returned undef, you reversed the order of the loop body (...) and the exit condition (last INPUT if $line eq ".\n";)

      True they are not functionally equivalent but I didn't care to replicate the bug of looping on a closed file descriptor;-) It works properly when used in a command pipeline.

      As far as last INPUT if $line eq ".\n" goes it is probably correct to put it at the top of the loop since ".\n" is being used as an equivalent to EOF. Whatever is done with ".\n" most likely requires special handling of some sort, for example a sendmail equivalent wouldn't include it in the message and would treat it the same as EOF.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://676066]
[Your Mother]: Heh. I keeping seeing related headlines but my narcissism is a perihelion right now so I'm busy. :P
[LanX]: https://www. news/series/ cambridge- analytica-files
[Your Mother]: s/a /at /;
[LanX]: just watch the video ...
[Your Mother]: Roger, will co.
[Your Mother]: The good news is, they think they are influencing people but Americans at least are irretrievably biased. All that can be accomplished is a little spike in domestic violence. :P

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2018-03-19 13:23 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (240 votes). Check out past polls.