[ABE.pm] review of last night's hacking

Ricardo SIGNES perl.abe at rjbs.manxome.org
Fri May 9 07:18:20 PDT 2008


* "Faber J. Fedor" <faber at linuxnj.com> [2008-05-09T09:38:23]
> On 08/05/08 22:35 -0400, Ricardo SIGNES wrote:
> > > Actually, I'm more interested in some of your programming techniques,
> > > like the *_PATTERNS/dispatch table trick.  I want to see what else
> > > you've got hidden in there.
> > 
> >   http://rjbs.manxome.org/rubric/entry/1564
> 
> Not the named capture stuff, this stuff:
> 
>     my ($pattern, $method) = @MSG_PATTERNS[ $i, $i + 1 ];
>     next unless $msg =~ /\A$pattern\z/;
>     my $result = $self->$method({%+});

That is the named capture stuff.

> I never thought to A) put a regex in the first index(?) of an array and
> B) I didn't know you could use a variable name as a method call like that.

You can put any scalar in any index of an array.  Don't get confused with
hashes, which only use strings as keys.

  @foo = ($object, $object, $object);
  %foo = ($object, $object, $object);

The first one stores an array containing three references to the object.

The second stores the same thing as:

  %foo = ("$object", $object, "$object", undef);

...and raises a warning about an odd number of items being assigned to a hash,
I think.

Using a scalar as a method name is often unknown or overlooked, but it's
incredibly powerful.  It means that objects can be easily used as a dispatch
table without most of the insanity of "using a string as a subroutine name."
(That's because of the difference between function and method dispatch, which
is pretty boring.)

> Where does '{%+}' come from?  Is that the Moose stuff?

You might want to go back and read the entry I linked to, taking it on faith
that it's about this.  Here's an section from it:

  The important points are:

   1. the regex pattern with named captures like: (?<x>-?\d+)
   2. the method call that passes in { %+ }

  See, if the pattern matches, all the named captures end up in the magic
  variable %+. That

You can read about %+ in the "perlvar" perldoc page, as long as you're running
5.10.

-- 
rjbs


More information about the ABE-pm mailing list