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
In reply to Re^5: Function name in a variable, can't recall the concept
by shmem
in thread Function name in a variable, can't recall the concept
by mpersico
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |