Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Code reuse question

by Athanasius (Archbishop)
on Jul 31, 2014 at 07:53 UTC ( [id://1095690]=note: print w/replies, xml ) Need Help??


in reply to Indent, anonymous sub, or lexical sub for module private code?

Anonymous Monk’s approach — prefix your sub’s name with an underscore and rely on the convention that subs so-named are to be treated as private — is likely the simplest and most convenient solution. But yes, an anonymous subroutine will give you what you want if you keep the (only) reference to it in a lexical variable:

package MyModule; my $sub = sub { ... }; $sub->() for 1 .. 2; # call the sub twice

However, this seems like overkill if you will really only call the subroutine twice. In that case, “just wrap it in another loop” and prevent the code from “disappear[ing] off the right of [your] screen” by re-formatting the code (splitting long lines). This is generally a good idea anyway as it makes the code easier to read (and to annotate with comments).

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 13:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found