Why are you using a goto on a ref to a method when you already have a ref to it

I'm not the OP, so I can only speculate on what the intention might have been...  but maybe the idea was to not mess up the call stack? Compare:

#!/usr/bin/perl package Foo; use Carp "cluck"; sub new { my $class = shift; return bless { @_ }, $class; } sub myFunction { my $self = shift; cluck $self->{msg}; } sub foo1 { my $self = $_[0]; my $func = $self->can('myFunction'); goto &$func; } sub foo2 { my $self = shift; my $func = sub { $self->myFunction }; goto &$func; } sub foo3 { my $self = shift; $self->myFunction(); } package main; my $obj = Foo->new( msg => "foo" ); $obj->myFunction(); # direct call $obj->foo1(); # indirect, using goto (transparent) $obj->foo2(); # indirect, using goto via closure $obj->foo3(); # indirect, using regular call via $self

Output:

foo at ./722627.pl line 16 Foo::myFunction('Foo=HASH(0x63c430)') called at ./722627.pl li +ne 40 foo at ./722627.pl line 16 Foo::myFunction('Foo=HASH(0x63c430)') called at ./722627.pl li +ne 41 foo at ./722627.pl line 16 Foo::myFunction('Foo=HASH(0x63c430)') called at ./722627.pl li +ne 27 Foo::__ANON__() called at ./722627.pl line 42 foo at ./722627.pl line 16 Foo::myFunction('Foo=HASH(0x63c430)') called at ./722627.pl li +ne 33 Foo::foo3('Foo=HASH(0x63c430)') called at ./722627.pl line 43

Only the goto variant of foo1() is leaving behind a call stack comparable to the direct call.


In reply to Re^2: oo code ref by almut
in thread oo code ref by Anonymous Monk

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.