Unfortunately, that gives a warning.

I note that:

use strict ; use warnings ; our $inner = "SET" ; sub inner { my ($s) = @_ ; print "$s outermost inner \$inner=$inner\n" ; } ; print "outermost start\n" ; inner('') ; buddy('') ; wrapper('') ; buddy('') ; print "outermost end\n" ; sub wrapper { my ($s) = @_ ; print "$s wrapper entered\n" ; inner($s.' ') ; local *inner ### ; print "$s *** separate local\n" ; *inner = sub { my ($s) = @_ ; $inner ||= "NOT SET" ; print "$s innermost inner $inner\n" ; } ; inner($s.' ') ; buddy($s.' ') ; print "$s wrapper exit\n" ; } ; sub buddy { my ($s) = @_ ; print "$s buddy entered\n" ; inner($s.' ') ; print "$s buddy exit\n" ; } ;
gives:
outermost start
  outermost inner $inner=SET
  buddy entered
    outermost inner $inner=SET
  buddy exit
  wrapper entered
    outermost inner $inner=SET
Subroutine main::inner redefined at zz.pl line 29.
    innermost inner SET
    buddy entered
      innermost inner SET
    buddy exit
  wrapper exit
  buddy entered
    outermost inner $inner=SET
  buddy exit
outermost end
showing the same warning. Also the value of $inner is unaffected by the local -- which may or may not be a surprise.

But if you remove the ### in the code above, to make that line:

local *inner ; print "$s *** separate local\n" ; *inner
that gives:
outermost start
  outermost inner $inner=SET
  buddy entered
    outermost inner $inner=SET
  buddy exit
  wrapper entered
    outermost inner $inner=SET
  *** separate local
    innermost inner NOT SET
    buddy entered
      innermost inner NOT SET
    buddy exit
  wrapper exit
  buddy entered
    outermost inner $inner=SET
  buddy exit
outermost end
NB: using the stand alone local *inner is undefining all parts of *inner.

I guess that the '=' is doing special things to assign the rhs to the right part of the lhs GLOB, so this is not a run of the mill assignment. Could this be why the presence of the initialiser appears to change the effect of local on a GLOB ?

You will recall that in another thread Setting signal handlers considered unsafe? it was established that  local $SIG{ALRM} = ... that the local sets the lhs undef before the assignment takes place. Which this isn't consistent with !


In reply to Re^6: sub fuction inside sub functioin? by gone2015
in thread sub fuction inside sub functioin? by deewanagan

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.