develooper Front page | perl.perl5.porters | Postings from February 2013

Re: [perl #116981] 'our sub' segfaults perl if the sub did notcompile

Thread Previous | Thread Next
From:
Jesse Luehrs
Date:
February 28, 2013 07:15
Subject:
Re: [perl #116981] 'our sub' segfaults perl if the sub did notcompile
Message ID:
20130228071514.GK23176@tozt.net
On Wed, Feb 27, 2013 at 11:05:42PM -0800, Peter Martini via RT wrote:
> Digging into this, I also realize:
> 
> perl -Mfeature=lexical_subs -e 'my sub foo { foo(1);} foo;'
> Undefined subroutine &main::foo called at -e line 1.
> 
> Is that intentional?

It is consistent with how lexical variables work elsewhere - new
lexicals are not available to be used until after the statement they are
declared in is finished:

  $ perl -E'use strict; say(my $foo = 1, $foo)'
  Global symbol "$foo" requires explicit package name at -e line 1.
  Execution of -e aborted due to compilation errors.

On the other hand, it's not consistent with how non-lexical subs work.
This is probably one of the cases where a decision had to be made about
what to be consistent with, and I think in most cases, the choice was to
be consistent with the equivalent long-hand version, so
'my $foo = sub { $foo->(1) }' in this case (which also doesn't work).
It's possibly useful to note that __SUB__ will work in both of these
cases.

That said, I do agree that it's confusing, and an argument could
possibly be made for doing it the other way, although I don't know what
the implications for the implementation would be.

-doy

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About