Chas. Owens wrote:
> On Mon, Mar 31, 2008 at 2:30 PM, Rob Dixon <[EMAIL PROTECTED]> wrote:
> snip
>>  >     my $sepchar = ',';
>>  >     for (@_) { $sepchar = ";" and last if /\Q$sepchar/ }
>>
>>  This relies on ';' being true, and uses 'and' in void context.
>>
> snip
> 
> There is nothing wrong with using and in void context.  If you have a
> problem with it then you have a problem with many Perl idioms such as
> 
> open my $fh, "<", $file
>     or die "could not open $file: $!";
> 
> This uses or in void context.  The use of and and or to control flow
> is a long standing feature of the language.  Perhaps you are confusing
> the use of grep and map in a void context being bad.  They are bad in
> a void context because they are inefficient compared to the equivalent
> for loops in some versions of Perl (prior to 5.8.1)  and they have no
> benefits in comparison with the for loop (it isn't shorter or more
> clear).

No there is no confusion, and I also have a problem with your example
with open: 'and' and 'or' are operators and return values that should be
used. It makes just as much sense to write

  ($sepchar = ';') + last if /\Q$sepchar/

Having said that I always use that idiom with open() because IMO the
alternative using 'if' is more clumsy and it's the best way to do that
in Perl. It also reads properly as English.

OTOH

  $sepchar = ';' and last

also reads as English but doesn't do what it says it does. I would go
with the comma operator though:

  $sepchar = ';', last

is fine.

> snip
>>   $sepchar = '' and last if ...
>>
>>  wouldn't work; or, rather, it would work but wouldn't exit from the loop
>>  when intended.
> snip
> 
> Look at the intent of the piece of code again.  Having an empty string
> for a separator would be meaningless (and would break the regex).  A
> better example would be "0", but even that is pushing it since zero is
> not a traditional separator character.

My misgivings were precisely that the construct would work only in that
context. Somebody could be forgiven for thinking that, based on the
advice, something like

  $idx = 0 and $string = '' and next;

would also work.

Rob

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to