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

Dear Monks, Why is this failing? Thanks Monks!
package Test; use strict; use Module::Test; our @ISA = qw ( Module::Test ); sub new { my $class = shift; my $t = "Test"; my $self = Module::$t->new; bless ($self, $class); return $self; } Bad name after Module:: at /home/bert/Test.pm line 18. 1;

Replies are listed 'Best First'.
Re: object contstruction
by blueberryCoffee (Scribe) on Dec 14, 2004 at 10:32 UTC
      Or just
      my $self = "Module::$t"->new();
      as ysth mentions.

      Note to original poster: the quotes are there to help perl understand where exactly the package name is. It is sometimes is not as obvious as here:

      my $t = 'Module::Test'; my $self = $t->new();
      --kap
Re: object contstruction
by dragonchild (Archbishop) on Dec 14, 2004 at 14:04 UTC
    If you're just trying to call your parent's new() function, you should do something like:
    my $self = $class->SUPER::new( @_ );

    If your parent is using the two-argument form of bless (which is bless ($self, $class);), then you don't have to re-bless $self - it will already be blessed into the right class.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: object contstruction
by zejames (Hermit) on Dec 14, 2004 at 10:15 UTC
Re: object contstruction
by rinceWind (Monsignor) on Dec 14, 2004 at 10:26 UTC
    1. You have Module::Test inheriting from itself. This is clearly wrong. Omit the line
      our @ISA = qw ( Module::Test );
    2. The following line is where your error is:
      my $self = Module::$t->new;
      I'm trying to understand what you are attempting to do here. You are wanting to call the constructor of some other class in some other Module:xxx namespace, and you are then blessing this into your passed classname.

      This is syntactically invalid, and also breaks strict 'refs', or rather would bresk it if you specified

      my $self = ${"Module::$t"}->new;
    I suggest you familiarise yourself with perl OO, by reading the documentation: perldoc perlobj and perlboot. Also, get hold of a copy of "Learning Perl" or "Programming Perl", as these books provide the details in dead tree form.

    --
    I'm Not Just Another Perl Hacker

      You have Module::Test inheriting from itself.
      Incorrect. Look again.
      The following line is where your error is:
      my $self = Module::$t->new;
      I'm trying to understand what you are attempting to do here. You are wanting to call the constructor of some other class in some other Module:xxx namespace, and you are then blessing this into your passed classname.

      This is syntactically invalid, and also breaks strict 'refs', or rather would bresk it if you specified

      my $self = ${"Module::$t"}->new;
      That's not right. It's perfectly legal (even under strict) to use interpolated strings as package names: "M::$t"->new. Your suggestion would look for a variable named $Module::Test and call new in the package or for the object contained in that variable, which is clearly not what the OP is trying to do.