develooper Front page | perl.perl5.porters | Postings from July 2009

Re: [perl #67694] List::Util attaching to the wrong $_ when used inside given/when construct

Thread Previous | Thread Next
From:
Graham Barr
Date:
July 24, 2009 17:30
Subject:
Re: [perl #67694] List::Util attaching to the wrong $_ when used inside given/when construct
Message ID:
DD621103-BF0E-4BB0-8CBE-6C2DE6910F88@pobox.com
On Jul 24, 2009, at 4:10 PM, Rafael Garcia-Suarez wrote:
> Graham, would you consider an improved version of the patch below for
> List::Util, to make first work with lexical $_ ? I don't think there
> are other areas in List::Util that need a similar modification.

Sure. first is the only place in List::Util where $_ is used. This  
patch would work for the XS version, but what about the pure Perl ?

Although should the code passed to first call a sub, that code will  
not see $_, but I suspect grep has the same issue there

Graham.

>
> --- a/ext/List-Util/ListUtil.xs
> +++ b/ext/List-Util/ListUtil.xs
> @@ -329,16 +329,25 @@ CODE:
>     I32 gimme = G_SCALAR;
>     SV **args = &PL_stack_base[ax];
>     CV *cv;
> +    PADOFFSET padoff_du = find_rundefsvoffset();
> +    bool has_global_underbar = padoff_du == NOT_IN_PAD
> +       || PAD_COMPNAME_FLAGS_isOUR(padoff_du);
>
>     if(items <= 1) {
>        XSRETURN_UNDEF;
>     }
>     cv = sv_2cv(block, &stash, &gv, 0);
>     PUSH_MULTICALL(cv);
> -    SAVESPTR(GvSV(PL_defgv));
> +    if (has_global_underbar)
> +       SAVESPTR(GvSV(PL_defgv));
> +    else
> +       SAVESPTR(PAD_SVl(padoff_du));
>
>     for(index = 1 ; index < items ; index++) {
> -       GvSV(PL_defgv) = args[index];
> +       if (has_global_underbar)
> +           GvSV(PL_defgv) = args[index];
> +       else
> +           PAD_SVl(padoff_du) = args[index];
>        MULTICALL;
>        if (SvTRUE(*PL_stack_sp)) {
>          POP_MULTICALL;
>


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