http://www.perlmonks.org?node_id=895973


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

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

Replies are listed 'Best First'.
Re^4: Problem with around BUILDARGS
by saintex (Scribe) on Mar 29, 2011 at 14:23 UTC
    thank you for your answer!
Re^4: Problem with around BUILDARGS
by saintex (Scribe) on Apr 19, 2011 at 16:45 UTC
    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

      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.

      -stvn
        Hello, I'm trying to use the accessor as string.

        That is the method in my class:

        sub startServer { my $self = shift; defined(my $pid=fork) or die "Cannot fork: $!"; unless ($pid) { eval { exec 'java -jar '.$self->serverLocation; }; die "Impossibile far partire il server selenium: $@\n;" if ($@ +); } }
        Thank you for your help.

        I'm interested to solve this problem for knowledge reason, not for production... so I'm not in any hurry.