Skip to content

Commit de90a05

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 3ce2956 commit de90a05

File tree

1 file changed

+22
-34
lines changed

1 file changed

+22
-34
lines changed

gv.c

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,11 +2101,11 @@ S_gv_magicalize(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len,
21012101
S_maybe_add_coresub(aTHX_ 0, gv, name, len);
21022102
}
21032103
}
2104-
else if (stash != PL_defstash) { /* not the main stash */
2105-
/* We only have to check for a few names here: a, b, EXPORT, ISA
2106-
and VERSION. All the others apply only to the main stash or to
2107-
CORE (which is checked right after this). */
2104+
else if (len > 1) {
21082105
switch (*name) {
2106+
2107+
/* Each in first set doesn't require this to be the main stash */
2108+
21092109
case 'E':
21102110
if (
21112111
len >= 6 && name[1] == 'X' &&
@@ -2129,17 +2129,15 @@ S_gv_magicalize(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len,
21292129
GvMULTI_on(gv_AVadd(gv));
21302130
break;
21312131
}
2132-
/* FALLTHROUGH */
2133-
case 'b':
2134-
if (len == 1 && sv_type == SVt_PV)
2135-
GvMULTI_on(gv);
21362132
goto try_core;
21372133

21382134
default:
2135+
2136+
/* The remainder apply only to the main stash */
2137+
if (stash != PL_defstash) {
21392138
goto try_core;
21402139
}
2141-
}
2142-
else if (len > 1) {
2140+
21432141
switch (*name) {
21442142
case 'A':
21452143
if (memEQs(name, len, "ARGV")) {
@@ -2149,21 +2147,6 @@ S_gv_magicalize(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len,
21492147
GvMULTI_on(gv);
21502148
}
21512149
break;
2152-
case 'E':
2153-
if (
2154-
len >= 6 && name[1] == 'X' &&
2155-
(memEQs(name, len, "EXPORT")
2156-
||memEQs(name, len, "EXPORT_OK")
2157-
||memEQs(name, len, "EXPORT_FAIL")
2158-
||memEQs(name, len, "EXPORT_TAGS"))
2159-
)
2160-
GvMULTI_on(gv);
2161-
break;
2162-
case 'I':
2163-
if (memEQs(name, len, "ISA")) {
2164-
gv_magicalize_isa(gv);
2165-
}
2166-
break;
21672150
case 'S':
21682151
if (memEQs(name, len, "SIG")) {
21692152
HV *hv;
@@ -2195,10 +2178,6 @@ S_gv_magicalize(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len,
21952178
}
21962179
}
21972180
break;
2198-
case 'V':
2199-
if (memEQs(name, len, "VERSION"))
2200-
GvMULTI_on(gv);
2201-
break;
22022181
case '\003': /* $^CHILD_ERROR_NATIVE */
22032182
if (memEQs(name, len, "\003HILD_ERROR_NATIVE"))
22042183
goto magicalize;
@@ -2300,10 +2279,17 @@ S_gv_magicalize(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len,
23002279
paren = (SSize_t)(I32)uv;
23012280
goto storeparen;
23022281
}
2303-
}
2304-
} else {
2305-
/* Names of length 1. (Or 0. But name is NUL terminated, so that will
2306-
be case '\0' in this switch statement (ie a default case) */
2282+
}
2283+
}
2284+
}
2285+
else if ( stash == PL_defstash /* Names of length 1. */
2286+
|| *name == 'a' || *name == 'b')
2287+
{
2288+
/* All but the above two length 1 names have to be in the main stash.
2289+
*
2290+
* Note that nothing failing here can apply to CORE, because the
2291+
* minimum length (for things like 'uc') is 2. */
2292+
23072293
switch (*name) {
23082294
case '&': /* $& */
23092295
paren = RX_BUFF_IDX_FULLMATCH;
@@ -2458,10 +2444,12 @@ S_gv_magicalize(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len,
24582444
SvREFCNT_dec(sv);
24592445
}
24602446
break;
2461-
case 'a':
2447+
2448+
case 'a': /* The len > 1 case was handled above */
24622449
case 'b':
24632450
if (sv_type == SVt_PV)
24642451
GvMULTI_on(gv);
2452+
break;
24652453
}
24662454
}
24672455

0 commit comments

Comments
 (0)