Re: commify_series script in cookbook page 94



On Mon, Mar 31, 2008 at 2:30 PM, Rob Dixon <rob.dixon@xxxxxxx> 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).

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.

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
.



Relevant Pages

  • Re: commify_series script in cookbook page 94
    ... This relies on ';' being true, and uses 'and' in void context. ... problem with it then you have a problem with many Perl idioms such as ... or, rather, it would work but wouldn't exit from the loop ...
    (perl.beginners)
  • Re: Whats being thrown away?
    ... Mathew Snyder wrote: ... variable in a void context" error. ... It is at line 64 which is the closing brace ... But it doesn't look like you are using $count inside the loop anyway so why ...
    (perl.beginners)
  • Re: Scalar variable in void context before a loop
    ... variable to be used as an iterator in void context before the loop. ... I am curious as to what reasons there are for doing this, ...
    (comp.lang.perl.misc)
  • Re: Scalar variable in void context before a loop
    ... markhobley@xxxxxxxxxxxxxxxxxxxxxxxxxxx (Mark Hobley) wrote: ... variable to be used as an iterator in void context before the loop. ...
    (comp.lang.perl.misc)
  • Scalar variable in void context before a loop
    ... In my professional perl programming guide, some of the examples put the ... variable to be used as an iterator in void context before the loop. ...
    (comp.lang.perl.misc)