Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Function name in a variable, can't recall the concept

by bliako (Monsignor)
on Apr 02, 2019 at 20:55 UTC ( [id://1232048]=note: print w/replies, xml ) Need Help??


in reply to Re: Function name in a variable, can't recall the concept
in thread Function name in a variable, can't recall the concept

Here is another exception, no complaints:

use strict; use warnings; sub asub { print "heee\n"; } my $x = 'asub'; main->$x();

bw, bliako

Replies are listed 'Best First'.
Re^3: Function name in a variable, can't recall the concept
by AnomalousMonk (Archbishop) on Apr 02, 2019 at 23:26 UTC

    Not really an exception IMHO because asub is a "method" of the main class/package, so the  -> invocant works as documented.


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

      I would like to call it a method too but where is the reference to the main class/package "object" as the first parameter when calling asub() (a-la $obj->asub() which sends $obj as the first parameter)? Also, I would like to call it a "static method" but this: main::$x() does not work. Too many exceptions! (Edit: could n't resist the pun of too many anomalies :) )

      bw, bliako

        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:  <%-{-{-{-<

        I would like to call it a method too but where is the reference to the main class/package "object" as the first parameter when calling asub() (a-la $obj->asub() which sends $obj as the first parameter)?

        Just write it down properly:

        use strict; use warnings; sub asub { my $c = 0; print ++$c,": $_ - heee\n" for @_ } + my $x = 'asub'; main->$x($x, 'blorf'); __END__ 1: main - heee 2: asub - heee 3: blorf - heee

        As you can see, the package - main - is passed into the sub as the first argument.

        Note that the package passed is just a string, not a refenrence. It would be a reference, if the invocant was a blessed variable:

        use strict; use warnings; sub asub { my $c = 0; print ++$c,": $_ - heee\n" for @_ } + my $x = 'asub'; my $obj = bless \$x; $obj->$x($x, 'blorf'); __END__ 1: main=SCALAR(0x1492870) - heee 2: asub - heee 3: blorf - heee
        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-26 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found