Skip to content

Commit

Permalink
Disable PL_sawampersand
Browse files Browse the repository at this point in the history
PL_sawampersand actually causes bugs (e.g., perl #4289), because the
behaviour changes.  eval '$&' after a match will produce different
results depending on whether $& was seen before the match.

Using copy-on-write for the pre-match copy (preceding patches do that)
alleviates the slowdown caused by mentioning $&.  The copy doesn’t
happen unless the string is modified after the match.  It’s now a
post- match copy.  So we no longer need to do things differently
depending on whether $& has been seen.

PL_sawampersand is now #defined to be equal to what it would be if
every program began with $',$&,$`.

I left the PL_sawampersand code in place, in case this commit proves
immature.  Running Configure with -Accflags=PERL_SAWAMPERSAND will
reënable the PL_sawampersand mechanism.
  • Loading branch information
Father Chrysostomos committed Nov 27, 2012
1 parent 07d01d6 commit 1a904fc
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions embedvar.h
Expand Up @@ -287,7 +287,9 @@
#define PL_savestack (vTHX->Isavestack)
#define PL_savestack_ix (vTHX->Isavestack_ix)
#define PL_savestack_max (vTHX->Isavestack_max)
#ifndef PL_sawampersand
#define PL_sawampersand (vTHX->Isawampersand)
#endif
#define PL_scopestack (vTHX->Iscopestack)
#define PL_scopestack_ix (vTHX->Iscopestack_ix)
#define PL_scopestack_max (vTHX->Iscopestack_max)
Expand Down
4 changes: 4 additions & 0 deletions gv.c
Expand Up @@ -1638,6 +1638,7 @@ Perl_gv_fetchpvn_flags(pTHX_ const char *nambeg, STRLEN full_len, I32 flags,
case '[':
require_tie_mod(gv,name,newSVpvs("arybase"),"FETCH",0);
break;
#ifdef PERL_SAWAMPERSAND
case '`':
PL_sawampersand |= SAWAMPERSAND_LEFT;
(void)GvSVn(gv);
Expand All @@ -1650,6 +1651,7 @@ Perl_gv_fetchpvn_flags(pTHX_ const char *nambeg, STRLEN full_len, I32 flags,
PL_sawampersand |= SAWAMPERSAND_RIGHT;
(void)GvSVn(gv);
break;
#endif
}
}
}
Expand Down Expand Up @@ -1854,6 +1856,7 @@ Perl_gv_fetchpvn_flags(pTHX_ const char *nambeg, STRLEN full_len, I32 flags,
case '&': /* $& */
case '`': /* $` */
case '\'': /* $' */
#ifdef PERL_SAWAMPERSAND
if (!(
sv_type == SVt_PVAV ||
sv_type == SVt_PVHV ||
Expand All @@ -1867,6 +1870,7 @@ Perl_gv_fetchpvn_flags(pTHX_ const char *nambeg, STRLEN full_len, I32 flags,
? SAWAMPERSAND_MIDDLE
: SAWAMPERSAND_RIGHT;
}
#endif
goto magicalize;

case ':': /* $: */
Expand Down
2 changes: 2 additions & 0 deletions intrpvar.h
Expand Up @@ -291,7 +291,9 @@ The C variable which corresponds to Perl's $^W warning variable.
*/

PERLVAR(I, dowarn, U8)
#ifdef PERL_SAWAMPERSAND
PERLVAR(I, sawampersand, U8) /* must save all match strings */
#endif
PERLVAR(I, unsafe, bool)
PERLVAR(I, exit_flags, U8) /* was exit() unexpected, etc. */

Expand Down
4 changes: 4 additions & 0 deletions makedef.pl
Expand Up @@ -279,6 +279,10 @@ sub readvar {
++$skip{Perl_sv_setsv_cow};
}

unless ($define{PERL_SAW_AMPERSAND}) {
++$skip{PL_sawampersand};
}

unless ($define{'USE_REENTRANT_API'}) {
++$skip{PL_reentrant_buffer};
}
Expand Down
2 changes: 2 additions & 0 deletions perl.c
Expand Up @@ -873,7 +873,9 @@ perl_destruct(pTHXx)
PL_minus_F = FALSE;
PL_doswitches = FALSE;
PL_dowarn = G_WARN_OFF;
#ifdef PERL_SAWAMPERSAND
PL_sawampersand = 0; /* must save all match strings */
#endif
PL_unsafe = FALSE;

Safefree(PL_inplace);
Expand Down
5 changes: 5 additions & 0 deletions perl.h
Expand Up @@ -4912,6 +4912,11 @@ typedef enum {
#define SAWAMPERSAND_MIDDLE 2 /* saw $& */
#define SAWAMPERSAND_RIGHT 4 /* saw $' */

#ifndef PERL_SAWAMPERSAND
# define PL_sawampersand \
(SAWAMPERSAND_LEFT|SAWAMPERSAND_MIDDLE|SAWAMPERSAND_RIGHT)
#endif

/* Various states of the input record separator SV (rs) */
#define RsSNARF(sv) (! SvOK(sv))
#define RsSIMPLE(sv) (SvOK(sv) && (! SvPOK(sv) || SvCUR(sv)))
Expand Down
6 changes: 6 additions & 0 deletions regen/embed.pl
Expand Up @@ -441,7 +441,13 @@ END
my $sym;
for $sym (@intrp) {
if ($sym eq 'sawampersand') {
print $em "#ifndef PL_sawampersand\n";
}
print $em multon($sym,'I','vTHX->');
if ($sym eq 'sawampersand') {
print $em "#endif\n";
}
}
print $em <<'END';
Expand Down
2 changes: 2 additions & 0 deletions sv.c
Expand Up @@ -13057,7 +13057,9 @@ perl_clone_using(PerlInterpreter *proto_perl, UV flags,
PL_minus_F = proto_perl->Iminus_F;
PL_doswitches = proto_perl->Idoswitches;
PL_dowarn = proto_perl->Idowarn;
#ifdef PERL_SAWAMPERSAND
PL_sawampersand = proto_perl->Isawampersand;
#endif
PL_unsafe = proto_perl->Iunsafe;
PL_perldb = proto_perl->Iperldb;
PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
Expand Down

0 comments on commit 1a904fc

Please sign in to comment.