Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Passing arguments to WWW::Mechanize::Firefox constructor with Moose

by kcott (Archbishop)
on Mar 11, 2016 at 14:50 UTC ( [id://1157440]=note: print w/replies, xml ) Need Help??


in reply to Re: Passing arguments to WWW::Mechanize::Firefox constructor with Moose
in thread Passing arguments to WWW::Mechanize::Firefox constructor with Moose

G'day nysus,

"OK, I just learned that when extending non-Moose modules, you should use the MooseX::NonMoose package."

I did this a few years back; these two articles were extremely helpful:

  1. Subclassing Tricky Non-Moose Classes: Don't Do It
  2. Subclassing Tricky Non-Moose Classes: Constructor Problems

I used the information in those blogs to code Anomaly (a personal, unpublished module), which extends Exception::Class::Base. This module is complete and working; it has examples of code that will cover much of what you might need, such as BUILDARGS & FOREIGNBUILDARGS, and uses MooseX::ClassAttribute.

package Anomaly v0.0.1; use 5.12.0; use Moose; use MooseX::ClassAttribute; use MooseX::NonMoose; extends qw(Exception::Class::Base); use namespace::autoclean; class_has generic_description => ( is => q{ro}, isa => q{Str}, default => q{Generic Anomaly}, ); around BUILDARGS => sub { my ($rc_constructor, $proto, @args) = @_; my $class = ref $proto || $proto; if (@args % 2) { push @args, q{message}; } return $class->$rc_constructor(@args); }; sub FOREIGNBUILDARGS { my ($proto, @args) = @_; my $class = ref $proto || $proto; my %wanted = (); if (@args % 2) { $wanted{message} = shift @args; } my %remaining = (@args); my %translation = (msg => q{message}); for my $key ($class->meta->get_attribute_list()) { if (exists $remaining{$key}) { if (exists $translation{$key}) { $wanted{$translation{$key}} = $remaining{$key}; } delete $remaining{$key}; } } if (exists $remaining{error}) { if (! exists $wanted{message}) { $wanted{message} = $remaining{error}; } delete $remaining{error}; } return (%wanted, %remaining); } has description => ( is => q{rw}, isa => q{Str}, default => sub { __PACKAGE__->generic_description() }, ); has detail => ( is => q{ro}, isa => q{Str}, default => q{}, ); has additional_info => ( is => q{ro}, isa => q{ArrayRef[HashRef]}, default => sub { [] }, lazy => 1, required => 0, ); sub add_additional_info { my ($self, $rh_new_info) = @_; my $ra_current_info = $self->additional_info(); push @$ra_current_info, $rh_new_info; $self->additional_info($ra_current_info); return; } no Exception::Class::Base; no MooseX::NonMoose; no MooseX::ClassAttribute; no Moose; __PACKAGE__->meta->make_immutable; 1;

— Ken

Replies are listed 'Best First'.
Re^3: Passing arguments to WWW::Mechanize::Firefox constructor with Moose
by nysus (Parson) on Mar 11, 2016 at 15:16 UTC

    OK, wow, thanks for the links and sample code. I will definitely study these closely.

    While I have you ear right now, what if I want to override a method in the parent class (WWW::Firefox::Mechanize)? Is delegating, as shown in your first link, still the best option? Here's some code pseudocode to try to convey what I mean:

    use WWW::Mechanize::Firefox; has 'bot' => ( is => 'ro', isa => 'WWW::Mechanize::Firefox', ); sub get { my ($self, $url) = @_; $self->bot->get($url, synchronize => 1); }

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon";
    $nysus = $PM . $MCF;
    Click here if you love Perl Monks

      "OK, wow, thanks for the links and sample code."

      You're welcome.

      "I will definitely study these closely."

      I encourage you to spend a little more time, improving your foundation Moose skills and knowledge, before delving into MooseX-land. I suggest you read the Moose manpage: it's not overly long and has many links to cookbook examples, more detailed information, and so on: follow as requirement, or curiosity, take you.

      "I'm a newb with Moose."

      See Moose manpage: New to Moose?.

      "if I want to override a method in the parent class ..."

      See Moose manpage: override ($name, &sub).

      — Ken

        I always a struggle to determine when I've learned enough to try to cut my teeth on writing real code. I wish I were the type that could just read through documentation and grok it immediately but often my eyes just glaze over as I try to figure out whether what I'm reading can be applied to the specific task I want to accomplish.

        So your timely advice gives me practical guidance I can chew on while I search for a good solution. I appreciate it.

        $PM = "Perl Monk's";
        $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon";
        $nysus = $PM . $MCF;
        Click here if you love Perl Monks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1157440]
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: (4)
As of 2024-04-23 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found