Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Problem with around BUILDARGS

by saintex (Scribe)
on Mar 24, 2011 at 10:56 UTC ( #895225=perlquestion: print w/replies, xml ) Need Help??
saintex has asked for the wisdom of the Perl Monks concerning the following question:

I would like to extends a package in order to have some default values.

The module I would like to extend is WWW::Selenium .

I would like to extend the package in my code, just to have some defualt values.
Ususally you call WWW::Selenium like this:
my $b=Selenium->new( host => "localhost", port => 4444, browser => "*firefox", browser_url => ' +m', );
I would like to call it just with new, without args:
So I tried to write an extension:
package Selenium; our $VERSION=0.01; use 5.010; use Moose; use MooseX::NonMoose; use Test::WWW::Selenium; extends 'Test::WWW::Selenium'; around BUILDARGS => sub { my $orig = shift; my $class = shift; my %args = ( @_ == 1 ? %{ $_[0] } : @_ ); $args{host}//="localhost"; $args{port}//=4444; $args{browser}//="*firefox"; $args{browser_url}//=''; return $class->$orig(%args); }; sub BUILD { my $self = shift; #$self->start; }

But it doesn't work. Without arguments I have an error.
Where I'm wrong?

Replies are listed 'Best First'.
Re: Problem with around BUILDARGS
by stvn (Monsignor) on Mar 24, 2011 at 13:32 UTC

    With MooseX::NonMoose, you want to use FOREIGNBUILDARGS and it should return the arguments for the extended NonMoose class, this version works.

    sub FOREIGNBUILDARGS { my $class = shift; my %args = ( @_ == 1 ? %{ $_[0] } : @_ ); $args{host}//="localhost"; $args{port}//=4444; $args{browser}//="*firefox"; $args{browser_url}//=''; return %args; }

      Thank you very much!
      It works,
      but how can I use some class proprieties inside FOREIGNBUILDARGS ?
      for example, If I try:
      has 'serverLocation' => (is=>'ro',isa=>'Str',default=>"$Bin/lib/seleni +um-server-standalone-2.0b2.jar"); sub FOREIGNBUILDARGS { my $self = shift; print $self->serverLocation; my %args = ( @_ == 1 ? %{ $_[0] } : @_ ); $args{host}//="localhost"; $args{port}//=4444; $args{browser}//="*firefox"; $args{browser_url}//=''; return %args; }
      I have this error:

      Can't use string ("Selenium2") as a HASH ref while "strict refs" in us +e at accessor serverLocation defined at /home/saintex/workspace/perl_ +scripts/navigator/classes/Browser/ line 18.

      because the propriety "serverLocation" has not been read yet.
      How can I fix it?

        So the problem is that $self is not an object instance, but rather the class. One way to solve this is to use builder instead of default like so.

        has 'serverLocation' => (is=>'ro',isa=>'Str',builder =>'buildServerLoc +ation'); sub buildServerLocation { return "$Bin/lib/selenium-server-standalone-2.0b2.jar" } sub FOREIGNBUILDARGS { my $class = shift; my %args = ( @_ == 1 ? %{ $_[0] } : @_ ); $args{serverLocation} ||= $class->buildServerLocation; $args{host}//="localhost"; $args{port}//=4444; $args{browser}//="*firefox"; $args{browser_url}//=''; return %args; }


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://895225]
Approved by chrestomanci
Jar. Jar!...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2017-06-27 09:43 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (601 votes). Check out past polls.