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

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'

Replies are listed 'Best First'.
Re^6: Function name in a variable, can't recall the concept
by bliako (Abbot) on Apr 14, 2019 at 08:46 UTC
    It would be a reference, if the invocant was a blessed variable

    thanks for reminding me that