in reply to Re: Re: Is this a symbolic reference?
in thread Is this a symbolic reference?

I think Damian's position is clear and the opposite of redsquirrel's interpretation of it. In other words, yes, it is a symbolic reference. You are using the symbolic name of the method. It isn't outlawed by use strict, and it isn't a symbolic reference to a variable, and it isn't a "bad thing". But it is a symbolic reference. It has to look up the name of the subroutine to call in the symbol table.

To quote more context:

"David Christensen" <dchristensen@california.com> writes:

>> my $ref = \&Bar::FooOnYou; >> $var->$ref("Howdy\n"); >OK interesting symetry with Martien's approach: >I'm wondering if this is TIMTOWDI, or if there's a gotcha in >there...

No gotchas. In fact the $bar->$ref syntax is the preferred one - it makes it clearer you're doing a method call.

Note that you can also use symbolic references and write:

my $ref = "Bar::FooOnYou"; $var->$ref("Howdy\n");

But you didn't hear that from me ;-)

Damian

So, as with all symbolic references, it isn't the syntax ($obj->$ref(@args) in this case) that makes it symbolic or not. It is whether $ref contains a real reference or simply a string to be looked up in a symbol table (or perhaps several symbol tables in this case since inheritance might be involved).

The alternatives are mostly just uglier and even have some bad features (like preventing late binding) so I'd just use this type of symbolic reference, call it "dynamic method invocation", and be happy.

Update: The only thing "bad" I see about this is that, in some cases, it may allow something to be used that shouldn't (the usual problem with symbolic references). So I encourage you to have, if appropriate, a list of allowed methods when using this. But that won't always be appropriate.

And if it weren't for Perl's somewhat sloppy OO model, I'd be even less worried. Many Perl OO classes will have subroutines that aren't meant to be object methods but that are in a symbol table such that they could be used as object methods. And this usage wouldn't catch such "mistakes". But using UNIVERSAL::can() wouldn't either. [ Nor would the usual $obj->method(), for that matter, except that you aren't supposed to write such code unless you've been told that it is okay. (: ]

Update2: Drat. I just realized that there is a dark side to this. You can use evil like $ref= "main::Unsubscribe" to execute stuff that you really shouldn't. UNIVERSAL::can() has the same "security problem". So I strongly urge you to ensure there are no colons nor apostrophes in any method name that you use in this way. ):

I'm tempted to consider this as a bug that should be fixed, but that goes somewhat against the grain of Perl...

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: Is this a symbolic reference?
by redsquirrel (Hermit) on Jan 17, 2002 at 00:13 UTC
    After re-rereading TheDamien's c.l.p.m. post, I see your point. I did misinterpret his intent.

    Thanks for clearing that up tye. My bad.

    --Dave

Re^2e: Is this a symbolic reference?
by Aristotle (Chancellor) on Jan 17, 2002 at 20:55 UTC
    I'm tempted to consider this as a bug that should be fixed, but that goes somewhat against the grain of Perl..

    My gut feeling is it should be chalked up as another case of the leash being loose enough to hang yourself with. Raising a warning might be appropriate though.