[Mpls-pm] Indirect reference/inheritance?

Ian Malpass ian at indecorous.com
Wed May 24 13:36:53 PDT 2006


Chris wrote:

> However, my attempt at an indirect reference is failing.
> 
> $class=ref($object)
> $var=$class."::Foo";
> 
> foreach $k (keys(%{$var}))
> {
> 	# do stuff
> }
> 
> I'm stuck with perl 5.8.0 on this one.
> 
> Any ideas what I'm screwing up?

Are you sure $object contains a reference, and isn't just the class 
name? Because then ref would return undef and $var would be "::Foo".

Ian

PackageA.pm:

   package PackageA;

   use strict;
   use warnings;

   sub foo {
       my $self = shift;
       my $class = ref $self || $self;
       no strict 'refs';
       for ( keys %{ $class . "::Foo" } ) {
           print $_, "\n";
       }
       use strict 'refs';
   }

   1;

PackageB.pm:

   package PackageB;

   use strict;
   use warnings;
   use base qw( PackageA );

   %PackageB::Foo = (
       a => 1,
       b => 2,
   );

   1;

On the command line:

   % perl -I./ -MPackageB -e 'PackageB->foo();'
   a
   b

   % perl -I./ -MPackageB -e 'my $b = bless {}, "PackageB"; $b->foo();'
   a
   b


More information about the Mpls-pm mailing list