Skip to content

Commit f0d4931

Browse files
committed
gv_magicalize: Refactor
This refactors to eliminate redundant code. Some things are magical only if we are using the main stash; others in any stash; one only in PL_debstash. Previously, the switches were structured thusly: 1) if we aren't using the main stash, handle things not requiring the main stash 2) if we are using the main stash, handle all len > 1 things that can be in the main stash. This duplicates much of item 1) 3) if we are using the main stash, handle all len == 1 things that can be in the main stash. This duplicates some of item 1) The new structure is if (len > 1) { 1) handle len > 1 things not requiring main stash, regardless of the stash we are in 2) handle len > 1 things requiring main stash } else { 3) handle len == 1 things, regardless of the stash we are in. } This removes the duplicated code. The case for 'a' and 'b' are special. When 'a' stands for "args" it is len > 1 and that is handled in 1). But 'a' can also mean a single character, as 'b' always does. These cases are handled in 3). These are the only two len == 1 characters that don't have to be in the main package, so there is an extra conditional clause to allow that.
1 parent df5f779 commit f0d4931

File tree

1 file changed

+26
-36
lines changed

1 file changed

+26
-36
lines changed

gv.c

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,11 +2094,11 @@ S_gv_magicalize(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len,
20942094
goto try_core;
20952095
}
20962096

2097-
if (stash != PL_defstash) { /* not the main stash */
2098-
/* We only have to check for a few names here: a, b, EXPORT, ISA
2099-
and VERSION. All the others apply only to the main stash or to
2100-
CORE (which is checked right after this). */
2097+
if (len > 1) {
21012098
switch (*name) {
2099+
2100+
/* The first set don't require this to be the main stash */
2101+
21022102
case 'E':
21032103
if (
21042104
len >= 6 && name[1] == 'X' &&
@@ -2122,16 +2122,15 @@ S_gv_magicalize(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len,
21222122
GvMULTI_on(gv_AVadd(gv));
21232123
break;
21242124
}
2125-
/* FALLTHROUGH */
2126-
case 'b':
2127-
if (len == 1 && sv_type == SVt_PV)
2128-
GvMULTI_on(gv);
2129-
/* FALLTHROUGH */
2130-
default:
2125+
goto try_core;
2126+
2127+
default:
2128+
2129+
/* The remainder apply only to the main stash */
2130+
if (stash != PL_defstash) {
21312131
goto try_core;
21322132
}
2133-
}
2134-
else if (len > 1) {
2133+
21352134
switch (*name) {
21362135
case 'A':
21372136
if (memEQs(name, len, "ARGV")) {
@@ -2141,21 +2140,6 @@ S_gv_magicalize(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len,
21412140
GvMULTI_on(gv);
21422141
}
21432142
break;
2144-
case 'E':
2145-
if (
2146-
len >= 6 && name[1] == 'X' &&
2147-
(memEQs(name, len, "EXPORT")
2148-
||memEQs(name, len, "EXPORT_OK")
2149-
||memEQs(name, len, "EXPORT_FAIL")
2150-
||memEQs(name, len, "EXPORT_TAGS"))
2151-
)
2152-
GvMULTI_on(gv);
2153-
break;
2154-
case 'I':
2155-
if (memEQs(name, len, "ISA")) {
2156-
gv_magicalize_isa(gv);
2157-
}
2158-
break;
21592143
case 'S':
21602144
if (memEQs(name, len, "SIG")) {
21612145
HV *hv;
@@ -2187,10 +2171,6 @@ S_gv_magicalize(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len,
21872171
}
21882172
}
21892173
break;
2190-
case 'V':
2191-
if (memEQs(name, len, "VERSION"))
2192-
GvMULTI_on(gv);
2193-
break;
21942174
case '\003': /* $^CHILD_ERROR_NATIVE */
21952175
if (memEQs(name, len, "\003HILD_ERROR_NATIVE"))
21962176
goto magicalize;
@@ -2292,10 +2272,15 @@ S_gv_magicalize(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len,
22922272
paren = (SSize_t)(I32)uv;
22932273
goto storeparen;
22942274
}
2295-
}
2296-
} else {
2297-
/* Names of length 1. (Or 0. But name is NUL terminated, so that will
2298-
be case '\0' in this switch statement (ie a default case) */
2275+
}
2276+
}
2277+
}
2278+
else if ( stash == PL_defstash /* Names of length 1. */
2279+
|| *name == 'a' || *name == 'b')
2280+
{
2281+
/* All but the above two length 1 names have to be in the main stash.
2282+
* */
2283+
22992284
switch (*name) {
23002285
case '&': /* $& */
23012286
paren = RX_BUFF_IDX_FULLMATCH;
@@ -2450,10 +2435,15 @@ S_gv_magicalize(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len,
24502435
SvREFCNT_dec(sv);
24512436
}
24522437
break;
2453-
case 'a':
2438+
2439+
case 'a': /* The len > 1 case was handled above */
24542440
case 'b':
24552441
if (sv_type == SVt_PV)
24562442
GvMULTI_on(gv);
2443+
/* FALLTHROUGH */
2444+
2445+
default:
2446+
goto try_core;
24572447
}
24582448
}
24592449

0 commit comments

Comments
 (0)