2009/4/17 Chap Harrison <[email protected]>:
> Now that I am learning how to work with complex data structures, I find myself
> writing things like this a lot:
>
> my $foo = ( defined $very_long_expression ? $very_long_expression : "n/a" );
>
> or
>
> my $foo = ( $very_long_expression > 0 ? $very_long_expression : 0 );
>
> (Where the long expression typically involves dereferencing hashes of arrays
> of hashes.)
>
> At some point I thought I read about a shorter way to write this, that did
> not involve repeating the $very_long_expression in the same statement.
> Perhaps a special variable or something, acting sort of like a pronoun.
> Anyone know of a shortcut?
snip
Why not
my $foo = $very_long_expression;
$foo = "n/a" unless defined $foo;
or
my $foo = $very_long_expression;
$foo = 0 unless $foo > 0;
If you are using Perl 5.10 you can say
my $foo = $very_long_expression // "n/a";
for the first case.
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/