Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Problem with around BUILDARGS

by stvn (Monsignor)
on Mar 24, 2011 at 13:32 UTC ( #895250=note: print w/ replies, xml ) Need Help??


in reply to Problem with around BUILDARGS

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}//='http://wwww.google.com'; return %args; }

-stvn


Comment on Re: Problem with around BUILDARGS
Download Code
Replies are listed 'Best First'.
Re^2: Problem with around BUILDARGS
by saintex (Scribe) on Mar 24, 2011 at 14:29 UTC
    Thank you very much!
Re^2: Problem with around BUILDARGS
by saintex (Scribe) on Mar 28, 2011 at 10:58 UTC
    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}//='http://wwww.google.com'; 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/Selenium2.pm 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}//='http://wwww.google.com'; return %args; }

      -stvn
        thank you for your answer!
        Hello,
        I tried just now to use this solution in a real case...

        But It works, until you don't have to call the proprieties in your code.

        If you try to call $self->serverLocation anyway in your code you have that error:

        Can't use string ("nameOfTheClass") as a HASH ref while "strict refs" +in use at accessor serverLocation defined at /home/saintex/workspace/ +perl_scripts/navigator/classes/Browser/Selenium2.pm
        Any help? Thank you

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2015-07-29 21:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls