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")

In reply to (tye)Re: Is this a symbolic reference? by tye
in thread Is this a symbolic reference? by demerphq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.