-
Notifications
You must be signed in to change notification settings - Fork 540
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
List::Util attaching to the wrong $_ when used inside given/when construct #9798
Comments
From vparseval@gmail.comCreated by vparseval@gmail.comPlease behold the following: #!/usr/bin/perl -l use feature qw/switch/; my $val = 1; # my $_ Note that when commenting out the lexical declaration of $_, the code behaves At this point, I am not sure if the problem is in List::Util or the perl core. Cheers, Perl Info
|
From p5p@perl.wizbit.be
[code snipped]
No it doesn't. Or at least not for me. I suggest you check your test This seem to happen because given creates a lexical $_. It also #!/usr/bin/perl -l use strict; sub s1 (&) { print "Given/when block:"; print ""; print ""; $ perl-5.10.0 rt-67694.pl For loop For loop with lexical $_ Best regards, Bram |
The RT System itself - Status changed from 'new' to 'open' |
From @rgs2009/7/21 Bram <p5p@perl.wizbit.be>:
Hmm what ? No, given does not create a lexical $_ : from pp_entergiven : if (PL_op->op_targ == 0) { (rewriting that with the proper macros will be nice) (not sure if What happens, at first glance, it that List::Util always uses the We provide UNDERBAR and dUNDERBAR macros to access the current $_, be However we don't have (yet) an API to localize the current $_ as I suppose that code from pp_grepstart could be copied in List::Util to |
From p5p@perl.wizbit.beCiteren Rafael Garcia-Suarez <rgarciasuarez@gmail.com>:
I didn't look at the code only at the docs and the output :/ from perldoc perlsyn: the output shows a different reference for $_ in the when block and in
Then what does in create in pp_entergiven? Adding an our $_ does solve the problem: Given/when block: But why is this be nessesary?
That would only work for the XS version right? Best regards, Bram |
From @rgs2009/7/21 Bram <p5p@perl.wizbit.be>:
Ah, you're right. The parser has this line :
Yes. |
From @iabynOn Tue, Jul 21, 2009 at 12:45:04PM +0200, Rafael Garcia-Suarez wrote:
So if I've understood this correctly, given() adds a lexical $_ to the use List::Util qw/first/; outputs: ok outside -- |
From p5p@perl.wizbit.beCiteren Dave Mitchell <davem@iabyn.com>:
I'm not sure if 'bug' is the correct word. What happens: (example with for instead of given) #!/usr/bin/perl -l for my $_ (1) { sub foo { $c is a closure and closes over $_. But: the closure closes over $_ so it will always uses the value 1... There are, as fas as I can see, two solutions: 1) modify given() so it doesn't use a lexical $_. 2) update the documentation for given() and make it more explicit that Best regards, Bram |
From @iabynOn Fri, Jul 24, 2009 at 07:16:17PM +0200, Bram wrote:
Well, use List::Util qw/first/; outputs ok grep So, the behaviour of first() could be described as unexpected, to say the -- |
From @rgs2009/7/24 Dave Mitchell wrote:
Yes, although I'm not 100% sure that pp_given does exactly the right -- |
From @rgs2009/7/24 Rafael Garcia-Suarez <rgarciasuarez@gmail.com>:
Yes, the patch below solves your reduced test case, the one with grep Graham, would you consider an improved version of the patch below for --- a/ext/List-Util/ListUtil.xs if(items <= 1) { for(index = 1 ; index < items ; index++) { |
From @ikegamiOn Fri, Jul 24, 2009 at 4:42 PM, Rafael Garcia-Suarez <
How are the Pure Perl implementation of List::Util and countless other Perl |
From @ysthOn Fri, July 24, 2009 3:44 pm, Eric Brine wrote:
Add a reference to the proper $_ to the caller() return list? |
From @gbarrOn Jul 24, 2009, at 4:10 PM, Rafael Garcia-Suarez wrote:
Sure. first is the only place in List::Util where $_ is used. This Although should the code passed to first call a sub, that code will Graham.
|
From @ysthOn Fri, July 24, 2009 5:28 pm, Yitzchak Scott-Thoennes wrote:
Err, never mind, that wouldn't tell you what $_ was in scope of the sub if |
From fany@cpan.orgCreated by fany@cpan.orgWhen I use a function which expects a codeblock as first Or, in other words, the test script below will output the 1 - 2 - 3 - grep OK! Kind regards, - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #!/usr/bin/perl -w use 5.01; sub fpp(&@) { my @array = 1 .. 3; my $where = ''; $where = ' within given'; Perl Info
|
From @cpansproutOn Sat Dec 10 09:55:36 2011, fany@cpan.org wrote:
This is a known issue, #67694. given does an implicit ‘my $_’. See RJBS: Would you be adverse to changing ‘given’ to alias $_ properly, or -- Father Chrysostomos |
The RT System itself - Status changed from 'new' to 'open' |
From @cpansproutOn Sat Dec 10 11:51:52 2011, sprout wrote:
BTW, the easiest workaround is to use ‘for’ instead of ‘given’. You can -- Father Chrysostomos |
From [Unknown Contact. See original ticket]On Sat Dec 10 11:51:52 2011, sprout wrote:
BTW, the easiest workaround is to use ‘for’ instead of ‘given’. You can -- Father Chrysostomos |
From tchrist@perl.com"Father Chrysostomos via RT" <perlbug-followup@perl.org> wrote
I've always felt this to be a bug, and would like to see some way --tom |
From @LeontOn Sat, Dec 10, 2011 at 9:04 PM, Tom Christiansen <tchrist@perl.com> wrote:
I'm afraid the only solution is «use 5.016;» :-| Leon |
From @cpansproutOn Sat Dec 10 12:04:57 2011, tom christiansen wrote:
Use powdered eggs. :-) -- Father Chrysostomos |
From @cpansproutOn Sat Dec 10 12:10:19 2011, LeonT wrote:
There is this comment in feature.pm: # TODO: I think that approach would cause too much confusion. feature.pm deals -- Father Chrysostomos |
From @rjbs* Father Chrysostomos via RT <perlbug-followup@perl.org> [2011-12-10T14:51:53]
I agree, *strenuously* that 'given' is a big problem on several fronts, and the I think fixing it via something like :5.16 containing given5_16 is not nuts, In the meantime, would we want to consider warning on closing over lexical $_ This email did not get a lot of deep thought. I composed it during a brief -- |
From @doyOn Sat, Dec 10, 2011 at 06:41:41PM -0500, Ricardo Signes wrote:
+1. I also don't think it's worth trying to reintroduce new semantics
Probably +1, although I haven't given it much thought yet. -doy |
From @cpansproutOn Sat Dec 10 15:42:18 2011, perl.p5p@rjbs.manxome.org wrote:
From a maintenance standpoint, +1000! But I am loath to break anyone’s
Absolutely.
I doubt it. -- Father Chrysostomos |
From @xdgOn Sat, Dec 10, 2011 at 6:41 PM, Ricardo Signes
I'm supportive only if we replaced it with a decent switch construct
A warning sounds reasonable and anyone doing it on purpose can turn -- David |
From chris@prather.orgI missed the list (my apologies David). -Chris ---------- Forwarded message ---------- On Sat, Dec 10, 2011 at 7:52 PM, David Golden <xdaveg@gmail.com> wrote:
Switch and Smart Matching are not equivalent. I'm guessing (hoping?) that Ric is simply talking about deprecating for ($var) { That is all you need for switch is a topicalizer (for) and a test that Fact is that I try very hard to only use given/when in cases where I
no warnings "given"; # none 'expected'; :) -Chris |
From @LeontOn Sun, Dec 11, 2011 at 12:41 AM, Ricardo Signes
It's possible to do the right thing AFAIK, it's just that no one does Leon |
From @cpansproutOn Sun Dec 11 06:05:24 2011, LeonT wrote:
What’s so ironic is that, if called functions can see it, $_ isn’t -- Father Chrysostomos |
From @rjbs* Father Chrysostomos via RT <perlbug-followup@perl.org> [2011-12-11T09:48:24]
given ($x) { Something along those lines?
Well, they're seeing it because they closed over it, right? That's still -- |
From @cpansproutOn Mon Dec 12 12:20:53 2011, perl.p5p@rjbs.manxome.org wrote:
I think Leon Timmermans meant that functions like Scalar::Util::first given ($x) {
I’m referring to the ability of XS modules (like Scalar::Util::first) to -- Father Chrysostomos |
From @LeontOn Mon, Dec 12, 2011 at 10:03 PM, Father Chrysostomos via RT
Exactly.
Though I'm not sure any module uses it currently TBH, ppport and some Leon |
From @cpansproutFixed by b5a6481. |
@cpansprout - Status changed from 'open' to 'resolved' |
Migrated from rt.perl.org#67694 (status was 'resolved')
Searchable as RT67694$
The text was updated successfully, but these errors were encountered: