Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Number of times I've used goto in Perl

by hippo (Bishop)
on Sep 26, 2018 at 13:33 UTC ( [id://1223067]=note: print w/replies, xml ) Need Help??


in reply to Re: Number of times I've used goto in Perl
in thread Number of times I've used goto in Perl

Compare and contrast:

#!/usr/bin/env perl use strict; use warnings FATAL => 'recursion'; my $i = 0; one(); sub one { print "$i\n" unless ++$i % 10; push @_, 'foo'; &one; }
#!/usr/bin/env perl use strict; use warnings FATAL => 'recursion'; my $i = 0; one(); sub one { print "$i\n" unless ++$i % 10; push @_, 'foo'; goto &one; }

Like much in the way of the Swiss Army Chainsaw you need to know precisely what you're doing if you run it with the guards off. Limbs can be easily lost.

Replies are listed 'Best First'.
Re^3: Number of times I've used goto in Perl
by LanX (Saint) on Sep 26, 2018 at 14:19 UTC
    Hm ...

    ... is the goto version faster? Is this a way to have tail-recursion in Perl?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      I'm pretty sure there was a detailed explanation of what makes goto slower (in most cases?) than a plain ⊂ call. But while searching for it I did end up finding this post and this answer. I would be tempted to guess that the function call with goto was more expensive (because of the extra magic to clear the local scope and play with the call stack), but the return was more efficient because it didn't require going back through all the calling stack (ie, tail recursion done right). I wouldn't rely on the result of benchmarks from 2005 for current versions of perl though :P

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-24 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found