in reply to Creating new objects from within a package

$ 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.

Replies are listed 'Best First'.
Re^2: Creating new objects from within a package
by hawtin (Prior) on Jul 22, 2004 at 07:46 UTC

    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