Brushing up on Perl?

Paul Seamons paul at seamons.com
Thu Jan 10 10:47:26 MST 2008


Wow - what a horribly cryptic chunk of code.  I hope there are some comments 
near it saying what will happen.

The short of it is the && operator short circuits.  If the first operand is 
false it will return a false value.  If the first operand is true it will 
return the second operand if it is true, false otherwise.  Essentially, this 
would only affect the key named "0" and its value would be false.

Often it is easier to see what is going on by dumping the resulting data:

use Data::Dumper qw(Dumper);
local $Data::Dumper::Sortkeys = 1;
print Dumper \%x;

Prints

$VAR1 = {
          '-1' => '90%',
          '-2' => '80%',
          '-3' => '70%',
          '-4' => '60%',
          '-5' => '50%',
          '-6' => '40%',
          '-7' => '30%',
          '-8' => '20%',
          '-9' => '10%',
          '0' => 0,
          '1' => '200%',
          '2' => '300%',
          '3' => '400%',
          '4' => '500%',
          '5' => '600%',
          '6' => '700%',
          '7' => '800%',
          '8' => '900%',
          '9' => '1000%'
        };

Paul

On Thursday 10 January 2008, Steve wrote:
> Hi everyone,
>
> After my last debate on whether or not Perl was worth spending time to
> sharpen my skills on.
> I decided to start strengthing my neglected skills in this language.
>
> I am presently working through some example code and am quite stuck on
> a line of code and since there are no answers at the back of the book,
> I'm not quite sure what this line is doing.
>
> 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
> );
>
> 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.
>
> Any ideas guys?
>
> Thanks for the helps!
>
> Sincerely,
> Steve
>
> /*
> PLUG: http://plug.org, #utah on irc.freenode.net
> Unsubscribe: http://plug.org/mailman/options/plug
> Don't fear the penguin.
> */



More information about the PLUG mailing list