Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Better syntax for dynamic method on internal object ?

by Your Mother (Archbishop)
on Sep 23, 2015 at 02:18 UTC ( [id://1142785]=note: print w/replies, xml ) Need Help??


in reply to Better syntax for dynamic method on internal object ?

How about this? :P Time for terse overkill!

sub do_something_with_a_uri { my $self = shift; my $uri = shift || die "uri is required"; my $method = $self->method; # You have to work out how to keep the JSON/Env stuff. $self->$method($uri); }

And here’s the full working demo code–

my @methods = qw/ get post response /; # Used by Env/GlipGlop. package Env { use Moo; has "method" => is => "rwp", isa => sub { grep $_ eq $_[0], @methods or die "No such method: $_[0]"; }; }; package GlipGlop { use Moo; use Scalar::Util "blessed"; has agent => is => "lazy", handles => \@methods, isa => sub { @methods == grep $_[0]->can($_), @methods or die "ua must be able to ", join ", ", @methods; }; sub _build_agent { require WWW::Mechanize; WWW::Mechanize->new( autocheck => 0 ); } has env => is => "rwp", isa => sub { blessed $_[0] eq "Env" }, handles => [ "method" ], default => sub { no warnings "uninitialized"; Env->new({ method => lc $ENV{REQUEST_METHOD} || "get" }); }; sub do_something_with_a_uri { my $self = shift; my $uri = shift || die "uri is required"; my $method = $self->method; # "env" handles this. # You have to work out how to keep the JSON/Env stuff. $self->$method($uri); # the "ua" handles the @methods. } }; use strictures; my $gg = GlipGlop->new; my $uri = shift || "http://example.org"; my $response = $gg->do_something_with_a_uri($uri); my ( $title ) = $response->content =~ m{<title[^>]*>([^<]+)}i; print $title, $/; __END__ moo@cow~>perl pm-1142751 Example Domain moo@cow~>perl pm-1142751 "http://perlmonks.org/?node_id=1142751" Better syntax for dynamic method on internal object ?

Replies are listed 'Best First'.
Re^2: Better syntax for dynamic method on internal object ?
by Anonymous Monk on Sep 23, 2015 at 23:29 UTC

      Oh… say… I always assumed title was a Mech provided method. I did not know it was in the underlying LWP stuff. I knew it existed but left it out of the example because of that. Good call-out. :P

Re^2: Better syntax for dynamic method on internal object ?
by NetWallah (Canon) on Sep 24, 2015 at 02:56 UTC
    Thanks for that extravaganza.

    I was trying to keep dependencies to a minimum - this needs to run on a back-level perl (<= 5.8 vintage), in a distributed set of nodes that probably will not have moose.

    Thanks for the entertainment.

            Software efficiency halves every 18 months, thus compensating for Moore's Law.

      Sure, sure. Moo is not Moose and the usual even you can use the CPAN style rebukes apply. :P Not that I was intending that code to be serious. I do think the code you posted could be organized in a better way. The Moo style points to it but you know the code base so you’ll have to judge what measure is reasonable to apply.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1142785]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-16 14:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found