[Israel.pm] package/sub ambiguity

Yuval Kogman nothingmuch at woobling.org
Sun Jun 1 12:35:57 PDT 2008


This is because barewords always give way to subs while parsing and
a class name is just a bareword that didn't get any complaints
because it was used in method context under strict.

An easy way to see the effect:

	perl -MO=Deparse -e 'sub Foo;  Foo->new'
	Foo()->new;

and lastly, see http://search.cpan.org/perldoc?aliased

On Sun, Jun 01, 2008 at 22:25:29 +0300, Shmuel Fomberg wrote:
> Hi All.
> 
> Here is what I had:
> a package (object) named Repeater and a sub named Repeater.
> when I tried to create a new Repeater, I did:
> $r = Repeater->create(...);
> and got unexpected results.
> After thinking a bit about it, I found out that instead of running the 
> create method in the Repeater package, it instead run the Repeater sub, 
> and tried to run create on the result.
> 
> meaning, I actually got:
> $r = (&Repeater())->create(...);
> 
> A way to resolve that was to write:
> $r = "Repeater"->create(...);
> a probably better way to resolve this is not creating packages and subs 
> with the same names.
> 
> Any advice from the community? I actually liked the sub-package name 
> duality.
> 
> Thanks,
> Shmuel.
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl

-- 
  Yuval Kogman <nothingmuch at woobling.org>
http://nothingmuch.woobling.org  0xEBD27418




More information about the Perl mailing list