in reply to Re^2: The & prototype and code references in scalars.
in thread The & prototype and code references in scalars.
Another way:
sub testit{ print 'hi' } sub doit (&) { $_[0]->() } $codeRef = \&testit; doit { &$codeRef };
update (after BrowserUk's response below) -
My suggestion adds next to nothing, since wrt to sub stacks, $ref->() is just syntactic sugar for &$ref or vice versa. The notation doit \&{ $subref } seems to be the right thing to do:
#!/usr/bin/perl use Carp qw(cluck); sub testit{ cluck 'hi',$/ };; sub doit (&) { print $_[0],$/;$_[0]->() };; $codeRef = \&testit;; doit { &$codeRef }; __END__ CODE(0x955737c) hi at coderef.pl line 3 main::testit called at coderef.pl line 6 main::__ANON__() called at coderef.pl line 4 main::doit('CODE(0x955737c)') called at coderef.pl line 6
#!/usr/bin/perl use Carp qw(cluck); sub testit{ cluck 'hi',$/ };; sub doit (&) { print $_[0],$/;$_[0]->() };; $codeRef = \&testit;; doit \&{ $codeRef }; __END__ CODE(0x9f4c11c) hi at coderef.pl line 3 main::testit() called at coderef.pl line 4 main::doit('CODE(0x9f4c11c)') called at coderef.pl line 6
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: The & prototype and code references in scalars.
by LanX (Saint) on Feb 18, 2010 at 00:11 UTC | |
by biohisham (Priest) on Feb 18, 2010 at 11:52 UTC | |
by LanX (Saint) on Feb 18, 2010 at 12:36 UTC | |
|
Re^4: The & prototype and code references in scalars.
by BrowserUk (Patriarch) on Feb 18, 2010 at 00:22 UTC |