New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"return" required in some anonymous closures #6853
Comments
From martin@suttles.sca.sfbay.sun.comCreated by martin@suttles.sca.sfbay.sun.comConsider the following perl program my $count = 0; This prints The two anonymous subs $x->[0] and $x->[1] should Perl Info
|
From @lizmatAt 23:01 +0000 10/19/03, martin@suttles.sca.sfbay.sun.com (via RT) wrote:
Confirmed. Please note that if you change: my $count = 0; to: our $count = 0; the result as is expected. So this would imply a lexical pad problem to me. Furthermore, if you change: my $count = 0; to: my $count = 'Foo'; the result is _still_: 0 3 which indicates to me the "0" is gotten from somewhere else, rather Liz |
From @lizmatAt 11:44 +0200 10/20/03, Elizabeth Mattijsen wrote:
Hmmm... both of the following return correct results also: BEGIN {my $count = 'foo'}; # $foo properly initialized at compile time my $count = 'foo'; So maybe this is an optimizer related problem as well? Liz |
From @lizmat(I should stop answering my own posts ;-) At 11:55 +0200 10/20/03, Elizabeth Mattijsen wrote:
The problem can be simplified to the following code: my $foo = 'foo'; so this looks like an optimizer problem to me: the $get sub is Liz |
From @iabynOn Mon, Oct 20, 2003 at 12:17:24PM +0200, Elizabeth Mattijsen wrote:
This isn't a bug, it's a feature :-) sub () {$lexical} is a constant sub generator, ie each call to 'sub' eg my $x; for (10,20) { outputs 10 11 since $a0 is a constant sub, but $a1 is a closure. -- |
From @lizmatAt 11:45 +0100 10/20/03, Dave Mitchell wrote:
I always thought you would need to document a bug before it becomes a Seriously, I haven't been able to find _any_ documentation on this If this _is_ a new feature of 5.8.0, maybe it should be at least Liz |
From nick.ing-simmons@elixent.comElizabeth Mattijsen <liz@dijkmat.nl> writes:
No. That has initialized a $count lexical to the BEGIN
No lexical $count in scope here 'tis a global.
|
From nick.ing-simmons@elixent.comElizabeth Mattijsen <liz@dijkmat.nl> writes:
What is a 'package lexical' ? |
From @lizmatAt 13:32 +0100 10/20/03, Nick Ing-Simmons wrote:
Good point: I of course meant: my $count; Unfortunately, I had missed the use of printf() rather than print, so Liz |
From @lizmatAt 13:34 +0100 10/20/03, Nick Ing-Simmons wrote:
A package lexical is a lexical defined at the package "scope"? package Foo; I thought that was a pretty well known concept? Liz |
From @mjdominusDave Mitchell <davem@fdgroup.com>:
Oh well, so much for the principle of least astonishment. What's the workaround for this feature? |
From @demerphq
Except that there is no such thing as package scope. There is file scope and #!perl -l __END__
Well, I think its a pretty well known misnomer. But it still is a misnomer. If you want a "package lexical" youll have to wrap the whole thing in a package foo; or { Personally I like the first one more.... Yves |
From @lizmatAt 08:58 -0400 10/20/03, Mark Jason Dominus wrote:
Dropping the prototype seems to work: my $foo = 'foo'; Liz |
From nick.ing-simmons@elixent.comElizabeth Mattijsen <liz@dijkmat.nl> writes:
package Bar; package Foo; |
From @iabynOn Mon, Oct 20, 2003 at 02:47:36PM +0200, Elizabeth Mattijsen wrote:
No, $package_lexical is a file-scoped lexical; it has nothing whatsoever -- |
From @iabynOn Mon, Oct 20, 2003 at 08:58:05AM -0400, Mark Jason Dominus wrote:
Remove the ()'s. -- |
From @lizmatAt 13:59 +0100 10/20/03, Orton, Yves wrote:
Or alternately: use strict; package Foo; package Bar; I see your point.
{ See, packages are scoped! ;-) Just kidding. I will now start to Liz |
From nick.ing-simmons@elixent.comMark Jason Dominus <mjd@plover.com> writes:
A. Don't give the () prototype (they don't do anything useful |
From nick.ing-simmons@elixent.comYves Orton <yves.orton@de.mci.com> writes:
And both are surely equivalent to {
|
From @demerphq
Obviously I only meant this at the lexical level...
Well I did assume that more would be inside the block, such as procedure Or did I miss your point? Yves |
From @iabynOn Mon, Oct 20, 2003 at 02:29:11PM +0200, Elizabeth Mattijsen wrote:
I think the best place would be in perlsub.pod in Personally I think its a confusing feature, but it appears to be used Anyone feel like deprecating it ;-) -- |
From @lizmatAt 14:57 +0100 10/20/03, Dave Mitchell wrote:
Ok, I'll do that but first:
Well, I guess Dominus feels like me, that it is catching people by surprise. I think this would call for a compile-time CODE attribute ": constant": {my ($name, $val) = @_; *$name = sub : constant{$val} Liz |
From nick.ing-simmons@elixent.comYves Orton <yves.orton@de.mci.com> writes:
Ah - I didn't I took your examples as "boiler plate" to generate
No.
|
From @gbarrOn 20 Oct 2003, at 13:29, Elizabeth Mattijsen wrote:
It is only supposed to generate a constant sub if there are no other
Constant subs were in 5.6.0 IIRC
I suspect it is a bug in the code that determines if a sub can be Graham. |
From @iabynOn Mon, Oct 20, 2003 at 04:59:20PM +0100, Graham Barr wrote:
But it's easy to create a reference after the event: sub new_constant_sub {
but anonymous 'const sub generators' were only introduced in 5.8.0, by [ 7389] By: jhi on 2000/10/21 14:26:45
No, I think its a deliberate feature (but I could be wrong!) + -- |
From perl_dummy@bloodgate.com-----BEGIN PGP SIGNED MESSAGE----- Moin, Dave Mitchell <davem@fdgroup.com>:
Oh. That got me by surprise. I thought that I followed p5p quite closely, but sub () { $lexical; } Best wishes, Tels - -- "Yeah, baby, yeah!" -----BEGIN PGP SIGNATURE----- iQEVAwUBP5QTkncLPEOTuEwVAQE85gf+KtNnAsgerq4BpXMKTCvfk7VKnvlPnA2m |
From @gbarrOn 20 Oct 2003, at 17:48, Dave Mitchell wrote:
But that is not when its decided that its a constant sub. Or at least When constant subs were first introduced it was in the optimizer that I am guessing that logic has been pushed back to when the sub is Graham.
|
From @ysthOn Sun, Oct 19, 2003 at 11:01:42PM -0000, "martin@suttles.sca.sfbay.sun.com (via RT)" <perlbug-followup@perl.org> wrote:
This surprising behaviour is intentional. See perlsub/Constant Actually, there does seem to be a bug here. If you reverse the order |
From @iabynOn Mon, Oct 20, 2003 at 06:20:13PM +0100, Graham Barr wrote:
The 5.6.0 behaviour was that subs (named or anon) were not marked as CONST This meant that C<use constant> cloned a full heavyweight anon sub for In 5.8.0, sub f(){1}-style subs were detected at compile time as being In bleedperl, I changed it to a hybrid behaviour. At compile time, the Thus, the refcnt<2 test is done: 5.6.0: sometime after cloning, when the sub call is being compiled Both the 5.8.0 and bleedperl behaviours have problems, and are very Thus, I like Elizabeth's suggestion of using a compile-time attribute Dave. -- |
From @nwc10On Mon, Oct 20, 2003 at 11:53:03PM +0100, Dave Mitchell wrote:
I like it, but I'm worried that I may be hasty, so I'd like to to simmer It would be nice if this (or something related) also provided a solution to As these "subroutines" are actually constant value returning, it would a: there was a way to declare them them from henceforth to be constants (Maybe I'm confused. Maybe we just need Exporter to declare them with b: there was a way to make the tokenizer/optimizer whatever call the sub Note that you can't simply call the AUTOLOAD routine on all the subs as Nicholas Clark |
From @druud62On 2010-11-29 10:22, Dave Mitchell wrote:
The compiler should patch the constsub at the '$x = 6' line. -- |
From @AbigailOn Tue, Nov 30, 2010 at 10:34:36PM +0100, Dr.Ruud wrote:
If the compiler can detect that afterwards $x won't change - but you BEGIN { the compiler could only patch the constsub if it knows bar() won't Abigail |
From @druud62On 2010-12-01 00:24, Abigail wrote:
And that is not '$x = CONSTANT', so that should then be a compile error. -- |
From @AbigailOn Wed, Dec 01, 2010 at 07:38:30PM +0100, Dr.Ruud wrote:
You're proposing a sub of the form 'sub {$x = shift;}' should be Abigail |
From @druud62On 2010-12-02 10:33, Abigail wrote:
Indeed. The $x there, is (an alias for) a constant, and the compiler A 'const' keyword would help. -- |
From @jkeenanOn Tue Oct 21 14:04:34 2003, davem wrote:
Discussion in this ticket petered out nine years ago. Are there any code or documentation changes we should be considering If not, can we close the ticket? Thank you very much. |
From @cpansproutOn Fri Mar 30 18:15:13 2012, jkeenan wrote:
This is still a bug that needs fixing. See also -- Father Chrysostomos |
From @nwc10The question is at the end. It won't make much sense without the backstory. On Sun, Oct 19, 2003 at 11:01:42PM -0000, martin@suttles.sca.sfbay.sun.com (via RT) wrote:
And the behaviour in blead remains is the same On Fri, Mar 30, 2012 at 10:15:03PM -0700, Father Chrysostomos via RT wrote:
What's curious is that I never remember anyone hitting this problem On Mon, Oct 20, 2003 at 11:53:03PM +0100, Dave Mitchell wrote:
I forgot that you thought of this. I knew that I *implemented* it :-) On Tue, Oct 21, 2003 at 12:53:12AM +0100, Dave Mitchell wrote:
On Tue, Oct 21, 2003 at 09:34:24PM +0100, Dave Mitchell wrote:
constant.pm now takes advantage of the "proxy constant subroutines" *foo::bar = sub () { $baz; } for an unshared $baz of value 42 behaves the same as sub foo::bar () { and creates a constant subroutine that the parser knows it can inline. So I think we've done the first part of that - "another way"
So, do we want to remove the inlining of (inferred to be)-constant value In the general case, is it impossible to detect all scenarios where the (Proxy constant subroutines remain unchanged) Nicholas Clark |
From zefram@fysh.orgNicholas Clark wrote:
Inlining of *actual* constant-value subs should remain, but "sub () { I've been wondering where the inlining really ought to be implemented. -zefram |
From @nwc10On Fri, Apr 20, 2012 at 12:43:50PM +0100, Zefram wrote:
To be clear, there's a specific subset of sub () { $x } which are perceived Also, assuming that it could be made 100% accurate, as the inlining is
What does this generator give us, that putting references-to-constants in
Does this reduce complexity? Nicholas Clark |
From zefram@fysh.orgNicholas Clark wrote:
Oh really, I didn't realise there was any attempt for it to be seriously With PadWalker and suchlike, it's impossible for it to be certain about
By definition, if an optimisation is done correctly then you can't tell
It gets you the constant-value sub as a sub, in isolation. It avoids [variant call checker]
Don't know yet. Probably leaves it about the same overall. That's why -zefram |
From @cpansproutOn Fri Apr 20 04:38:42 2012, nicholas wrote:
Don’t we ask people to check whether bugs have been reported already? I
Yes!
Let’s put it in 5.17.early and find out! -- Father Chrysostomos |
From @cpansproutOn Fri Apr 20 04:52:01 2012, nicholas wrote:
Subroutine redefinition. -- Father Chrysostomos |
From @iabynOn Fri, Apr 20, 2012 at 12:37:55PM +0100, Nicholas Clark wrote:
Well, having a quick look at constant.pm, it may use the new method as its my [ just by-the-by, I don't understand why constant.pm is written in such a I don't know enough about how the new method works to be abloe to say
It's effectively impossible, and its trivially easy to later modify the { After I changed it so that the refcnt < 2 test is done at runtime during { So, I think we should remove this 'feature'. -- |
From @nwc10On Fri, Apr 20, 2012 at 04:55:12PM +0100, Dave Mitchell wrote:
Hmmm, bother. Although that can probably be solved like this: *_CAN_PCS = $] > 5.009002 ? sub () { 1 } : sub () { 0 }; (not tested)
It is a dual-life CPAN distribution. See https://metacpan.org/module/constant
I thought that it didn't use them, but there is one obvious bit that is still # The constant serves to optimise this entire block out on I think that's only hard to work round in the case of naming a constant I'm not so sure about this: } elsif (@_) { but did list inlining *ever* actually inline? I'm going to guess "no", given In the current version of Perl, list constants are not inlined date from 5.004 when constant.pm was added http://perl5.git.perl.org/perl.git/blame/HEAD:/dist/constant/lib/constant.pm#l347
Given that constant doesn't *need* it any more, there seems to be consensus
I was initially thinking "do we really need a warning? No" Nicholas Clark |
From @demerphqOn Friday, 20 April 2012, Dave Mitchell wrote:
We use this a lot at work. We can recode to use other mechanisms to produce Dosomething() if CONSTANT; Is used a lot. Yves -- |
From @nwc10On Sat, Apr 21, 2012 at 10:48:13AM +0200, demerphq wrote:
Not sure if you're aware as it's been cut - the context is *specifically* sub CONSTANT_FROM_VAR () { $foo; } constants generated like this: sub CONSTANT_FROM_CONSTANT () { 42 } aren't a problem. (And constants generated by constant.pm aren't a problem) Nicholas Clark |
From patcat88@snet.netIf this bug is about return or no return keyword in constant optimized |
From @cpansproutFixed in 137da2b. -- Father Chrysostomos |
From [Unknown Contact. See original ticket]Fixed in 137da2b. -- Father Chrysostomos |
@cpansprout - Status changed from 'open' to 'resolved' |
From @avarOn fös 26 júl 00:45:03 2013, sprout wrote:
I'm re-opening this bug because as discussed in http://code.activestate.com/lists/perl5-porters/206933/ I think this fix should be backed out for now, and since it changes explicitly documented behavior should at the very least go through a deprecation cycle where we first add a warning indicating that subroutines that were previously inlined no longer are. Otherwise we're just fixing a really obscure bug at the cost of performance regressions in code that relies on the previously documented behavior, and changing that behavior silently. I think a perfectly acceptable way to deal with this bug would be to just document this special case, which I've done in 'perlsub: Improve the "Constant Functions" documentation' which I'm about to push to a branch. I.e. say that if you really need a subroutine that returns one variable and has a () prototype but don't want inlining just do : sub X () { return $x } |
@avar - Status changed from 'resolved' to 'open' |
From ambrus@math.bme.huOn Fri, Apr 20, 2012 at 1:51 PM, Nicholas Clark <nick@ccl4.org> wrote:
It allows the constant pragma to define an inlined constant subroutine Ambrus |
Migrated from rt.perl.org#24250 (status was 'open')
Searchable as RT24250$
The text was updated successfully, but these errors were encountered: