Brushing up on Perl?

Doran L. Barton fozz at iodynamics.com
Thu Jan 10 10:50:33 MST 2008


Man, this is exactly what people mean when they say Perl is unreadable. It
doesn't have to be. Any self-respecting Perl developer, especially one who
has read Perl Best Practices by Damian Conway, would never write this kind
of code.

Not long ago, Steve proclaimed...
> The line I'm stuck on is this...
> ($_, $_ && "$l%")
> 
> The code in it's context is this...
> my %x =
> (
> 	map {
> 		 my $l = $_ < 0
> 		 ? (($_ + 10) * 10)
> 		 : (($_ + 1) * 100);
> 
> 	 	($_, $_ && "$l%")
> 	} -9 .. 9
> );

It's creating a name-value pair for each number in the map input sequence
(-9 to 9). The && basically says if the left argument is false, then the
right argument isn't even evaluaed. 

> Now I see that it's creating a hash.
> It does so by counting in a loop fron -9 to +9
> It first checks to see if $_ (Some number between -9 and +9) is < 0
> If it is then $l = ($_+10)*10 else $l = ($_+1)*100
> But the next line has me baffled.
> I see that it's creating a hash with the 2 values, the first is of
> course the number in question, but that second value has got me
> stumped.
> Is it really saying the second value is the modulus of number in
> question and $l ?  Or is it something else?  I've never seen && used
> in this way before.

The % is a literal % inside a string. The value side of the hash values
essentially evaluates to a string except when the sequence input value ($_)
is 0. 

Run this right after that code:

    use Data::Dumper;
    print Dumper(\%x);

-=Fozz

-- 
fozz at iodynamics.com is Doran L. "Fozz" Barton
 "We are ecologically minded. This package will self-destruct in Mother
  Earth."
    -- Seen on a Japan package
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://plug.org/pipermail/plug/attachments/20080110/ab5079a7/attachment.bin 


More information about the PLUG mailing list