[Wellington-pm] Strange (for me) notation

Daniel Pittman daniel at rimspace.net
Tue Jul 21 17:19:55 PDT 2009


Chris Eade <chris.eade at gmail.com> writes:

> My name is Chris Eade - Long time listener, first time caller. Well, not so
> much the long time listener :)
>
> I am working as a developer on a suite of web applications written in
> Perl. Its basically maintaining legacy code, and there are always bits of
> code that make me stop and think: what the..?!

Grant has answered the question quite well, but a tool that might help y'all
in future investigating this sort of thing:

> However, this particular piece of code has me really stumped. It looks
> like this:
>
>   use constant LATEST_D_ACROSS_PRODUCTS => 'P';
>   $ib->{reg}->{@{[LATEST_D_ACROSS_PRODUCTS]}} = $ed;

I rewrote this bit into a Perl script as follows:

    use constant LATEST_D_ACROSS_PRODUCTS => 'P';
    $ib->{reg}->{@{[LATEST_D_ACROSS_PRODUCTS]}} = $ed;
    $ib->{reg}->{LATEST_D_ACROSS_PRODUCTS()} = $ed;
    $ib->{reg}->{&LATEST_D_ACROSS_PRODUCTS} = $ed;
    $ib->{reg}->{LATEST_D_ACROSS_PRODUCTS} = $ed;

Then, using B::Deparse, you can see the effect of the various presentations:

] perl -MO=Deparse example.pl
sub LATEST_D_ACROSS_PRODUCTS () { 'P' }
use constant ('LATEST_D_ACROSS_PRODUCTS', 'P');
$$ib{'reg'}{@{['P'];}} = $ed;
$$ib{'reg'}{'P'} = $ed;
$$ib{'reg'}{&LATEST_D_ACROSS_PRODUCTS} = $ed;
$$ib{'reg'}{'LATEST_D_ACROSS_PRODUCTS'} = $ed;
example.pl syntax OK

That shows you what Perl is actually doing at each step along the way, and how
it interprets the code.  Oh, and which of the options is the fastest because
it allows the optimizer to actually eliminate the function call. ;)


Anyway, B::Deparse can eliminate some of the syntax fluff around statements
and tell you what Perl actually /does/ rather than just what it looks like...

Regards,
        Daniel
-- 
✣ Daniel Pittman            ✉ daniel at rimspace.net            ☎ +61 401 155 707
               ♽ made with 100 percent post-consumer electrons


More information about the Wellington-pm mailing list