Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
You cannot use local on lexically scoped variables. This is a totally artificial restriction but it means that in those (rare) ocasions when the semantics of local are the tidiest way to implement something you need to use package variables.

Perl does not support nested named subroutines. If you need nested subroutines you can use anonymous subroutines but this makes stackdumps nigh-on unreadable. Using file-scoped lexicals with local() would be a solution but as I said before you can't do that. So the next best thing is our().

I once wrote some twisted recursive code to render tables of tables in HTML. Sometimes it would cluck() and I'd get a backtrace with a dozen levels of anonynous subroutine.

use AtExit; sub awful { my ($various,%shared,@vars); my ($recusive1,$recursive2); my $cleanup = AtExit->new(sub{ undef $recursive1; undef $recursive2; }); $recursive1 = sub { # Do stuff with $various,%shared,@vars and make further calls +to # $recursive1->() and $recursive2->() } $recursive2 = sub { # Do stuff with $various,%shared,@vars and make further calls +to # $recursive1->() and &$recursive2->() and call frobnicate($recursive1); } $recursive1->(); }

I decided it was more maintainable as:

our ($various,%shared,@vars); sub awful { local ($various,%shared,@vars); recursive1(); } sub recursive1 { # Do stuff with $various,%shared,@vars and make further calls to # recursive1() and recursive2() } sub recursive2 { # Do stuff with $various,%shared,@vars and make further calls to # recursive1() and recursive2() frobnicate(\&recursive1); }

Note that frobnicate may call awful() but this inner awful() would not want to share the outer awful()'s variables.

Now there is a potential problem here if frobnicate() store its CODE reference argument somewhere and thus manages to call back to recursive1() after awful() completes or for that matter during a reentrant call of awful(). But I happened to know that wasn't going to happen.

There's a similar problem to the nested subroutine problem with regex containing (?{}) except you don't get the warning.

use strict; use warnings; sub foo { my $x = shift; print "$x "; sub foo1 { print "$x "; # Warning } foo1(); '' =~ /(?{ print "$x\n" })/; # No warning } foo('Hello'); # Prints 'Hello Hello Hello' foo('Goodbye'); # Prints 'Goodbye Hello Hello' sub bar { local our $x = shift; print "$x "; sub bar1 { print "$x "; } bar1(); '' =~ /(?{ print "$x\n" })/; } bar('Hello'); # Prints 'Hello Hello Hello' bar('Goodbye'); # Prints 'Goodbye Goodbye Goodbye' __END__

In reply to Re^3: OUR declaration by nobull
in thread OUR declaration by Anonymous Monk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2023-11-28 16:44 GMT
Find Nodes?
    Voting Booth?

    No recent polls found