Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How can my sub cannot get hidden in the scope of function such in
use strict; sub out{ my sub this{my $n=9;print "\n$n"} my $i=7; my $j=3 } &this; 9

How is it clarified ?

Replies are listed 'Best First'.
Re: issue 'my' sub cannot work
by LanX (Saint) on Jan 03, 2022 at 13:09 UTC
    works for me

    D:\tmp>perl use warnings; use strict; sub out{ my sub this{my $n=9;print "\n$n"} } &this; __END__ Undefined subroutine &main::this called at - line 12.

    This is perl 5, version 32, subversion 1 (v5.32.1) built for MSWin32-x +64-multi-t hread

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      O sorry. fine as rule and the problem is it is scratch run interactive in debug and not purged / reset. so most likely garbage 9 is there as one-liner above code is run
      if reset it stops
      Topic should not on this but on "How to purge all everything easily in interactive debug " as it annoys much to set 0 or '' so many 'my' one by one in there
Re: issue 'my' sub cannot work
by Anonymous Monk on Jan 03, 2022 at 13:24 UTC

    I have tried your code under the most-recent release of every production Perl from 5.18 to 5.34, and they all produce the output I expect: Undefined subroutine &main::this called at fubar.pl line 6. This is because the 'my' restricts the scope of sub this to the interior of sub out.

    Did you get something else? If so, what?

    Were you expecting something else? If so, what?

    Note that for some of the Perls I tried it was necessary to run the script this way:

    $ perl -Mexperimental=lexical_subs fubar.pl
    
      > Did you get something else? If so, what?

      The OP claimed the 9 was printed.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        Ah. At the time I posted all I had was the original problem statement, so I assumed the 9 was part of the Perl source.