in reply to Inlining a "lexically-scoped scalar which has no other references"

Neither a bug nor a doc error:
# same as 'use constant FOO => 10' BEGIN { my $lex = 10; *FOO = sub () { $lex }; }

Dave.

  • Comment on Re: Inlining a "lexically-scoped scalar which has no other references"
  • Download Code

Replies are listed 'Best First'.
Re^2: Inlining a "lexically-scoped scalar which has no other references"
by BrowserUk (Patriarch) on Sep 28, 2009 at 04:57 UTC

    That's cool:

    perl -MO=Deparse -e"BEGIN{ my $x = 12345; *Foo = sub(){ $x } }; print +Foo" sub BEGIN { my $x = 12345; *Foo = sub () { $x; } ; } print 12345; -e syntax OK

    But why is the BEGIN block necessary?:

    perl -MO=Deparse -e"{ my $x = 12345; *Foo = sub(){ $x }; } print Foo() +" { my $x = 12345; *Foo = sub () { $x; } ; } print Foo(); -e syntax OK

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      But why is the BEGIN block necessary?:

      You can't compile a subroutine call into a constant if the subroutine hasn't been made into a constant yet

        So even though the sub declaration has been seen before you get to the call to it, it hasn't yet been compiled?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.