Weird coincidence

Ovid on 2003-09-17T00:53:50

After talking with a coworker, I came up with a scheme that would allow me to do this:

#!/usr/bin/perl -w
use strict;
use Class::WhenNeeded 'Foo';

my $customer = Foo::Customer->new;
my $company  = Foo::Company->new;
my $salesrep = Foo::Company::SalesRep->new;

In other words, you to skip long declarations of "using" all of the packages (in my scheme, this happens at runtime on an "as needed" basis).

So here I am, thinking this is such an interesting idea and frankly, I haven't seen anyone do anything this general purpose before with this type of syntax. So I post this to Perlmonks.

The same day, BrowserUK poses a virtually identical problem. What a weird frickin' coincidence. I feel like a bit stupid as it almost looks like I stole someone elsess idea (and hence this long post).


Weird coincidence

nkuitse on 2003-09-17T13:34:33

In my current project I'm using -> notation to do the same sort of thing:
package Foo;
sub Foo::Bar { require Foo::Bar; 'Foo::Bar' }
 
package Whatever;
use Foo;
my $bar = Foo->Bar->new;
I just hand-code the Bar method, but then I don't need multi-level stuff like my $qux = Foo->Bar->Baz->Qux->new, which would be relatively easy with AUTOLOAD.

What's not a coincidence is the (not very obvious) similarity here to Self and its "paths to objects" idiom/dogma.

See also UNIVERSAL::moniker

schwern on 2003-09-18T17:17:13

However, I don't think it contains the actual method call magic.