Just another perl hacker somewhere near Disneyland
I have this homenode [perlmonks.org] of little consequence on Perl Monks [perlmonks.org] that you probably have no interest in whatsoever.
I also have some modules [cpan.org] on CPAN [cpan.org] some of which are marginally [cpan.org] more [cpan.org] useful [cpan.org] than others.
$doc->orders->order->order_id->es_value(100)
I didn't ask at the time what to do about namespaces (since perl doesn't allow ':' as part of a bareword, but does allow it in a dynamic method call where the method name is a variable), but through the magic of source filtering, I came up with this to allow something like $doc->foo:bar (of course, allowing this does break certain other constructs, so just don't do those):
package MethodFilter;
use Filter::Util::Call;
sub import {
filter_add(
sub {
s/(?<=->)((\w+):(\w+))/
$meth = __PACKAGE__ . "::_${2}_${3}";
$$meth = $1;
"\$$meth";
/eg if ($status = filter_read()) > 0;
$status;
}
);
}
1;
I'm sure you could even do this with Filter::Simple, but this seems simple enough. And as for the lack of 'use strict', it started out that way, but it adds too much clutter for such a short bit of code...
(Hey, I finally got around to making an entry!)
Update: Simplified regex from autarch's reply. Used to be:
s/(?<=->)((\w+):(\w+))
(?{
$tmp = __PACKAGE__ . "::_${2}_${3}";
$$tmp = $1;
})/\$$tmp/gx
Wacky regex (Score:2)
Why use such a strange regex (specifically (?{...}) )?
What's wrong with just doing the work in the right side of the substitution with /e?
or even better, move it out of the substitution altogether:
Re:Wacky regex (Score:1)
Re:Wacky regex (Score:1)