Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
"be consistent"
 
PerlMonks  

Re: $foo = "Foo::Bar"; $foo->new() works?

by dws (Chancellor)
on Jan 22, 2005 at 23:18 UTC ( [id://424335]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to $foo = "Foo::Bar"; $foo->new() works?

One reason that it is a Very Good Thing that it works this way is that it allows you to write things like subclassable factories, where the class of the thing being constructed is determined by the factory subclass.

package Factory; sub makeThing { my ($class, @args) = @_; my $thingClass = $class->thingClass; my $thing = $thingClass->new(@args); $class->magicStuff($thing); return $thing; } sub thingClass { 'Thing' } sub magicStuff { my ($class, $thing) = @_; # pretend there's generic magic stuff here }

Now, to change the class of the thing being constructed, you simply subclass the factory class and override one method.

package SpecialFactory; use base qw(Factory); sub thingClass { 'SpecialThing' }

Now, Factory->makeThing gives you a Thing, and SpecialFactory->makeThing gives you a SpecialThing.

Replies are listed 'Best First'.
Re^2: $foo = "Foo::Bar"; $foo->new() works?
by Thelonious (Scribe) on Jan 23, 2005 at 10:29 UTC
    or you could even

    sub thingClass { ref $_[0] }

    in the base class, and forget about having to create overrides.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://424335]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.