in reply to How do I interpolate package name in a fully qualified name?

package Zelda; our $type = "prince"; package Mario; our $type = "hero"; package Kupa; our $type = "villan"; package main; foreach my $pkg (qw/Zelda Mario Kupa/) { print ${$pkg.'::type'}, "\n"; } __END__
But seriously, isn't this what objects are for ?:)

Replies are listed 'Best First'.
Re^2: How do I interpolate package name in a fully qualified name?
by AnomalousMonk (Archbishop) on Apr 21, 2011 at 02:44 UTC
    ... isn't this what objects are for?

    Second that. (And strictures don't have to be turned off.)

    >perl -wMstrict -le "{ package Zelda; sub type { shift; return 'prince'; } } ;; { package Mario; sub type { shift; return 'hero'; } } ;; { package Kupa; sub type { shift; return 'villan'; } } ;; foreach my $pkg (qw/Zelda Mario Kupa/) { printf qq{$pkg is a %s \n}, $pkg->type; } " Zelda is a prince Mario is a hero Kupa is a villan

    Update: Note: The  shift; in each of the
        sub type { shift;  return 'whatever'; }
    subroutine definitions was intended only to point up the fact that a string representing the package (or class) name is implicitly passed as the first argument to any class method (which this subroutine is). Because the method does not interact with the argument list in any way, it is not needed.

      OK, Both of them are working. Thank you.