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

Re: [perl #54728] PathTools-3.27 triggers a bug in Perl

Thread Previous | Thread Next
Dave Mitchell
August 12, 2013 14:44
Re: [perl #54728] PathTools-3.27 triggers a bug in Perl
Message ID:
On Fri, Aug 09, 2013 at 10:26:37AM +0000, Ed Avis wrote:
> But at present $1 exists in a strange halfway house where it is neither truly
> global nor truly local.  It's a global variable where Perl shuffles around the
> value behind the scenes;

In this respect, $1 is no different than for example $. :

    sub f {
	print "$_[0]\n"; # prints 1
	open my $fh, $0 or die;
	<$fh>; <$fh>;
	print "$_[0]\n"; # prints 2

    open my $fh, $0 or die;

> but this does not give the same behaviour as:
>     our $o;
>     sub foo {
>         local $o = 'a';
>         say $_[0] + $_[1];
>     }
>     local $o = 123;
>     foo(5, $o);
> Even though foo() is setting the value of $o, this doesn't break the
> argument passing because $o is localized.  If $1 were localized properly
> then it wouldn't break either.

Well yes, but for a successful match, this would involve:

* for $1:
    * save the existing scalar slot of *1 on the savestack;
    * create a new SV and copy the relevant substring of the match string
      to it;
    * put that new SV in the scalar slot of *1;
    * on scope exit, undo the above and free the SV;
* ditto for $2, and any other captures;
* possibly ditto for $`, $&, $&;
* possibly ditto for ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH};
* for @-:
    similarly localise, creating a new array with new indices;
* something similar for @+, %-, %+;
* possibly something similar for $+, $^N, $^R.

Which collectively would impose a significant performance penalty for each
successful match.

Potentially these vars could remain magic, but when localised, a new var with
the same magic attached could be created, where the magic points to a
different match object (i.e. PL_curpm). This would avoid having to copy
the relevant substring to each var until needed, but would still involve
lots of SV creation and destruction and typeglob+savestack activity for
each successful match.

"Strange women lying in ponds distributing swords is no basis for a system
of government. Supreme executive power derives from a mandate from the
masses, not from some farcical aquatic ceremony."
    -- Dennis, "Monty Python and the Holy Grail"

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About