Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Creating new objects from within a package

by hawtin (Prior)
on Jul 21, 2004 at 14:15 UTC ( [id://376247]=perlquestion: print w/replies, xml ) Need Help??

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

Brothers,

I am having trouble with calling new within one package to create an object from a different one. Within the code below the call to new within the s1 subroutine gives me an error:

Bareword "T" not allowed while "strict subs" in use

but I don't see why that is different from the code 10 lines higher up. What am I missing?

use strict; use warnings; { # This works my $l2 = new T; my $l1 = new P; exit 0; } { package P; sub new { my $self = {}; bless $self,shift; # This call works my $t1 = new T; return $self; } sub s1 { # This call complains that # Bareword "T" not allowed # while "strict subs" in use my $t1 = new T; } } sub s2 { # Even this works my $t1 = new T; } { package T; sub new { return bless {},shift; } }

Update:Added s2 subroutine to prove that it is something to do with packages. Also this is on Perl 5.6.1 from ActiveState running on a Windows 2000 box (but I doubt that makes a difference)

Replies are listed 'Best First'.
Re: Creating new objects from within a package
by dave_the_m (Monsignor) on Jul 21, 2004 at 14:46 UTC
    $ perl -MO=Deparse -e 'sub new { new T } sub s1 { new T }' sub new { 'T'->new; } sub s1 { new 'T'; }
    The first use of 'new' appears before the 'new' sub has been compiled, so perl doesn't know about it yet. It assumes that 'new T' must be an indirect method call on the 'T' class. In the second case, 'new' now exists in the symbol table, so perl assumes you're calling the function 'new' with the parameter 'T', which is a bareword.

    Dave.

      Yes, thank you dave_the_m that explains it. Of course in most real code this issue will never arise because the class will be declared before it is called.

      And in this case I can fix the issue by "predefining" the forward ref with

      { package T; }

      somewhere before the class is used

Re: Creating new objects from within a package
by herveus (Prior) on Jul 21, 2004 at 14:27 UTC
    Howdy!

    That's interesting. I get the same error using 5.6.1 on solaris 8.

    If you move the package T block to before the package P block, the error goes away. If you say "T->new" instead of "new T", the error also goes away.

    yours,
    Michael
Re: Creating new objects from within a package
by BUU (Prior) on Jul 21, 2004 at 14:32 UTC
    Just to second herveus, indirect method calls ( meth class() ) are, in general, funky and unsupported. You should probably be using class->meth() instead, as that leaves no ambiguities.

      I wouldn't go so far as to say they are unsupported, however there are certainly issues of disambiguation as the OP has seen

      /J\

        Neither does it "leaves no ambiguities". T->new() can still be ambiguous, it is just not as ambiguous. If you want to leave no ambiguities, write 'T'->new().

        - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://376247]
Approved by gellyfish
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-19 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found