opensourcer has asked for the wisdom of the Perl Monks concerning the following question:

this is a package
package Utils::Agent; sub SetDestination { my $self = shift; my $class = shift; my $construct = "\$".$class; # i have lot of destination in different packages # so i can't say every time $pkg::destination $Destination = $construct::destination; return $Destination; }
-----------
use Utils::Agent; use Data::Config; use Web::Config; my $config = new Data::Config; my $webConfig = new Web::Config; my $utils = new Utils::Agent; $utils->SetDestination(ref($config));
and when i try this i get nothing

Replies are listed 'Best First'.
Re: calling scalar from scalar. symlinks
by ikegami (Patriarch) on Oct 25, 2006 at 18:04 UTC
    It's very hard to tell what you want. Are you trying to return \&Web::Config::destination where Web::Config is dynamic?
    sub SetDestination { my ($self, $class) = @_; $self->{destination} = do { no strict 'refs'; \&{"${class}::destination"} }; } sub ... { my ($self, ...) = @_; ... $self->{destination}->(...); ... }

    In any case, this is poor design. Why not just pass $config?

    sub SetDestination { my ($self, $config) = @_; $self->{config} = $config; } sub ... { my ($self, ...) = @_; ... $self->{config}->destination(...); ... }
      Your package needs to return a true value -- ie. the last line of the package should be:
      1;
      Where do you want *them* to go today?
Re: calling scalar from scalar. symlinks
by quester (Vicar) on Oct 25, 2006 at 22:31 UTC
    Ikegami's suggestion to fix the design is probably a good idea, but if you "get nothing" the first thing to reach for is the Perl debugger... try
      perl -wd name.pl
         b linenumber
         c
         x $construct
    
    The documentation should be accessible in "perldocs perldebtut" and "perldocs perldebug".