I'll stick to my assertion that asub can properly be deemed a "method" of main for | for the purposes of  -> invocation (although I have to agree that this terminology (update: and usage!) is a little bit perverse): it follows all the rules for class and object reference invocation. And  main::$x() can even be made to work (sorta) without strictures (which were invented (update: in part) to put a stop to this kind of madness) if the string variable is a package variable and syntax is as documented.

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "sub func { print qq{hey @_}; } my $symref = 'func'; our $symref = 'func'; ;; my $mainobj = bless []; dd $mainobj; ;; main->$symref('ho'); $mainobj->$symref('there'); ;; &$main::symref('nonny-nonny'); " bless([], "main") hey main ho hey main=ARRAY(0x157618c) there Can't use string ("func") as a subroutine ref while "strict refs" in u +se at -e line 1. c:\@Work\Perl\monks>perl -w -MData::Dump -le "sub func { print qq{hey @_}; } my $symref = 'func'; our $symref = 'func'; ;; my $mainobj = bless []; dd $mainobj; ;; main->$symref('ho'); $mainobj->$symref('there'); ;; &$main::symref('nonny-nonny'); " bless([], "main") hey main ho hey main=ARRAY(0x185614c) there hey nonny-nonny
c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "sub func { print qq{hey @_}; } sub bonk { print qq{zot @_}; } ;; my $symref = 'func'; ;; use vars qw($symref); $main::symref = 'bonk'; ;; my $mainobj = bless []; dd $mainobj; ;; main -> $symref('ho'); 'main' -> $symref('hi'); my $mainstring = 'main'; $mainstring -> $symref('he'); $mainobj -> $symref('there'); ;; no strict 'refs'; &$main::symref('nonny-nonny'); " bless([], "main") hey main ho hey main hi hey main he hey main=ARRAY(0x37716c) there zot nonny-nonny
(And they're not anomalies, they are features!) Run under ActiveState 5.8.9.

Update 1: Changed example code to better illustrate difference between lexical and package variables.

Update 2: And BTW, this even works (under strictures!) with aliasing:

my $str = 'aaa'; for my $alias (main::, 'main', $mainstring, $mainobj) { $alias->$symref($str++); }


Give a man a fish:  <%-{-{-{-<


In reply to Re^5: Function name in a variable, can't recall the concept (updated x2) by AnomalousMonk
in thread Function name in a variable, can't recall the concept by mpersico

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.