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, |
In reply to Re: Code reuse question
by Athanasius
in thread Indent, anonymous sub, or lexical sub for module private code?
by 1s44c
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |