Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by Aristotle (Chancellor)
on Jan 23, 2005 at 16:00 UTC ( [id://424400]=note: print w/replies, xml ) Need Help??


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

$ perl -MO=Deparse,-x7 -e'Foo::Bar->new()' 'Foo::Bar'->new; -e syntax OK

Pay attention to the quotes: as far as Perl is concerned, class methods are always invoked on a string containing the package name. Whether that's a literal string or one stored in a variable matters naught.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^2: $foo = "Foo::Bar"; $foo->new() works?
by gaal (Parson) on Jan 23, 2005 at 16:32 UTC
    It even turns out that there is no speed difference. (I expected *some* advantage to the inline string, because there'd be an extra indirection. But it's lost to the method call overhead.)

    use Benchmark; package Bar; sub new {}; package main; my $obj; my $class = "Bar"; # "our" *is* slower, though. timethese -1, { bareword => sub { $x = Bar->new() }, var => sub { $x = $class->new() }, }; ######################################### Benchmark: running bareword, var for at least 1 CPU seconds... bareword: 1 wallclock secs ( 1.06 usr + 0.00 sys = 1.06 CPU) @ 59 +0160.38/s (n=625570) var: 1 wallclock secs ( 1.06 usr + 0.00 sys = 1.06 CPU) @ 59 +0160.38/s (n=625570)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2025-07-20 18:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.