Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^2: Problem with around BUILDARGS

by saintex (Scribe)
on Mar 28, 2011 at 10:58 UTC ( #895883=note: print w/replies, xml ) Need Help??

in reply to Re: Problem with around BUILDARGS
in thread Problem with around BUILDARGS

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?

Replies are listed 'Best First'.
Re^3: Problem with around BUILDARGS
by stvn (Monsignor) on Mar 28, 2011 at 17:01 UTC

    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; }

      thank you for your answer!
      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/
      Any help? Thank you

        I would need to see more code, but I suspect that something inside the 'serverLocation' accessor is trying to use the "nameOfTheClass" as if it was an object instance. Show some more context and perhaps I can help.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2022-01-25 23:03 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (69 votes). Check out past polls.