in reply to Need Help Refactoring Method Calls

I don't think you can take references to class methods like that.
The obvious solution (to me at least) is to make the package name the variable:

sub _fill_lists { my ($thing, $thing_ref) = @_; my $class = "My::Class::$thing"; # + <== if (ref $giant_hash{$thing} eq 'ARRAY') { for (@{ $giant_hash{$thing} }) { push @{ $thing_ref }, $class->new( $_ ); # + <== } } else { push @{ $thing_ref }, $class->new( $giant_hash{$thing} ); # + <== } }

Replies are listed 'Best First'.
Re^2: Need Help Refactoring Method Calls
by jffry (Hermit) on Jan 30, 2006 at 17:14 UTC
    Wow. I really did think of something like this at first, but I concluded that it would be a soft reference that the 'warnings' pragma wouldn't catch but Perl Lint would. I didn't want to use a fancy soft reference like that.

    However, since no one else has commented that what you wrote was a soft reference (even though it still looks like one to me), I decided to run an example thru Perl Lint, and sure enough, it passes.

    # cat test_me.pl #!/usr/bin/perl -w use strict; use warnings; use Top::Middle; my $class = "Top::Middle"; my $obj = $class->new( 'info' => 'some_data_or_whatever' ); $obj->method(); exit 0;
    # perl -MO=Lint test_me.pl test_me.pl syntax OK
      I don't think we're dealing with soft references here. The $class is just a string. Perl lets you write barewords for package names as well, but really it's just the package name that you're passing around.

      Remember, Foo->new, or $class->new, both translate to new( Foo ) or new( $class ), which is simply passing the string into the new() call.

      (I know, I should probably have written Foo::new('Foo'), plus there's some method dispatching logic buried in the ->, but that doesn't detract from the fact.)