I couldn't figure out how to pass a nested subroutine.
"pass" a sub? You didn't pass a sub. You defined it, then used it, then returned it's return value. If you want to pass a sub, you need a reference to it. Off the top of my head, there are 2 easy ways:

1) Define an anonymous (no-name) sub, and assign it to a scalar ref

my $square_this = sub { my $x = shift; return $x**2; };
2) Take a reference to a named sub
sub square { my $x = shift; return $x**2; } my $square_this = \□
Use it like this (either version):
my $squared = $square_this->($hypotenuse);
Perhaps what you really meant was nested sub, in the Pascal sense. In Perl there's no such animal per se, though it can be emulated. In Perl we could talk about a limited scope sub, only available to a certain sub or collection of subs. Again, a couple of ways:

1) Packages: define a package to limit scope

package Square; sub square { my $x = shift; return $x**2; }
Use it one of these ways:
1) Inside package Square (perhaps by other routines in that package)
my $squared = square($hypotenuse);
2) Outside of package Square, e.g., main:
my $squared = Square::square($hypotenuse);
3) If you do this, it won't be limited in scope.
Package Square is in Square.pm, the following is in main:
use Square qw(square); my $squared = square($hypotenuse);
The solution I think you're looking for (and tell me if I'm wrong), is a closure with an anonymous sub, that can only be seen by your named sub. For instance:
{ # closure for cubed my $square = sub { my $x = shift; return $x**2; }; sub cubed { my $z = shift; return $z*$square->($z); } } # end closure for cubed
Now only the subs (and other code) in the closure can see $square (unless you give out a reference to it).

-QM
--
Quantum Mechanics: The dreams stuff is made of


In reply to Re^3: "Biochem BINGO" or "Noob Seeks Zen Thrashing" by QM
in thread "Biochem BINGO" or "Noob Seeks Zen Thrashing" by NamfFohyr

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.