Skip to Main Content
Perl Cookbook
book

Perl Cookbook

by Tom Christiansen, Nathan Torkington
August 1998
Intermediate to advanced content levelIntermediate to advanced
800 pages
39h 20m
English
O'Reilly Media, Inc.
Content preview from Perl Cookbook

Nesting Subroutines

Problem

You want to have nested subroutines, such that one subroutine is only visible and callable from another. When you try the obvious approach of nesting sub FOO { sub BAR { } ... } Perl gives you warnings about variables that will not stay shared.

Solution

Instead of having the inner functions be normal subroutines, make them closures and temporarily assign them to the typeglob of the right name to create a localized function.

Discussion

If you use nested subroutines in other programming languages with their own private variables, you’ll have to work at it a bit in Perl. The intuitive coding of this kind of thing gives the warning “will not stay shared”. For example, this won’t work:

sub outer {
    my $x = $_[0] + 35;
    sub inner { return $x * 19 }   # WRONG
    return $x + inner();
}

The following is a workaround:

sub outer {
    my $x = $_[0] + 35;
    local *inner = sub { return $x * 19 };
    return $x + inner();
}

Now inner() can only be called from within outer() because of the temporary assignments of the closure. But when it does, it has normal access to the lexical variable $x from the scope of outer().

This essentially creates a function local to another function, something not directly supported in Perl; however, the programming isn’t always clear.

See Also

The sections on “Symbol Tables” in Chapter 5 of Programming Perl and in perlmod (1); the section on “Closures” in Chapter 4 of Programming Perl and the discussion of closures in perlref(1); Section 10.13; Section 11.4 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Perl in a Nutshell

Perl in a Nutshell

Nathan Patwardhan, Ellen Siever, Stephen Spainhour
Perl Best Practices

Perl Best Practices

Damian Conway
Mastering Perl

Mastering Perl

brian d foy
Perl Cookbook, 2nd Edition

Perl Cookbook, 2nd Edition

Tom Christiansen, Nathan Torkington

Publisher Resources

ISBN: 1565922433Supplemental ContentCatalog PageErrata