[Wellington-pm] Strange (for me) notation

Grant McLean grant at mclean.net.nz
Tue Jul 21 16:23:18 PDT 2009


Hi Chris!

It's been so quiet here without you :-)

On Wed, 2009-07-22 at 11:10 +1200, Chris Eade wrote:
> ... this particular piece of code has me really stumped. It looks
> like this:
> 
>   use constant LATEST_D_ACROSS_PRODUCTS => 'P';
>   <snip>
>   $ib->{reg}->{@{[LATEST_D_ACROSS_PRODUCTS]}} = $ed;
>   $ib->{prodNo}->{@{[LATEST_D_ACROSS_PRODUCTS]}} .= "$product_no,";

The problem is if they just did ...

  $ib->{reg}->{LATEST_D_ACROSS_PRODUCTS} = $ed;

Then LATEST_D_ACROSS_PRODUCTS would be interpreted as the string
'LATEST_D_ACROSS_PRODUCTS'.

> 
> The part I really don't get is the meaning of this:
> 
>   @{[LATEST_D_ACROSS_PRODUCTS]}

Yeah that's pretty awful.  Putting [ .. ] around the constant creates an
arrayref, putting @{ .. } around that dereferences the array ref back to
a list of all its values.

Since a Perl constant is really a subroutine (that gets optimised away),
alternative approaches are:

  $ib->{reg}->{LATEST_D_ACROSS_PRODUCTS()} = $ed;

or:

  $ib->{reg}->{&LATEST_D_ACROSS_PRODUCTS} = $ed;

Cheers
Grant




More information about the Wellington-pm mailing list