Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* gv.c |
79072805 | 2 | * |
e6906430 | 3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
9e310c53 | 4 | * 2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others |
79072805 LW |
5 | * |
6 | * You may distribute under the terms of either the GNU General Public | |
7 | * License or the Artistic License, as specified in the README file. | |
8 | * | |
a0d0e21e LW |
9 | */ |
10 | ||
11 | /* | |
12 | * 'Mercy!' cried Gandalf. 'If the giving of information is to be the cure | |
13 | * of your inquisitiveness, I shall spend all the rest of my days answering | |
14 | * you. What more do you want to know?' | |
15 | * 'The names of all the stars, and of all living things, and the whole | |
16 | * history of Middle-earth and Over-heaven and of the Sundering Seas,' | |
17 | * laughed Pippin. | |
79072805 LW |
18 | */ |
19 | ||
ccfc67b7 JH |
20 | /* |
21 | =head1 GV Functions | |
40d34c0d SB |
22 | |
23 | A GV is a structure which corresponds to to a Perl typeglob, ie *foo. | |
24 | It is a structure that holds a pointer to a scalar, an array, a hash etc, | |
25 | corresponding to $foo, @foo, %foo. | |
26 | ||
27 | GVs are usually found as values in stashes (symbol table hashes) where | |
28 | Perl stores its global variables. | |
29 | ||
30 | =cut | |
ccfc67b7 JH |
31 | */ |
32 | ||
79072805 | 33 | #include "EXTERN.h" |
864dbfa3 | 34 | #define PERL_IN_GV_C |
79072805 LW |
35 | #include "perl.h" |
36 | ||
37 | GV * | |
864dbfa3 | 38 | Perl_gv_AVadd(pTHX_ register GV *gv) |
79072805 | 39 | { |
a0d0e21e | 40 | if (!gv || SvTYPE((SV*)gv) != SVt_PVGV) |
cea2e8a9 | 41 | Perl_croak(aTHX_ "Bad symbol for array"); |
79072805 LW |
42 | if (!GvAV(gv)) |
43 | GvAV(gv) = newAV(); | |
44 | return gv; | |
45 | } | |
46 | ||
47 | GV * | |
864dbfa3 | 48 | Perl_gv_HVadd(pTHX_ register GV *gv) |
79072805 | 49 | { |
a0d0e21e | 50 | if (!gv || SvTYPE((SV*)gv) != SVt_PVGV) |
cea2e8a9 | 51 | Perl_croak(aTHX_ "Bad symbol for hash"); |
79072805 | 52 | if (!GvHV(gv)) |
463ee0b2 | 53 | GvHV(gv) = newHV(); |
79072805 LW |
54 | return gv; |
55 | } | |
56 | ||
57 | GV * | |
864dbfa3 | 58 | Perl_gv_IOadd(pTHX_ register GV *gv) |
a0d0e21e LW |
59 | { |
60 | if (!gv || SvTYPE((SV*)gv) != SVt_PVGV) | |
cea2e8a9 | 61 | Perl_croak(aTHX_ "Bad symbol for filehandle"); |
5bd07a3d | 62 | if (!GvIOp(gv)) { |
7fb37951 AMS |
63 | #ifdef GV_UNIQUE_CHECK |
64 | if (GvUNIQUE(gv)) { | |
65 | Perl_croak(aTHX_ "Bad symbol for filehandle (GV is unique)"); | |
5bd07a3d DM |
66 | } |
67 | #endif | |
a0d0e21e | 68 | GvIOp(gv) = newIO(); |
5bd07a3d | 69 | } |
a0d0e21e LW |
70 | return gv; |
71 | } | |
72 | ||
73 | GV * | |
864dbfa3 | 74 | Perl_gv_fetchfile(pTHX_ const char *name) |
79072805 | 75 | { |
eacec437 | 76 | char smallbuf[256]; |
53d95988 | 77 | char *tmpbuf; |
8ebc5c01 | 78 | STRLEN tmplen; |
79072805 LW |
79 | GV *gv; |
80 | ||
1d7c1841 GS |
81 | if (!PL_defstash) |
82 | return Nullgv; | |
83 | ||
53d95988 CS |
84 | tmplen = strlen(name) + 2; |
85 | if (tmplen < sizeof smallbuf) | |
86 | tmpbuf = smallbuf; | |
87 | else | |
88 | New(603, tmpbuf, tmplen + 1, char); | |
0ac0412a | 89 | /* This is where the debugger's %{"::_<$filename"} hash is created */ |
53d95988 CS |
90 | tmpbuf[0] = '_'; |
91 | tmpbuf[1] = '<'; | |
92 | strcpy(tmpbuf + 2, name); | |
3280af22 | 93 | gv = *(GV**)hv_fetch(PL_defstash, tmpbuf, tmplen, TRUE); |
1d7c1841 | 94 | if (!isGV(gv)) { |
3280af22 | 95 | gv_init(gv, PL_defstash, tmpbuf, tmplen, FALSE); |
1d7c1841 GS |
96 | sv_setpv(GvSV(gv), name); |
97 | if (PERLDB_LINE) | |
14befaf4 | 98 | hv_magic(GvHVn(gv_AVadd(gv)), Nullgv, PERL_MAGIC_dbfile); |
1d7c1841 | 99 | } |
53d95988 CS |
100 | if (tmpbuf != smallbuf) |
101 | Safefree(tmpbuf); | |
79072805 LW |
102 | return gv; |
103 | } | |
104 | ||
463ee0b2 | 105 | void |
864dbfa3 | 106 | Perl_gv_init(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len, int multi) |
463ee0b2 LW |
107 | { |
108 | register GP *gp; | |
55d729e4 GS |
109 | bool doproto = SvTYPE(gv) > SVt_NULL; |
110 | char *proto = (doproto && SvPOK(gv)) ? SvPVX(gv) : NULL; | |
463ee0b2 | 111 | |
dc437b57 | 112 | sv_upgrade((SV*)gv, SVt_PVGV); |
55d729e4 GS |
113 | if (SvLEN(gv)) { |
114 | if (proto) { | |
0da6cfda | 115 | SvPV_set(gv, NULL); |
a8dc4fe8 | 116 | SvLEN_set(gv, 0); |
55d729e4 GS |
117 | SvPOK_off(gv); |
118 | } else | |
119 | Safefree(SvPVX(gv)); | |
120 | } | |
44a8e56a | 121 | Newz(602, gp, 1, GP); |
8990e307 | 122 | GvGP(gv) = gp_ref(gp); |
463ee0b2 | 123 | GvSV(gv) = NEWSV(72,0); |
1d7c1841 GS |
124 | GvLINE(gv) = CopLINE(PL_curcop); |
125 | GvFILE(gv) = CopFILE(PL_curcop) ? CopFILE(PL_curcop) : ""; | |
005a453c | 126 | GvCVGEN(gv) = 0; |
463ee0b2 | 127 | GvEGV(gv) = gv; |
14befaf4 | 128 | sv_magic((SV*)gv, (SV*)gv, PERL_MAGIC_glob, Nullch, 0); |
85aff577 | 129 | GvSTASH(gv) = (HV*)SvREFCNT_inc(stash); |
a0d0e21e | 130 | GvNAME(gv) = savepvn(name, len); |
463ee0b2 | 131 | GvNAMELEN(gv) = len; |
23ad5bf5 | 132 | if (multi || doproto) /* doproto means it _was_ mentioned */ |
a5f75d66 | 133 | GvMULTI_on(gv); |
55d729e4 | 134 | if (doproto) { /* Replicate part of newSUB here. */ |
57ff9a15 | 135 | SvIOK_off(gv); |
55d729e4 | 136 | ENTER; |
b099ddc0 | 137 | /* XXX unsafe for threads if eval_owner isn't held */ |
55d729e4 | 138 | start_subparse(0,0); /* Create CV in compcv. */ |
3280af22 | 139 | GvCV(gv) = PL_compcv; |
55d729e4 GS |
140 | LEAVE; |
141 | ||
3280af22 | 142 | PL_sub_generation++; |
65c50114 | 143 | CvGV(GvCV(gv)) = gv; |
e0d088d0 | 144 | CvFILE_set_from_cop(GvCV(gv), PL_curcop); |
3280af22 | 145 | CvSTASH(GvCV(gv)) = PL_curstash; |
4d1ff10f | 146 | #ifdef USE_5005THREADS |
55d729e4 | 147 | CvOWNER(GvCV(gv)) = 0; |
1cfa4ec7 | 148 | if (!CvMUTEXP(GvCV(gv))) { |
b0a484d2 | 149 | New(666, CvMUTEXP(GvCV(gv)), 1, perl_mutex); |
1cfa4ec7 GS |
150 | MUTEX_INIT(CvMUTEXP(GvCV(gv))); |
151 | } | |
4d1ff10f | 152 | #endif /* USE_5005THREADS */ |
55d729e4 GS |
153 | if (proto) { |
154 | sv_setpv((SV*)GvCV(gv), proto); | |
155 | Safefree(proto); | |
156 | } | |
157 | } | |
463ee0b2 LW |
158 | } |
159 | ||
76e3520e | 160 | STATIC void |
cea2e8a9 | 161 | S_gv_init_sv(pTHX_ GV *gv, I32 sv_type) |
a0d0e21e LW |
162 | { |
163 | switch (sv_type) { | |
164 | case SVt_PVIO: | |
165 | (void)GvIOn(gv); | |
166 | break; | |
167 | case SVt_PVAV: | |
168 | (void)GvAVn(gv); | |
169 | break; | |
170 | case SVt_PVHV: | |
171 | (void)GvHVn(gv); | |
172 | break; | |
173 | } | |
174 | } | |
175 | ||
954c1994 GS |
176 | /* |
177 | =for apidoc gv_fetchmeth | |
178 | ||
179 | Returns the glob with the given C<name> and a defined subroutine or | |
180 | C<NULL>. The glob lives in the given C<stash>, or in the stashes | |
07766739 | 181 | accessible via @ISA and UNIVERSAL::. |
954c1994 GS |
182 | |
183 | The argument C<level> should be either 0 or -1. If C<level==0>, as a | |
184 | side-effect creates a glob with the given C<name> in the given C<stash> | |
185 | which in the case of success contains an alias for the subroutine, and sets | |
b267980d | 186 | up caching info for this glob. Similarly for all the searched stashes. |
954c1994 GS |
187 | |
188 | This function grants C<"SUPER"> token as a postfix of the stash name. The | |
189 | GV returned from C<gv_fetchmeth> may be a method cache entry, which is not | |
4929bf7b | 190 | visible to Perl code. So when calling C<call_sv>, you should not use |
954c1994 | 191 | the GV directly; instead, you should use the method's CV, which can be |
b267980d | 192 | obtained from the GV with the C<GvCV> macro. |
954c1994 GS |
193 | |
194 | =cut | |
195 | */ | |
196 | ||
79072805 | 197 | GV * |
864dbfa3 | 198 | Perl_gv_fetchmeth(pTHX_ HV *stash, const char *name, STRLEN len, I32 level) |
79072805 LW |
199 | { |
200 | AV* av; | |
463ee0b2 | 201 | GV* topgv; |
79072805 | 202 | GV* gv; |
463ee0b2 | 203 | GV** gvp; |
748a9306 | 204 | CV* cv; |
a0d0e21e | 205 | |
af09ea45 IK |
206 | /* UNIVERSAL methods should be callable without a stash */ |
207 | if (!stash) { | |
208 | level = -1; /* probably appropriate */ | |
209 | if(!(stash = gv_stashpvn("UNIVERSAL", 9, FALSE))) | |
210 | return 0; | |
211 | } | |
212 | ||
3198e4e5 AV |
213 | if (!HvNAME(stash)) |
214 | Perl_croak(aTHX_ | |
215 | "Can't use anonymous symbol table for method lookup"); | |
216 | ||
44a8e56a | 217 | if ((level > 100) || (level < -100)) |
cea2e8a9 | 218 | Perl_croak(aTHX_ "Recursive inheritance detected while looking for method '%s' in package '%s'", |
3e0ccd42 | 219 | name, HvNAME(stash)); |
463ee0b2 | 220 | |
cea2e8a9 | 221 | DEBUG_o( Perl_deb(aTHX_ "Looking for method %s in package %s\n",name,HvNAME(stash)) ); |
44a8e56a | 222 | |
223 | gvp = (GV**)hv_fetch(stash, name, len, (level >= 0)); | |
224 | if (!gvp) | |
225 | topgv = Nullgv; | |
226 | else { | |
227 | topgv = *gvp; | |
228 | if (SvTYPE(topgv) != SVt_PVGV) | |
229 | gv_init(topgv, stash, name, len, TRUE); | |
155aba94 | 230 | if ((cv = GvCV(topgv))) { |
44a8e56a | 231 | /* If genuine method or valid cache entry, use it */ |
3280af22 | 232 | if (!GvCVGEN(topgv) || GvCVGEN(topgv) == PL_sub_generation) |
7a4c00b4 | 233 | return topgv; |
44a8e56a | 234 | /* Stale cached entry: junk it */ |
235 | SvREFCNT_dec(cv); | |
236 | GvCV(topgv) = cv = Nullcv; | |
237 | GvCVGEN(topgv) = 0; | |
748a9306 | 238 | } |
3280af22 | 239 | else if (GvCVGEN(topgv) == PL_sub_generation) |
005a453c | 240 | return 0; /* cache indicates sub doesn't exist */ |
463ee0b2 | 241 | } |
79072805 | 242 | |
9607fc9c | 243 | gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE); |
3280af22 | 244 | av = (gvp && (gv = *gvp) && gv != (GV*)&PL_sv_undef) ? GvAV(gv) : Nullav; |
9607fc9c | 245 | |
fb73857a | 246 | /* create and re-create @.*::SUPER::ISA on demand */ |
247 | if (!av || !SvMAGIC(av)) { | |
9607fc9c | 248 | char* packname = HvNAME(stash); |
249 | STRLEN packlen = strlen(packname); | |
250 | ||
251 | if (packlen >= 7 && strEQ(packname + packlen - 7, "::SUPER")) { | |
252 | HV* basestash; | |
253 | ||
254 | packlen -= 7; | |
255 | basestash = gv_stashpvn(packname, packlen, TRUE); | |
256 | gvp = (GV**)hv_fetch(basestash, "ISA", 3, FALSE); | |
3280af22 | 257 | if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) { |
9607fc9c | 258 | gvp = (GV**)hv_fetch(stash, "ISA", 3, TRUE); |
259 | if (!gvp || !(gv = *gvp)) | |
cea2e8a9 | 260 | Perl_croak(aTHX_ "Cannot create %s::ISA", HvNAME(stash)); |
9607fc9c | 261 | if (SvTYPE(gv) != SVt_PVGV) |
262 | gv_init(gv, stash, "ISA", 3, TRUE); | |
263 | SvREFCNT_dec(GvAV(gv)); | |
264 | GvAV(gv) = (AV*)SvREFCNT_inc(av); | |
265 | } | |
266 | } | |
267 | } | |
268 | ||
269 | if (av) { | |
79072805 | 270 | SV** svp = AvARRAY(av); |
93965878 NIS |
271 | /* NOTE: No support for tied ISA */ |
272 | I32 items = AvFILLp(av) + 1; | |
79072805 | 273 | while (items--) { |
79072805 | 274 | SV* sv = *svp++; |
a0d0e21e | 275 | HV* basestash = gv_stashsv(sv, FALSE); |
9bbf4081 | 276 | if (!basestash) { |
599cee73 | 277 | if (ckWARN(WARN_MISC)) |
c293eb2b NC |
278 | Perl_warner(aTHX_ packWARN(WARN_MISC), "Can't locate package %"SVf" for @%s::ISA", |
279 | sv, HvNAME(stash)); | |
79072805 LW |
280 | continue; |
281 | } | |
44a8e56a | 282 | gv = gv_fetchmeth(basestash, name, len, |
283 | (level >= 0) ? level + 1 : level - 1); | |
284 | if (gv) | |
285 | goto gotcha; | |
79072805 LW |
286 | } |
287 | } | |
a0d0e21e | 288 | |
9607fc9c | 289 | /* if at top level, try UNIVERSAL */ |
290 | ||
44a8e56a | 291 | if (level == 0 || level == -1) { |
9607fc9c | 292 | HV* lastchance; |
293 | ||
155aba94 GS |
294 | if ((lastchance = gv_stashpvn("UNIVERSAL", 9, FALSE))) { |
295 | if ((gv = gv_fetchmeth(lastchance, name, len, | |
296 | (level >= 0) ? level + 1 : level - 1))) | |
297 | { | |
44a8e56a | 298 | gotcha: |
dc848c6f | 299 | /* |
300 | * Cache method in topgv if: | |
301 | * 1. topgv has no synonyms (else inheritance crosses wires) | |
302 | * 2. method isn't a stub (else AUTOLOAD fails spectacularly) | |
303 | */ | |
304 | if (topgv && | |
305 | GvREFCNT(topgv) == 1 && | |
306 | (cv = GvCV(gv)) && | |
307 | (CvROOT(cv) || CvXSUB(cv))) | |
308 | { | |
155aba94 | 309 | if ((cv = GvCV(topgv))) |
44a8e56a | 310 | SvREFCNT_dec(cv); |
311 | GvCV(topgv) = (CV*)SvREFCNT_inc(GvCV(gv)); | |
3280af22 | 312 | GvCVGEN(topgv) = PL_sub_generation; |
44a8e56a | 313 | } |
a0d0e21e LW |
314 | return gv; |
315 | } | |
005a453c JP |
316 | else if (topgv && GvREFCNT(topgv) == 1) { |
317 | /* cache the fact that the method is not defined */ | |
3280af22 | 318 | GvCVGEN(topgv) = PL_sub_generation; |
005a453c | 319 | } |
a0d0e21e LW |
320 | } |
321 | } | |
322 | ||
79072805 LW |
323 | return 0; |
324 | } | |
325 | ||
954c1994 | 326 | /* |
611c1e95 IZ |
327 | =for apidoc gv_fetchmeth_autoload |
328 | ||
329 | Same as gv_fetchmeth(), but looks for autoloaded subroutines too. | |
330 | Returns a glob for the subroutine. | |
331 | ||
332 | For an autoloaded subroutine without a GV, will create a GV even | |
333 | if C<level < 0>. For an autoloaded subroutine without a stub, GvCV() | |
334 | of the result may be zero. | |
335 | ||
336 | =cut | |
337 | */ | |
338 | ||
339 | GV * | |
340 | Perl_gv_fetchmeth_autoload(pTHX_ HV *stash, const char *name, STRLEN len, I32 level) | |
341 | { | |
342 | GV *gv = gv_fetchmeth(stash, name, len, level); | |
343 | ||
344 | if (!gv) { | |
345 | char autoload[] = "AUTOLOAD"; | |
346 | STRLEN autolen = sizeof(autoload)-1; | |
347 | CV *cv; | |
348 | GV **gvp; | |
349 | ||
350 | if (!stash) | |
351 | return Nullgv; /* UNIVERSAL::AUTOLOAD could cause trouble */ | |
352 | if (len == autolen && strnEQ(name, autoload, autolen)) | |
353 | return Nullgv; | |
354 | if (!(gv = gv_fetchmeth(stash, autoload, autolen, FALSE))) | |
355 | return Nullgv; | |
356 | cv = GvCV(gv); | |
357 | if (!(CvROOT(cv) || CvXSUB(cv))) | |
358 | return Nullgv; | |
359 | /* Have an autoload */ | |
360 | if (level < 0) /* Cannot do without a stub */ | |
361 | gv_fetchmeth(stash, name, len, 0); | |
362 | gvp = (GV**)hv_fetch(stash, name, len, (level >= 0)); | |
363 | if (!gvp) | |
364 | return Nullgv; | |
365 | return *gvp; | |
366 | } | |
367 | return gv; | |
368 | } | |
369 | ||
370 | /* | |
954c1994 GS |
371 | =for apidoc gv_fetchmethod |
372 | ||
6d0f518e | 373 | See L<gv_fetchmethod_autoload>. |
954c1994 GS |
374 | |
375 | =cut | |
376 | */ | |
377 | ||
79072805 | 378 | GV * |
864dbfa3 | 379 | Perl_gv_fetchmethod(pTHX_ HV *stash, const char *name) |
463ee0b2 | 380 | { |
dc848c6f | 381 | return gv_fetchmethod_autoload(stash, name, TRUE); |
382 | } | |
383 | ||
954c1994 GS |
384 | /* |
385 | =for apidoc gv_fetchmethod_autoload | |
386 | ||
387 | Returns the glob which contains the subroutine to call to invoke the method | |
388 | on the C<stash>. In fact in the presence of autoloading this may be the | |
389 | glob for "AUTOLOAD". In this case the corresponding variable $AUTOLOAD is | |
b267980d | 390 | already setup. |
954c1994 GS |
391 | |
392 | The third parameter of C<gv_fetchmethod_autoload> determines whether | |
393 | AUTOLOAD lookup is performed if the given method is not present: non-zero | |
b267980d | 394 | means yes, look for AUTOLOAD; zero means no, don't look for AUTOLOAD. |
954c1994 | 395 | Calling C<gv_fetchmethod> is equivalent to calling C<gv_fetchmethod_autoload> |
b267980d | 396 | with a non-zero C<autoload> parameter. |
954c1994 GS |
397 | |
398 | These functions grant C<"SUPER"> token as a prefix of the method name. Note | |
399 | that if you want to keep the returned glob for a long time, you need to | |
400 | check for it being "AUTOLOAD", since at the later time the call may load a | |
401 | different subroutine due to $AUTOLOAD changing its value. Use the glob | |
b267980d | 402 | created via a side effect to do this. |
954c1994 GS |
403 | |
404 | These functions have the same side-effects and as C<gv_fetchmeth> with | |
405 | C<level==0>. C<name> should be writable if contains C<':'> or C<' | |
406 | ''>. The warning against passing the GV returned by C<gv_fetchmeth> to | |
b267980d | 407 | C<call_sv> apply equally to these functions. |
954c1994 GS |
408 | |
409 | =cut | |
410 | */ | |
411 | ||
dc848c6f | 412 | GV * |
864dbfa3 | 413 | Perl_gv_fetchmethod_autoload(pTHX_ HV *stash, const char *name, I32 autoload) |
dc848c6f | 414 | { |
08105a92 GS |
415 | register const char *nend; |
416 | const char *nsplit = 0; | |
a0d0e21e | 417 | GV* gv; |
c240c76d JH |
418 | HV* ostash = stash; |
419 | ||
420 | if (stash && SvTYPE(stash) < SVt_PVHV) | |
421 | stash = Nullhv; | |
b267980d | 422 | |
463ee0b2 | 423 | for (nend = name; *nend; nend++) { |
9607fc9c | 424 | if (*nend == '\'') |
a0d0e21e | 425 | nsplit = nend; |
9607fc9c | 426 | else if (*nend == ':' && *(nend + 1) == ':') |
427 | nsplit = ++nend; | |
a0d0e21e LW |
428 | } |
429 | if (nsplit) { | |
08105a92 | 430 | const char *origname = name; |
a0d0e21e | 431 | name = nsplit + 1; |
a0d0e21e LW |
432 | if (*nsplit == ':') |
433 | --nsplit; | |
9607fc9c | 434 | if ((nsplit - origname) == 5 && strnEQ(origname, "SUPER", 5)) { |
435 | /* ->SUPER::method should really be looked up in original stash */ | |
cea2e8a9 | 436 | SV *tmpstr = sv_2mortal(Perl_newSVpvf(aTHX_ "%s::SUPER", |
1d7c1841 | 437 | CopSTASHPV(PL_curcop))); |
af09ea45 | 438 | /* __PACKAGE__::SUPER stash should be autovivified */ |
9607fc9c | 439 | stash = gv_stashpvn(SvPVX(tmpstr), SvCUR(tmpstr), TRUE); |
cea2e8a9 | 440 | DEBUG_o( Perl_deb(aTHX_ "Treating %s as %s::%s\n", |
9607fc9c | 441 | origname, HvNAME(stash), name) ); |
4633a7c4 | 442 | } |
e189a56d | 443 | else { |
af09ea45 IK |
444 | /* don't autovifify if ->NoSuchStash::method */ |
445 | stash = gv_stashpvn(origname, nsplit - origname, FALSE); | |
e189a56d IK |
446 | |
447 | /* however, explicit calls to Pkg::SUPER::method may | |
448 | happen, and may require autovivification to work */ | |
449 | if (!stash && (nsplit - origname) >= 7 && | |
450 | strnEQ(nsplit - 7, "::SUPER", 7) && | |
451 | gv_stashpvn(origname, nsplit - origname - 7, FALSE)) | |
452 | stash = gv_stashpvn(origname, nsplit - origname, TRUE); | |
453 | } | |
c240c76d | 454 | ostash = stash; |
4633a7c4 LW |
455 | } |
456 | ||
9607fc9c | 457 | gv = gv_fetchmeth(stash, name, nend - name, 0); |
a0d0e21e | 458 | if (!gv) { |
2f6e0fe7 | 459 | if (strEQ(name,"import") || strEQ(name,"unimport")) |
3280af22 | 460 | gv = (GV*)&PL_sv_yes; |
dc848c6f | 461 | else if (autoload) |
c240c76d | 462 | gv = gv_autoload4(ostash, name, nend - name, TRUE); |
463ee0b2 | 463 | } |
dc848c6f | 464 | else if (autoload) { |
465 | CV* cv = GvCV(gv); | |
09280a33 CS |
466 | if (!CvROOT(cv) && !CvXSUB(cv)) { |
467 | GV* stubgv; | |
468 | GV* autogv; | |
469 | ||
470 | if (CvANON(cv)) | |
471 | stubgv = gv; | |
472 | else { | |
473 | stubgv = CvGV(cv); | |
474 | if (GvCV(stubgv) != cv) /* orphaned import */ | |
475 | stubgv = gv; | |
476 | } | |
477 | autogv = gv_autoload4(GvSTASH(stubgv), | |
478 | GvNAME(stubgv), GvNAMELEN(stubgv), TRUE); | |
dc848c6f | 479 | if (autogv) |
480 | gv = autogv; | |
481 | } | |
482 | } | |
44a8e56a | 483 | |
484 | return gv; | |
485 | } | |
486 | ||
487 | GV* | |
864dbfa3 | 488 | Perl_gv_autoload4(pTHX_ HV *stash, const char *name, STRLEN len, I32 method) |
44a8e56a | 489 | { |
df3728a2 JH |
490 | char autoload[] = "AUTOLOAD"; |
491 | STRLEN autolen = sizeof(autoload)-1; | |
44a8e56a | 492 | GV* gv; |
493 | CV* cv; | |
494 | HV* varstash; | |
495 | GV* vargv; | |
496 | SV* varsv; | |
c240c76d | 497 | char *packname = ""; |
44a8e56a | 498 | |
499 | if (len == autolen && strnEQ(name, autoload, autolen)) | |
500 | return Nullgv; | |
c240c76d JH |
501 | if (stash) { |
502 | if (SvTYPE(stash) < SVt_PVHV) { | |
503 | packname = SvPV_nolen((SV*)stash); | |
504 | stash = Nullhv; | |
505 | } | |
506 | else { | |
507 | packname = HvNAME(stash); | |
508 | } | |
509 | } | |
dc848c6f | 510 | if (!(gv = gv_fetchmeth(stash, autoload, autolen, FALSE))) |
511 | return Nullgv; | |
512 | cv = GvCV(gv); | |
513 | ||
adb5a9ae | 514 | if (!(CvROOT(cv) || CvXSUB(cv))) |
ed850460 JH |
515 | return Nullgv; |
516 | ||
dc848c6f | 517 | /* |
518 | * Inheriting AUTOLOAD for non-methods works ... for now. | |
519 | */ | |
12bcd1a6 | 520 | if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX) && !method && |
599cee73 | 521 | (GvCVGEN(gv) || GvSTASH(gv) != stash)) |
12bcd1a6 | 522 | Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX), |
dc848c6f | 523 | "Use of inherited AUTOLOAD for non-method %s::%.*s() is deprecated", |
c240c76d | 524 | packname, (int)len, name); |
44a8e56a | 525 | |
4d1ff10f | 526 | #ifndef USE_5005THREADS |
adb5a9ae DM |
527 | if (CvXSUB(cv)) { |
528 | /* rather than lookup/init $AUTOLOAD here | |
529 | * only to have the XSUB do another lookup for $AUTOLOAD | |
530 | * and split that value on the last '::', | |
531 | * pass along the same data via some unused fields in the CV | |
532 | */ | |
533 | CvSTASH(cv) = stash; | |
0da6cfda | 534 | SvPV_set(cv, (char *)name); /* cast to lose constness warning */ |
a8dc4fe8 | 535 | SvCUR_set(cv, len); |
adb5a9ae DM |
536 | return gv; |
537 | } | |
538 | #endif | |
539 | ||
44a8e56a | 540 | /* |
541 | * Given &FOO::AUTOLOAD, set $FOO::AUTOLOAD to desired function name. | |
542 | * The subroutine's original name may not be "AUTOLOAD", so we don't | |
543 | * use that, but for lack of anything better we will use the sub's | |
544 | * original package to look up $AUTOLOAD. | |
545 | */ | |
546 | varstash = GvSTASH(CvGV(cv)); | |
547 | vargv = *(GV**)hv_fetch(varstash, autoload, autolen, TRUE); | |
3d35f11b GS |
548 | ENTER; |
549 | ||
4d1ff10f | 550 | #ifdef USE_5005THREADS |
4755096e | 551 | sv_lock((SV *)varstash); |
3d35f11b | 552 | #endif |
44a8e56a | 553 | if (!isGV(vargv)) |
554 | gv_init(vargv, varstash, autoload, autolen, FALSE); | |
3d35f11b | 555 | LEAVE; |
44a8e56a | 556 | varsv = GvSV(vargv); |
4d1ff10f | 557 | #ifdef USE_5005THREADS |
4755096e | 558 | sv_lock(varsv); |
3d35f11b | 559 | #endif |
c240c76d | 560 | sv_setpv(varsv, packname); |
44a8e56a | 561 | sv_catpvn(varsv, "::", 2); |
562 | sv_catpvn(varsv, name, len); | |
563 | SvTAINTED_off(varsv); | |
a0d0e21e LW |
564 | return gv; |
565 | } | |
566 | ||
d2c93421 RH |
567 | /* The "gv" parameter should be the glob known to Perl code as *! |
568 | * The scalar must already have been magicalized. | |
569 | */ | |
570 | STATIC void | |
571 | S_require_errno(pTHX_ GV *gv) | |
572 | { | |
573 | HV* stash = gv_stashpvn("Errno",5,FALSE); | |
574 | ||
575 | if (!stash || !(gv_fetchmethod(stash, "TIEHASH"))) { | |
576 | dSP; | |
577 | PUTBACK; | |
578 | ENTER; | |
579 | save_scalar(gv); /* keep the value of $! */ | |
84f1fa11 AMS |
580 | Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, |
581 | newSVpvn("Errno",5), Nullsv); | |
d2c93421 RH |
582 | LEAVE; |
583 | SPAGAIN; | |
584 | stash = gv_stashpvn("Errno",5,FALSE); | |
585 | if (!stash || !(gv_fetchmethod(stash, "TIEHASH"))) | |
586 | Perl_croak(aTHX_ "Can't use %%! because Errno.pm is not available"); | |
587 | } | |
588 | } | |
589 | ||
954c1994 GS |
590 | /* |
591 | =for apidoc gv_stashpv | |
592 | ||
386d01d6 | 593 | Returns a pointer to the stash for a specified package. C<name> should |
0df18620 NC |
594 | be a valid UTF-8 string and must be null-terminated. If C<create> is set |
595 | then the package will be created if it does not already exist. If C<create> | |
596 | is not set and the package does not exist then NULL is returned. | |
954c1994 GS |
597 | |
598 | =cut | |
599 | */ | |
600 | ||
a0d0e21e | 601 | HV* |
864dbfa3 | 602 | Perl_gv_stashpv(pTHX_ const char *name, I32 create) |
a0d0e21e | 603 | { |
dc437b57 | 604 | return gv_stashpvn(name, strlen(name), create); |
605 | } | |
606 | ||
0df18620 NC |
607 | /* |
608 | =for apidoc gv_stashpvn | |
609 | ||
610 | Returns a pointer to the stash for a specified package. C<name> should | |
611 | be a valid UTF-8 string. The C<namelen> parameter indicates the length of | |
612 | the C<name>, in bytes. If C<create> is set then the package will be | |
613 | created if it does not already exist. If C<create> is not set and the | |
614 | package does not exist then NULL is returned. | |
615 | ||
616 | =cut | |
617 | */ | |
618 | ||
dc437b57 | 619 | HV* |
864dbfa3 | 620 | Perl_gv_stashpvn(pTHX_ const char *name, U32 namelen, I32 create) |
dc437b57 | 621 | { |
46fc3d4c | 622 | char smallbuf[256]; |
623 | char *tmpbuf; | |
a0d0e21e LW |
624 | HV *stash; |
625 | GV *tmpgv; | |
dc437b57 | 626 | |
46fc3d4c | 627 | if (namelen + 3 < sizeof smallbuf) |
628 | tmpbuf = smallbuf; | |
629 | else | |
630 | New(606, tmpbuf, namelen + 3, char); | |
dc437b57 | 631 | Copy(name,tmpbuf,namelen,char); |
632 | tmpbuf[namelen++] = ':'; | |
633 | tmpbuf[namelen++] = ':'; | |
634 | tmpbuf[namelen] = '\0'; | |
46fc3d4c | 635 | tmpgv = gv_fetchpv(tmpbuf, create, SVt_PVHV); |
636 | if (tmpbuf != smallbuf) | |
637 | Safefree(tmpbuf); | |
a0d0e21e LW |
638 | if (!tmpgv) |
639 | return 0; | |
640 | if (!GvHV(tmpgv)) | |
641 | GvHV(tmpgv) = newHV(); | |
642 | stash = GvHV(tmpgv); | |
643 | if (!HvNAME(stash)) | |
644 | HvNAME(stash) = savepv(name); | |
645 | return stash; | |
463ee0b2 LW |
646 | } |
647 | ||
954c1994 GS |
648 | /* |
649 | =for apidoc gv_stashsv | |
650 | ||
386d01d6 GS |
651 | Returns a pointer to the stash for a specified package, which must be a |
652 | valid UTF-8 string. See C<gv_stashpv>. | |
954c1994 GS |
653 | |
654 | =cut | |
655 | */ | |
656 | ||
a0d0e21e | 657 | HV* |
864dbfa3 | 658 | Perl_gv_stashsv(pTHX_ SV *sv, I32 create) |
a0d0e21e | 659 | { |
dc437b57 | 660 | register char *ptr; |
661 | STRLEN len; | |
662 | ptr = SvPV(sv,len); | |
663 | return gv_stashpvn(ptr, len, create); | |
a0d0e21e LW |
664 | } |
665 | ||
666 | ||
463ee0b2 | 667 | GV * |
864dbfa3 | 668 | Perl_gv_fetchpv(pTHX_ const char *nambeg, I32 add, I32 sv_type) |
79072805 | 669 | { |
08105a92 | 670 | register const char *name = nambeg; |
463ee0b2 | 671 | register GV *gv = 0; |
79072805 | 672 | GV**gvp; |
79072805 | 673 | I32 len; |
08105a92 | 674 | register const char *namend; |
463ee0b2 | 675 | HV *stash = 0; |
79072805 | 676 | |
c07a80fd | 677 | if (*name == '*' && isALPHA(name[1])) /* accidental stringify on a GV? */ |
678 | name++; | |
679 | ||
79072805 | 680 | for (namend = name; *namend; namend++) { |
1d7c1841 GS |
681 | if ((*namend == ':' && namend[1] == ':') |
682 | || (*namend == '\'' && namend[1])) | |
463ee0b2 | 683 | { |
463ee0b2 | 684 | if (!stash) |
3280af22 | 685 | stash = PL_defstash; |
dc437b57 | 686 | if (!stash || !SvREFCNT(stash)) /* symbol table under destruction */ |
a0d0e21e | 687 | return Nullgv; |
463ee0b2 | 688 | |
85e6fe83 LW |
689 | len = namend - name; |
690 | if (len > 0) { | |
3c78fafa | 691 | char smallbuf[256]; |
62b57502 | 692 | char *tmpbuf; |
62b57502 | 693 | |
25c09a70 | 694 | if (len + 3 < sizeof (smallbuf)) |
3c78fafa | 695 | tmpbuf = smallbuf; |
62b57502 MB |
696 | else |
697 | New(601, tmpbuf, len+3, char); | |
a0d0e21e LW |
698 | Copy(name, tmpbuf, len, char); |
699 | tmpbuf[len++] = ':'; | |
700 | tmpbuf[len++] = ':'; | |
701 | tmpbuf[len] = '\0'; | |
463ee0b2 | 702 | gvp = (GV**)hv_fetch(stash,tmpbuf,len,add); |
6fa846a0 | 703 | gv = gvp ? *gvp : Nullgv; |
3280af22 | 704 | if (gv && gv != (GV*)&PL_sv_undef) { |
6fa846a0 | 705 | if (SvTYPE(gv) != SVt_PVGV) |
0f303493 | 706 | gv_init(gv, stash, tmpbuf, len, (add & GV_ADDMULTI)); |
6fa846a0 GS |
707 | else |
708 | GvMULTI_on(gv); | |
709 | } | |
3c78fafa | 710 | if (tmpbuf != smallbuf) |
62b57502 | 711 | Safefree(tmpbuf); |
3280af22 | 712 | if (!gv || gv == (GV*)&PL_sv_undef) |
a0d0e21e | 713 | return Nullgv; |
85e6fe83 | 714 | |
463ee0b2 LW |
715 | if (!(stash = GvHV(gv))) |
716 | stash = GvHV(gv) = newHV(); | |
85e6fe83 | 717 | |
463ee0b2 | 718 | if (!HvNAME(stash)) |
a0d0e21e | 719 | HvNAME(stash) = savepvn(nambeg, namend - nambeg); |
463ee0b2 LW |
720 | } |
721 | ||
722 | if (*namend == ':') | |
723 | namend++; | |
724 | namend++; | |
725 | name = namend; | |
726 | if (!*name) | |
3280af22 | 727 | return gv ? gv : (GV*)*hv_fetch(PL_defstash, "main::", 6, TRUE); |
79072805 | 728 | } |
79072805 | 729 | } |
a0d0e21e | 730 | len = namend - name; |
463ee0b2 LW |
731 | |
732 | /* No stash in name, so see how we can default */ | |
733 | ||
734 | if (!stash) { | |
7e2040f0 | 735 | if (isIDFIRST_lazy(name)) { |
9607fc9c | 736 | bool global = FALSE; |
737 | ||
9e310c53 NC |
738 | /* name is always \0 terminated, and initial \0 wouldn't return |
739 | true from isIDFIRST_lazy, so we know that name[1] is defined */ | |
740 | switch (name[1]) { | |
741 | case '\0': | |
742 | if (*name == '_') | |
9d116dd7 | 743 | global = TRUE; |
9e310c53 NC |
744 | break; |
745 | case 'N': | |
746 | if (strEQ(name, "INC") || strEQ(name, "ENV")) | |
9d116dd7 | 747 | global = TRUE; |
9e310c53 NC |
748 | break; |
749 | case 'I': | |
750 | if (strEQ(name, "SIG")) | |
9d116dd7 | 751 | global = TRUE; |
9e310c53 NC |
752 | break; |
753 | case 'T': | |
754 | if (strEQ(name, "STDIN") || strEQ(name, "STDOUT") || | |
755 | strEQ(name, "STDERR")) | |
463ee0b2 | 756 | global = TRUE; |
9e310c53 NC |
757 | break; |
758 | case 'R': | |
759 | if (strEQ(name, "ARGV") || strEQ(name, "ARGVOUT")) | |
760 | global = TRUE; | |
761 | break; | |
463ee0b2 | 762 | } |
9607fc9c | 763 | |
463ee0b2 | 764 | if (global) |
3280af22 | 765 | stash = PL_defstash; |
ef7b71f0 | 766 | else if (IN_PERL_COMPILETIME) { |
3280af22 NIS |
767 | stash = PL_curstash; |
768 | if (add && (PL_hints & HINT_STRICT_VARS) && | |
748a9306 LW |
769 | sv_type != SVt_PVCV && |
770 | sv_type != SVt_PVGV && | |
4633a7c4 | 771 | sv_type != SVt_PVFM && |
c07a80fd | 772 | sv_type != SVt_PVIO && |
3c71ae1e NC |
773 | !(len == 1 && sv_type == SVt_PV && |
774 | (*name == 'a' || *name == 'b')) ) | |
748a9306 | 775 | { |
4633a7c4 LW |
776 | gvp = (GV**)hv_fetch(stash,name,len,0); |
777 | if (!gvp || | |
3280af22 | 778 | *gvp == (GV*)&PL_sv_undef || |
a5f75d66 AD |
779 | SvTYPE(*gvp) != SVt_PVGV) |
780 | { | |
4633a7c4 | 781 | stash = 0; |
a5f75d66 | 782 | } |
155aba94 GS |
783 | else if ((sv_type == SVt_PV && !GvIMPORTED_SV(*gvp)) || |
784 | (sv_type == SVt_PVAV && !GvIMPORTED_AV(*gvp)) || | |
785 | (sv_type == SVt_PVHV && !GvIMPORTED_HV(*gvp)) ) | |
4633a7c4 | 786 | { |
cea2e8a9 | 787 | Perl_warn(aTHX_ "Variable \"%c%s\" is not imported", |
4633a7c4 LW |
788 | sv_type == SVt_PVAV ? '@' : |
789 | sv_type == SVt_PVHV ? '%' : '$', | |
790 | name); | |
8ebc5c01 | 791 | if (GvCVu(*gvp)) |
cc507455 | 792 | Perl_warn(aTHX_ "\t(Did you mean &%s instead?)\n", name); |
a0d0e21e | 793 | stash = 0; |
4633a7c4 | 794 | } |
a0d0e21e | 795 | } |
85e6fe83 | 796 | } |
463ee0b2 | 797 | else |
1d7c1841 | 798 | stash = CopSTASH(PL_curcop); |
463ee0b2 LW |
799 | } |
800 | else | |
3280af22 | 801 | stash = PL_defstash; |
463ee0b2 LW |
802 | } |
803 | ||
804 | /* By this point we should have a stash and a name */ | |
805 | ||
a0d0e21e | 806 | if (!stash) { |
5a844595 | 807 | if (add) { |
8f34e12f | 808 | register SV *err = Perl_mess(aTHX_ |
5a844595 GS |
809 | "Global symbol \"%s%s\" requires explicit package name", |
810 | (sv_type == SVt_PV ? "$" | |
811 | : sv_type == SVt_PVAV ? "@" | |
812 | : sv_type == SVt_PVHV ? "%" | |
8f34e12f JH |
813 | : ""), name); |
814 | if (USE_UTF8_IN_NAMES) | |
815 | SvUTF8_on(err); | |
816 | qerror(err); | |
f180df80 | 817 | stash = PL_nullstash; |
a0d0e21e | 818 | } |
f180df80 GS |
819 | else |
820 | return Nullgv; | |
a0d0e21e LW |
821 | } |
822 | ||
823 | if (!SvREFCNT(stash)) /* symbol table under destruction */ | |
824 | return Nullgv; | |
825 | ||
79072805 | 826 | gvp = (GV**)hv_fetch(stash,name,len,add); |
3280af22 | 827 | if (!gvp || *gvp == (GV*)&PL_sv_undef) |
79072805 LW |
828 | return Nullgv; |
829 | gv = *gvp; | |
830 | if (SvTYPE(gv) == SVt_PVGV) { | |
a0d0e21e | 831 | if (add) { |
a5f75d66 | 832 | GvMULTI_on(gv); |
a0d0e21e | 833 | gv_init_sv(gv, sv_type); |
d2c93421 RH |
834 | if (*name=='!' && sv_type == SVt_PVHV && len==1) |
835 | require_errno(gv); | |
a0d0e21e | 836 | } |
79072805 | 837 | return gv; |
55d729e4 GS |
838 | } else if (add & GV_NOINIT) { |
839 | return gv; | |
79072805 | 840 | } |
93a17b20 LW |
841 | |
842 | /* Adding a new symbol */ | |
843 | ||
0453d815 | 844 | if (add & GV_ADDWARN && ckWARN_d(WARN_INTERNAL)) |
9014280d | 845 | Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "Had to create %s unexpectedly", nambeg); |
55d729e4 | 846 | gv_init(gv, stash, name, len, add & GV_ADDMULTI); |
a0d0e21e | 847 | gv_init_sv(gv, sv_type); |
93a17b20 | 848 | |
7272584d PM |
849 | if (isALPHA(name[0]) && ! (isLEXWARN_on ? ckWARN(WARN_ONCE) |
850 | : (PL_dowarn & G_WARN_ON ) ) ) | |
0453d815 PM |
851 | GvMULTI_on(gv) ; |
852 | ||
93a17b20 | 853 | /* set up magic where warranted */ |
9e310c53 NC |
854 | if (len > 1) { |
855 | #ifndef EBCDIC | |
856 | if (*name > 'V' ) { | |
857 | /* Nothing else to do. | |
858 | The compiler will probably turn the switch statement into a | |
859 | branch table. Make sure we avoid even that small overhead for | |
860 | the common case of lower case variable names. */ | |
861 | } else | |
862 | #endif | |
863 | { | |
864 | const char *name2 = name + 1; | |
865 | switch (*name) { | |
866 | case 'A': | |
867 | if (strEQ(name2, "RGV")) { | |
868 | IoFLAGS(GvIOn(gv)) |= IOf_ARGV|IOf_START; | |
869 | } | |
870 | break; | |
871 | case 'E': | |
872 | if (strnEQ(name2, "XPORT", 5)) | |
873 | GvMULTI_on(gv); | |
874 | break; | |
875 | case 'I': | |
876 | if (strEQ(name2, "SA")) { | |
877 | AV* av = GvAVn(gv); | |
878 | GvMULTI_on(gv); | |
879 | sv_magic((SV*)av, (SV*)gv, PERL_MAGIC_isa, Nullch, 0); | |
880 | /* NOTE: No support for tied ISA */ | |
881 | if ((add & GV_ADDMULTI) && strEQ(nambeg,"AnyDBM_File::ISA") | |
882 | && AvFILLp(av) == -1) | |
883 | { | |
884 | char *pname; | |
885 | av_push(av, newSVpvn(pname = "NDBM_File",9)); | |
886 | gv_stashpvn(pname, 9, TRUE); | |
887 | av_push(av, newSVpvn(pname = "DB_File",7)); | |
888 | gv_stashpvn(pname, 7, TRUE); | |
889 | av_push(av, newSVpvn(pname = "GDBM_File",9)); | |
890 | gv_stashpvn(pname, 9, TRUE); | |
891 | av_push(av, newSVpvn(pname = "SDBM_File",9)); | |
892 | gv_stashpvn(pname, 9, TRUE); | |
893 | av_push(av, newSVpvn(pname = "ODBM_File",9)); | |
894 | gv_stashpvn(pname, 9, TRUE); | |
895 | } | |
896 | } | |
897 | break; | |
898 | case 'O': | |
899 | if (strEQ(name2, "VERLOAD")) { | |
900 | HV* hv = GvHVn(gv); | |
901 | GvMULTI_on(gv); | |
902 | hv_magic(hv, Nullgv, PERL_MAGIC_overload); | |
903 | } | |
904 | break; | |
905 | case 'S': | |
906 | if (strEQ(name2, "IG")) { | |
907 | HV *hv; | |
908 | I32 i; | |
909 | if (!PL_psig_ptr) { | |
910 | Newz(73, PL_psig_ptr, SIG_SIZE, SV*); | |
911 | Newz(73, PL_psig_name, SIG_SIZE, SV*); | |
912 | Newz(73, PL_psig_pend, SIG_SIZE, int); | |
913 | } | |
914 | GvMULTI_on(gv); | |
915 | hv = GvHVn(gv); | |
916 | hv_magic(hv, Nullgv, PERL_MAGIC_sig); | |
917 | for (i = 1; i < SIG_SIZE; i++) { | |
918 | SV ** init; | |
919 | init = hv_fetch(hv, PL_sig_name[i], strlen(PL_sig_name[i]), 1); | |
920 | if (init) | |
921 | sv_setsv(*init, &PL_sv_undef); | |
922 | PL_psig_ptr[i] = 0; | |
923 | PL_psig_name[i] = 0; | |
924 | PL_psig_pend[i] = 0; | |
925 | } | |
926 | } | |
927 | break; | |
928 | case 'V': | |
929 | if (strEQ(name2, "ERSION")) | |
930 | GvMULTI_on(gv); | |
931 | break; | |
932 | case '\005': /* $^ENCODING */ | |
933 | if (strEQ(name2, "NCODING")) | |
934 | goto magicalize; | |
935 | break; | |
936 | case '\017': /* $^OPEN */ | |
937 | if (strEQ(name2, "PEN")) | |
938 | goto magicalize; | |
939 | break; | |
940 | case '\024': /* ${^TAINT} */ | |
941 | if (strEQ(name2, "AINT")) | |
942 | goto ro_magicalize; | |
943 | break; | |
4c9c322d | 944 | case '\025': /* ${^UNICODE}, ${^UTF8LOCALE} */ |
9e310c53 NC |
945 | if (strEQ(name2, "NICODE")) |
946 | goto ro_magicalize; | |
4c9c322d NC |
947 | if (strEQ(name2, "TF8LOCALE")) |
948 | goto ro_magicalize; | |
9e310c53 NC |
949 | break; |
950 | case '\027': /* $^WARNING_BITS */ | |
951 | if (strEQ(name2, "ARNING_BITS")) | |
952 | goto magicalize; | |
953 | break; | |
954 | case '1': | |
955 | case '2': | |
956 | case '3': | |
957 | case '4': | |
958 | case '5': | |
959 | case '6': | |
960 | case '7': | |
961 | case '8': | |
962 | case '9': | |
85e6fe83 | 963 | { |
9e310c53 NC |
964 | /* ensures variable is only digits */ |
965 | /* ${"1foo"} fails this test (and is thus writeable) */ | |
966 | /* added by japhy, but borrowed from is_gv_magical */ | |
967 | const char *end = name + len; | |
968 | while (--end > name) { | |
969 | if (!isDIGIT(*end)) return gv; | |
970 | } | |
971 | goto ro_magicalize; | |
1d7c1841 | 972 | } |
dc437b57 | 973 | } |
93a17b20 | 974 | } |
9e310c53 NC |
975 | } else { |
976 | /* Names of length 1. (Or 0. But name is NUL terminated, so that will | |
977 | be case '\0' in this switch statement (ie a default case) */ | |
978 | switch (*name) { | |
979 | case '&': | |
980 | case '`': | |
981 | case '\'': | |
982 | if ( | |
983 | sv_type == SVt_PVAV || | |
984 | sv_type == SVt_PVHV || | |
985 | sv_type == SVt_PVCV || | |
986 | sv_type == SVt_PVFM || | |
987 | sv_type == SVt_PVIO | |
988 | ) { break; } | |
989 | PL_sawampersand = TRUE; | |
990 | goto ro_magicalize; | |
991 | ||
992 | case ':': | |
993 | sv_setpv(GvSV(gv),PL_chopset); | |
994 | goto magicalize; | |
995 | ||
996 | case '?': | |
ff0cee69 | 997 | #ifdef COMPLEX_STATUS |
9e310c53 | 998 | (void)SvUPGRADE(GvSV(gv), SVt_PVLV); |
ff0cee69 | 999 | #endif |
9e310c53 | 1000 | goto magicalize; |
ff0cee69 | 1001 | |
9e310c53 | 1002 | case '!': |
d2c93421 | 1003 | |
9e310c53 NC |
1004 | /* If %! has been used, automatically load Errno.pm. |
1005 | The require will itself set errno, so in order to | |
1006 | preserve its value we have to set up the magic | |
1007 | now (rather than going to magicalize) | |
1008 | */ | |
d2c93421 | 1009 | |
9e310c53 | 1010 | sv_magic(GvSV(gv), (SV*)gv, PERL_MAGIC_sv, name, len); |
d2c93421 | 1011 | |
9e310c53 NC |
1012 | if (sv_type == SVt_PVHV) |
1013 | require_errno(gv); | |
d2c93421 | 1014 | |
6cef1e77 | 1015 | break; |
9e310c53 NC |
1016 | case '-': |
1017 | { | |
6cef1e77 | 1018 | AV* av = GvAVn(gv); |
14befaf4 | 1019 | sv_magic((SV*)av, Nullsv, PERL_MAGIC_regdata, Nullch, 0); |
03a27ae7 | 1020 | SvREADONLY_on(av); |
9e310c53 NC |
1021 | goto magicalize; |
1022 | } | |
1023 | case '#': | |
1024 | case '*': | |
1025 | if (sv_type == SVt_PV && ckWARN2(WARN_DEPRECATED, WARN_SYNTAX)) | |
1026 | Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX), | |
1027 | "Use of $%s is deprecated", name); | |
1028 | goto magicalize; | |
1029 | case '|': | |
1030 | sv_setiv(GvSV(gv), (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0); | |
1031 | goto magicalize; | |
1032 | ||
1033 | case '+': | |
1034 | { | |
1035 | AV* av = GvAVn(gv); | |
14befaf4 | 1036 | sv_magic((SV*)av, (SV*)av, PERL_MAGIC_regdata, Nullch, 0); |
03a27ae7 | 1037 | SvREADONLY_on(av); |
9e310c53 | 1038 | /* FALL THROUGH */ |
e521374c | 1039 | } |
9e310c53 NC |
1040 | case '\023': /* $^S */ |
1041 | case '1': | |
1042 | case '2': | |
1043 | case '3': | |
1044 | case '4': | |
1045 | case '5': | |
1046 | case '6': | |
1047 | case '7': | |
1048 | case '8': | |
1049 | case '9': | |
1050 | ro_magicalize: | |
1051 | SvREADONLY_on(GvSV(gv)); | |
1052 | /* FALL THROUGH */ | |
1053 | case '[': | |
1054 | case '^': | |
1055 | case '~': | |
1056 | case '=': | |
1057 | case '%': | |
1058 | case '.': | |
1059 | case '(': | |
1060 | case ')': | |
1061 | case '<': | |
1062 | case '>': | |
1063 | case ',': | |
1064 | case '\\': | |
1065 | case '/': | |
1066 | case '\001': /* $^A */ | |
1067 | case '\003': /* $^C */ | |
1068 | case '\004': /* $^D */ | |
1069 | case '\005': /* $^E */ | |
1070 | case '\006': /* $^F */ | |
1071 | case '\010': /* $^H */ | |
1072 | case '\011': /* $^I, NOT \t in EBCDIC */ | |
1073 | case '\016': /* $^N */ | |
1074 | case '\017': /* $^O */ | |
1075 | case '\020': /* $^P */ | |
1076 | case '\024': /* $^T */ | |
1077 | case '\027': /* $^W */ | |
1078 | magicalize: | |
1079 | sv_magic(GvSV(gv), (SV*)gv, PERL_MAGIC_sv, name, len); | |
1080 | break; | |
e521374c | 1081 | |
9e310c53 | 1082 | case '\014': /* $^L */ |
2a8de9e2 | 1083 | sv_setpvn(GvSV(gv),"\f",1); |
9e310c53 | 1084 | PL_formfeed = GvSV(gv); |
463ee0b2 | 1085 | break; |
9e310c53 | 1086 | case ';': |
2a8de9e2 | 1087 | sv_setpvn(GvSV(gv),"\034",1); |
463ee0b2 | 1088 | break; |
9e310c53 NC |
1089 | case ']': |
1090 | { | |
f86702cc | 1091 | SV *sv = GvSV(gv); |
5089c844 | 1092 | (void)SvUPGRADE(sv, SVt_PVNV); |
6a6ba966 SB |
1093 | Perl_sv_setpvf(aTHX_ sv, |
1094 | #if defined(PERL_SUBVERSION) && (PERL_SUBVERSION > 0) | |
1095 | "%8.6" | |
1096 | #else | |
1097 | "%5.3" | |
1098 | #endif | |
1099 | NVff, | |
1100 | SvNVX(PL_patchlevel)); | |
5089c844 GS |
1101 | SvNVX(sv) = SvNVX(PL_patchlevel); |
1102 | SvNOK_on(sv); | |
5089c844 | 1103 | SvREADONLY_on(sv); |
93a17b20 LW |
1104 | } |
1105 | break; | |
9e310c53 NC |
1106 | case '\026': /* $^V */ |
1107 | { | |
7120cae1 | 1108 | SV * const sv = GvSV(gv); |
16070b82 GS |
1109 | GvSV(gv) = SvREFCNT_inc(PL_patchlevel); |
1110 | SvREFCNT_dec(sv); | |
1111 | } | |
1112 | break; | |
9e310c53 | 1113 | } |
79072805 | 1114 | } |
93a17b20 | 1115 | return gv; |
79072805 LW |
1116 | } |
1117 | ||
1118 | void | |
43693395 GS |
1119 | Perl_gv_fullname4(pTHX_ SV *sv, GV *gv, const char *prefix, bool keepmain) |
1120 | { | |
7120cae1 AL |
1121 | const char *name; |
1122 | const HV * const hv = GvSTASH(gv); | |
43693395 | 1123 | if (!hv) { |
7460c263 | 1124 | SvOK_off(sv); |
43693395 GS |
1125 | return; |
1126 | } | |
1127 | sv_setpv(sv, prefix ? prefix : ""); | |
3198e4e5 | 1128 | |
3c71ae1e NC |
1129 | name = HvNAME(hv); |
1130 | if (!name) | |
3198e4e5 | 1131 | name = "__ANON__"; |
3c71ae1e | 1132 | |
3198e4e5 AV |
1133 | if (keepmain || strNE(name, "main")) { |
1134 | sv_catpv(sv,name); | |
43693395 GS |
1135 | sv_catpvn(sv,"::", 2); |
1136 | } | |
1137 | sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv)); | |
1138 | } | |
1139 | ||
1140 | void | |
864dbfa3 | 1141 | Perl_gv_fullname3(pTHX_ SV *sv, GV *gv, const char *prefix) |
79072805 | 1142 | { |
5b7ea690 | 1143 | gv_fullname4(sv, gv, prefix, TRUE); |
79072805 LW |
1144 | } |
1145 | ||
1146 | void | |
43693395 GS |
1147 | Perl_gv_efullname4(pTHX_ SV *sv, GV *gv, const char *prefix, bool keepmain) |
1148 | { | |
1149 | GV *egv = GvEGV(gv); | |
1150 | if (!egv) | |
1151 | egv = gv; | |
1152 | gv_fullname4(sv, egv, prefix, keepmain); | |
1153 | } | |
1154 | ||
1155 | void | |
864dbfa3 | 1156 | Perl_gv_efullname3(pTHX_ SV *sv, GV *gv, const char *prefix) |
79072805 | 1157 | { |
5b7ea690 | 1158 | gv_efullname4(sv, gv, prefix, TRUE); |
f6aff53a | 1159 | } |
1160 | ||
7120cae1 | 1161 | /* compatibility with versions <= 5.003. */ |
f6aff53a | 1162 | void |
864dbfa3 | 1163 | Perl_gv_fullname(pTHX_ SV *sv, GV *gv) |
f6aff53a | 1164 | { |
7120cae1 | 1165 | gv_fullname3(sv, gv, sv == (const SV*)gv ? "*" : ""); |
f6aff53a | 1166 | } |
1167 | ||
7120cae1 | 1168 | /* compatibility with versions <= 5.003. */ |
f6aff53a | 1169 | void |
864dbfa3 | 1170 | Perl_gv_efullname(pTHX_ SV *sv, GV *gv) |
f6aff53a | 1171 | { |
1172 | gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : ""); | |
79072805 LW |
1173 | } |
1174 | ||
1175 | IO * | |
864dbfa3 | 1176 | Perl_newIO(pTHX) |
79072805 LW |
1177 | { |
1178 | IO *io; | |
8990e307 LW |
1179 | GV *iogv; |
1180 | ||
1181 | io = (IO*)NEWSV(0,0); | |
a0d0e21e | 1182 | sv_upgrade((SV *)io,SVt_PVIO); |
8990e307 LW |
1183 | SvREFCNT(io) = 1; |
1184 | SvOBJECT_on(io); | |
c0401c5d JH |
1185 | /* Clear the stashcache because a new IO could overrule a |
1186 | package name */ | |
1187 | hv_clear(PL_stashcache); | |
c9de509e | 1188 | iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV); |
5f2d631d GS |
1189 | /* unless exists($main::{FileHandle}) and defined(%main::FileHandle::) */ |
1190 | if (!(iogv && GvHV(iogv) && HvARRAY(GvHV(iogv)))) | |
c9de509e | 1191 | iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV); |
a8dc4fe8 | 1192 | SvSTASH_set(io, (HV*)SvREFCNT_inc(GvHV(iogv))); |
79072805 LW |
1193 | return io; |
1194 | } | |
1195 | ||
1196 | void | |
864dbfa3 | 1197 | Perl_gv_check(pTHX_ HV *stash) |
79072805 LW |
1198 | { |
1199 | register HE *entry; | |
1200 | register I32 i; | |
1201 | register GV *gv; | |
463ee0b2 LW |
1202 | HV *hv; |
1203 | ||
8990e307 LW |
1204 | if (!HvARRAY(stash)) |
1205 | return; | |
a0d0e21e | 1206 | for (i = 0; i <= (I32) HvMAX(stash); i++) { |
dc437b57 | 1207 | for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) { |
1208 | if (HeKEY(entry)[HeKLEN(entry)-1] == ':' && | |
b9219079 | 1209 | (gv = (GV*)HeVAL(entry)) && isGV(gv) && (hv = GvHV(gv))) |
a0d0e21e | 1210 | { |
19b6c847 | 1211 | if (hv != PL_defstash && hv != stash) |
a0d0e21e LW |
1212 | gv_check(hv); /* nested package */ |
1213 | } | |
dc437b57 | 1214 | else if (isALPHA(*HeKEY(entry))) { |
1d7c1841 | 1215 | char *file; |
dc437b57 | 1216 | gv = (GV*)HeVAL(entry); |
55d729e4 | 1217 | if (SvTYPE(gv) != SVt_PVGV || GvMULTI(gv)) |
463ee0b2 | 1218 | continue; |
1d7c1841 GS |
1219 | file = GvFILE(gv); |
1220 | /* performance hack: if filename is absolute and it's a standard | |
1221 | * module, don't bother warning */ | |
1222 | if (file | |
1223 | && PERL_FILE_IS_ABSOLUTE(file) | |
6eb630b7 CN |
1224 | #ifdef MACOS_TRADITIONAL |
1225 | && (instr(file, ":lib:") | |
1226 | #else | |
1227 | && (instr(file, "/lib/") | |
1228 | #endif | |
1229 | || instr(file, ".pm"))) | |
1d7c1841 | 1230 | { |
8990e307 | 1231 | continue; |
1d7c1841 GS |
1232 | } |
1233 | CopLINE_set(PL_curcop, GvLINE(gv)); | |
1234 | #ifdef USE_ITHREADS | |
1235 | CopFILE(PL_curcop) = file; /* set for warning */ | |
1236 | #else | |
1237 | CopFILEGV(PL_curcop) = gv_fetchfile(file); | |
1238 | #endif | |
9014280d | 1239 | Perl_warner(aTHX_ packWARN(WARN_ONCE), |
599cee73 | 1240 | "Name \"%s::%s\" used only once: possible typo", |
a0d0e21e | 1241 | HvNAME(stash), GvNAME(gv)); |
463ee0b2 | 1242 | } |
79072805 LW |
1243 | } |
1244 | } | |
1245 | } | |
1246 | ||
1247 | GV * | |
864dbfa3 | 1248 | Perl_newGVgen(pTHX_ char *pack) |
79072805 | 1249 | { |
cea2e8a9 | 1250 | return gv_fetchpv(Perl_form(aTHX_ "%s::_GEN_%ld", pack, (long)PL_gensym++), |
46fc3d4c | 1251 | TRUE, SVt_PVGV); |
79072805 LW |
1252 | } |
1253 | ||
1254 | /* hopefully this is only called on local symbol table entries */ | |
1255 | ||
1256 | GP* | |
864dbfa3 | 1257 | Perl_gp_ref(pTHX_ GP *gp) |
79072805 | 1258 | { |
1d7c1841 GS |
1259 | if (!gp) |
1260 | return (GP*)NULL; | |
79072805 | 1261 | gp->gp_refcnt++; |
44a8e56a | 1262 | if (gp->gp_cv) { |
1263 | if (gp->gp_cvgen) { | |
1264 | /* multi-named GPs cannot be used for method cache */ | |
1265 | SvREFCNT_dec(gp->gp_cv); | |
1266 | gp->gp_cv = Nullcv; | |
1267 | gp->gp_cvgen = 0; | |
1268 | } | |
1269 | else { | |
1270 | /* Adding a new name to a subroutine invalidates method cache */ | |
3280af22 | 1271 | PL_sub_generation++; |
44a8e56a | 1272 | } |
1273 | } | |
79072805 | 1274 | return gp; |
79072805 LW |
1275 | } |
1276 | ||
1277 | void | |
864dbfa3 | 1278 | Perl_gp_free(pTHX_ GV *gv) |
79072805 | 1279 | { |
79072805 LW |
1280 | GP* gp; |
1281 | ||
1282 | if (!gv || !(gp = GvGP(gv))) | |
1283 | return; | |
f248d071 GS |
1284 | if (gp->gp_refcnt == 0) { |
1285 | if (ckWARN_d(WARN_INTERNAL)) | |
9014280d | 1286 | Perl_warner(aTHX_ packWARN(WARN_INTERNAL), |
b035a42e NC |
1287 | "Attempt to free unreferenced glob pointers" |
1288 | pTHX__FORMAT pTHX__VALUE); | |
79072805 LW |
1289 | return; |
1290 | } | |
44a8e56a | 1291 | if (gp->gp_cv) { |
1292 | /* Deleting the name of a subroutine invalidates method cache */ | |
3280af22 | 1293 | PL_sub_generation++; |
44a8e56a | 1294 | } |
748a9306 LW |
1295 | if (--gp->gp_refcnt > 0) { |
1296 | if (gp->gp_egv == gv) | |
1297 | gp->gp_egv = 0; | |
79072805 | 1298 | return; |
748a9306 | 1299 | } |
79072805 | 1300 | |
7cc0bfa7 | 1301 | if (gp->gp_sv) SvREFCNT_dec(gp->gp_sv); |
8b3a4b74 | 1302 | if (gp->gp_av) SvREFCNT_dec(gp->gp_av); |
7cc0bfa7 AT |
1303 | if (gp->gp_hv) { |
1304 | if (PL_stashcache && HvNAME(gp->gp_hv)) | |
1305 | hv_delete(PL_stashcache, | |
1306 | HvNAME(gp->gp_hv), strlen(HvNAME(gp->gp_hv)), | |
1307 | G_DISCARD); | |
1308 | SvREFCNT_dec(gp->gp_hv); | |
1309 | } | |
1310 | if (gp->gp_io) SvREFCNT_dec(gp->gp_io); | |
1311 | if (gp->gp_cv) SvREFCNT_dec(gp->gp_cv); | |
1312 | if (gp->gp_form) SvREFCNT_dec(gp->gp_form); | |
748a9306 | 1313 | |
79072805 LW |
1314 | Safefree(gp); |
1315 | GvGP(gv) = 0; | |
1316 | } | |
1317 | ||
d460ef45 NIS |
1318 | int |
1319 | Perl_magic_freeovrld(pTHX_ SV *sv, MAGIC *mg) | |
1320 | { | |
1321 | AMT *amtp = (AMT*)mg->mg_ptr; | |
1322 | if (amtp && AMT_AMAGIC(amtp)) { | |
1323 | int i; | |
1324 | for (i = 1; i < NofAMmeth; i++) { | |
1325 | CV *cv = amtp->table[i]; | |
1326 | if (cv != Nullcv) { | |
1327 | SvREFCNT_dec((SV *) cv); | |
1328 | amtp->table[i] = Nullcv; | |
1329 | } | |
1330 | } | |
1331 | } | |
1332 | return 0; | |
1333 | } | |
1334 | ||
a0d0e21e LW |
1335 | /* Updates and caches the CV's */ |
1336 | ||
1337 | bool | |
864dbfa3 | 1338 | Perl_Gv_AMupdate(pTHX_ HV *stash) |
a0d0e21e | 1339 | { |
a0d0e21e LW |
1340 | GV* gv; |
1341 | CV* cv; | |
14befaf4 | 1342 | MAGIC* mg=mg_find((SV*)stash, PERL_MAGIC_overload_table); |
8ac85365 | 1343 | AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL; |
a6006777 | 1344 | AMT amt; |
a0d0e21e | 1345 | |
3280af22 NIS |
1346 | if (mg && amtp->was_ok_am == PL_amagic_generation |
1347 | && amtp->was_ok_sub == PL_sub_generation) | |
eb160463 | 1348 | return (bool)AMT_OVERLOADED(amtp); |
14befaf4 | 1349 | sv_unmagic((SV*)stash, PERL_MAGIC_overload_table); |
a0d0e21e | 1350 | |
cea2e8a9 | 1351 | DEBUG_o( Perl_deb(aTHX_ "Recalcing overload magic in package %s\n",HvNAME(stash)) ); |
a0d0e21e | 1352 | |
d460ef45 | 1353 | Zero(&amt,1,AMT); |
3280af22 NIS |
1354 | amt.was_ok_am = PL_amagic_generation; |
1355 | amt.was_ok_sub = PL_sub_generation; | |
a6006777 | 1356 | amt.fallback = AMGfallNO; |
1357 | amt.flags = 0; | |
1358 | ||
a6006777 | 1359 | { |
32251b26 IZ |
1360 | int filled = 0, have_ovl = 0; |
1361 | int i, lim = 1; | |
a6006777 | 1362 | SV* sv = NULL; |
a6006777 | 1363 | |
22c35a8c | 1364 | /* Work with "fallback" key, which we assume to be first in PL_AMG_names */ |
a6006777 | 1365 | |
89ffc314 IZ |
1366 | /* Try to find via inheritance. */ |
1367 | gv = gv_fetchmeth(stash, PL_AMG_names[0], 2, -1); | |
1368 | if (gv) | |
1369 | sv = GvSV(gv); | |
1370 | ||
1371 | if (!gv) | |
32251b26 | 1372 | lim = DESTROY_amg; /* Skip overloading entries. */ |
89ffc314 IZ |
1373 | else if (SvTRUE(sv)) |
1374 | amt.fallback=AMGfallYES; | |
1375 | else if (SvOK(sv)) | |
1376 | amt.fallback=AMGfallNEVER; | |
a6006777 | 1377 | |
32251b26 IZ |
1378 | for (i = 1; i < lim; i++) |
1379 | amt.table[i] = Nullcv; | |
1380 | for (; i < NofAMmeth; i++) { | |
c8ce92fc | 1381 | char *cooky = (char*)PL_AMG_names[i]; |
32251b26 IZ |
1382 | /* Human-readable form, for debugging: */ |
1383 | char *cp = (i >= DESTROY_amg ? cooky : AMG_id2name(i)); | |
89ffc314 IZ |
1384 | STRLEN l = strlen(cooky); |
1385 | ||
cea2e8a9 | 1386 | DEBUG_o( Perl_deb(aTHX_ "Checking overloading of `%s' in package `%.256s'\n", |
44a8e56a | 1387 | cp, HvNAME(stash)) ); |
611c1e95 IZ |
1388 | /* don't fill the cache while looking up! |
1389 | Creation of inheritance stubs in intermediate packages may | |
1390 | conflict with the logic of runtime method substitution. | |
1391 | Indeed, for inheritance A -> B -> C, if C overloads "+0", | |
1392 | then we could have created stubs for "(+0" in A and C too. | |
1393 | But if B overloads "bool", we may want to use it for | |
1394 | numifying instead of C's "+0". */ | |
1395 | if (i >= DESTROY_amg) | |
1396 | gv = Perl_gv_fetchmeth_autoload(aTHX_ stash, cooky, l, 0); | |
1397 | else /* Autoload taken care of below */ | |
1398 | gv = Perl_gv_fetchmeth(aTHX_ stash, cooky, l, -1); | |
46fc3d4c | 1399 | cv = 0; |
89ffc314 | 1400 | if (gv && (cv = GvCV(gv))) { |
44a8e56a | 1401 | if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil") |
1402 | && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) { | |
611c1e95 IZ |
1403 | /* This is a hack to support autoloading..., while |
1404 | knowing *which* methods were declared as overloaded. */ | |
44a8e56a | 1405 | /* GvSV contains the name of the method. */ |
4ea42e7f | 1406 | GV *ngv = Nullgv; |
44a8e56a | 1407 | |
f824e39a JH |
1408 | DEBUG_o( Perl_deb(aTHX_ "Resolving method `%"SVf256\ |
1409 | "' for overloaded `%s' in package `%.256s'\n", | |
c293eb2b | 1410 | GvSV(gv), cp, HvNAME(stash)) ); |
b267980d | 1411 | if (!SvPOK(GvSV(gv)) |
dc848c6f | 1412 | || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)), |
1413 | FALSE))) | |
1414 | { | |
44a8e56a | 1415 | /* Can be an import stub (created by `can'). */ |
c293eb2b NC |
1416 | SV *gvsv = GvSV(gv); |
1417 | const char *name = SvPOK(gvsv) ? SvPVX(gvsv) : "???"; | |
f824e39a JH |
1418 | Perl_croak(aTHX_ "%s method `%.256s' overloading `%s' "\ |
1419 | "in package `%.256s'", | |
c293eb2b NC |
1420 | (GvCVGEN(gv) ? "Stub found while resolving" |
1421 | : "Can't resolve"), | |
1422 | name, cp, HvNAME(stash)); | |
44a8e56a | 1423 | } |
dc848c6f | 1424 | cv = GvCV(gv = ngv); |
44a8e56a | 1425 | } |
cea2e8a9 | 1426 | DEBUG_o( Perl_deb(aTHX_ "Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n", |
44a8e56a | 1427 | cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))), |
1428 | GvNAME(CvGV(cv))) ); | |
1429 | filled = 1; | |
32251b26 IZ |
1430 | if (i < DESTROY_amg) |
1431 | have_ovl = 1; | |
611c1e95 IZ |
1432 | } else if (gv) { /* Autoloaded... */ |
1433 | cv = (CV*)gv; | |
1434 | filled = 1; | |
44a8e56a | 1435 | } |
a6006777 | 1436 | amt.table[i]=(CV*)SvREFCNT_inc(cv); |
a0d0e21e | 1437 | } |
a0d0e21e | 1438 | if (filled) { |
a6006777 | 1439 | AMT_AMAGIC_on(&amt); |
32251b26 IZ |
1440 | if (have_ovl) |
1441 | AMT_OVERLOADED_on(&amt); | |
14befaf4 DM |
1442 | sv_magic((SV*)stash, 0, PERL_MAGIC_overload_table, |
1443 | (char*)&amt, sizeof(AMT)); | |
32251b26 | 1444 | return have_ovl; |
a0d0e21e LW |
1445 | } |
1446 | } | |
a6006777 | 1447 | /* Here we have no table: */ |
9cbac4c7 | 1448 | /* no_table: */ |
a6006777 | 1449 | AMT_AMAGIC_off(&amt); |
14befaf4 DM |
1450 | sv_magic((SV*)stash, 0, PERL_MAGIC_overload_table, |
1451 | (char*)&amt, sizeof(AMTS)); | |
a0d0e21e LW |
1452 | return FALSE; |
1453 | } | |
1454 | ||
32251b26 IZ |
1455 | |
1456 | CV* | |
1457 | Perl_gv_handler(pTHX_ HV *stash, I32 id) | |
1458 | { | |
3f8f4626 | 1459 | MAGIC *mg; |
32251b26 | 1460 | AMT *amtp; |
3ad83ce7 | 1461 | CV *ret; |
32251b26 | 1462 | |
3198e4e5 | 1463 | if (!stash || !HvNAME(stash)) |
3f8f4626 | 1464 | return Nullcv; |
14befaf4 | 1465 | mg = mg_find((SV*)stash, PERL_MAGIC_overload_table); |
32251b26 IZ |
1466 | if (!mg) { |
1467 | do_update: | |
1468 | Gv_AMupdate(stash); | |
14befaf4 | 1469 | mg = mg_find((SV*)stash, PERL_MAGIC_overload_table); |
32251b26 IZ |
1470 | } |
1471 | amtp = (AMT*)mg->mg_ptr; | |
1472 | if ( amtp->was_ok_am != PL_amagic_generation | |
1473 | || amtp->was_ok_sub != PL_sub_generation ) | |
1474 | goto do_update; | |
3ad83ce7 AMS |
1475 | if (AMT_AMAGIC(amtp)) { |
1476 | ret = amtp->table[id]; | |
1477 | if (ret && isGV(ret)) { /* Autoloading stab */ | |
1478 | /* Passing it through may have resulted in a warning | |
1479 | "Inherited AUTOLOAD for a non-method deprecated", since | |
1480 | our caller is going through a function call, not a method call. | |
1481 | So return the CV for AUTOLOAD, setting $AUTOLOAD. */ | |
1482 | GV *gv = gv_fetchmethod(stash, (char*)PL_AMG_names[id]); | |
1483 | ||
1484 | if (gv && GvCV(gv)) | |
1485 | return GvCV(gv); | |
1486 | } | |
1487 | return ret; | |
1488 | } | |
1489 | ||
32251b26 IZ |
1490 | return Nullcv; |
1491 | } | |
1492 | ||
1493 | ||
a0d0e21e | 1494 | SV* |
864dbfa3 | 1495 | Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags) |
a0d0e21e | 1496 | { |
b267980d | 1497 | MAGIC *mg; |
9c5ffd7c | 1498 | CV *cv=NULL; |
a0d0e21e | 1499 | CV **cvp=NULL, **ocvp=NULL; |
9c5ffd7c | 1500 | AMT *amtp=NULL, *oamtp=NULL; |
497b47a8 | 1501 | int off=0, off1, lr=0, assign=AMGf_assign & flags, notfound=0; |
ee239bfe | 1502 | int postpr = 0, force_cpy = 0, assignshift = assign ? 1 : 0; |
497b47a8 JH |
1503 | #ifdef DEBUGGING |
1504 | int fl=0; | |
497b47a8 | 1505 | #endif |
25716404 | 1506 | HV* stash=NULL; |
a0d0e21e | 1507 | if (!(AMGf_noleft & flags) && SvAMAGIC(left) |
25716404 GS |
1508 | && (stash = SvSTASH(SvRV(left))) |
1509 | && (mg = mg_find((SV*)stash, PERL_MAGIC_overload_table)) | |
b267980d | 1510 | && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr) |
a6006777 | 1511 | ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table |
8ac85365 | 1512 | : (CV **) NULL)) |
b267980d | 1513 | && ((cv = cvp[off=method+assignshift]) |
748a9306 LW |
1514 | || (assign && amtp->fallback > AMGfallNEVER && /* fallback to |
1515 | * usual method */ | |
497b47a8 JH |
1516 | ( |
1517 | #ifdef DEBUGGING | |
1518 | fl = 1, | |
1519 | #endif | |
1520 | cv = cvp[off=method])))) { | |
a0d0e21e LW |
1521 | lr = -1; /* Call method for left argument */ |
1522 | } else { | |
1523 | if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) { | |
1524 | int logic; | |
1525 | ||
1526 | /* look for substituted methods */ | |
ee239bfe | 1527 | /* In all the covered cases we should be called with assign==0. */ |
a0d0e21e LW |
1528 | switch (method) { |
1529 | case inc_amg: | |
ee239bfe IZ |
1530 | force_cpy = 1; |
1531 | if ((cv = cvp[off=add_ass_amg]) | |
1532 | || ((cv = cvp[off = add_amg]) && (force_cpy = 0, postpr = 1))) { | |
3280af22 | 1533 | right = &PL_sv_yes; lr = -1; assign = 1; |
a0d0e21e LW |
1534 | } |
1535 | break; | |
1536 | case dec_amg: | |
ee239bfe IZ |
1537 | force_cpy = 1; |
1538 | if ((cv = cvp[off = subtr_ass_amg]) | |
1539 | || ((cv = cvp[off = subtr_amg]) && (force_cpy = 0, postpr=1))) { | |
3280af22 | 1540 | right = &PL_sv_yes; lr = -1; assign = 1; |
a0d0e21e LW |
1541 | } |
1542 | break; | |
1543 | case bool__amg: | |
1544 | (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg])); | |
1545 | break; | |
1546 | case numer_amg: | |
1547 | (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg])); | |
1548 | break; | |
1549 | case string_amg: | |
1550 | (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg])); | |
1551 | break; | |
dc437b57 | 1552 | case not_amg: |
b267980d | 1553 | (void)((cv = cvp[off=bool__amg]) |
dc437b57 | 1554 | || (cv = cvp[off=numer_amg]) |
1555 | || (cv = cvp[off=string_amg])); | |
1556 | postpr = 1; | |
1557 | break; | |
748a9306 LW |
1558 | case copy_amg: |
1559 | { | |
76e3520e GS |
1560 | /* |
1561 | * SV* ref causes confusion with the interpreter variable of | |
1562 | * the same name | |
1563 | */ | |
1564 | SV* tmpRef=SvRV(left); | |
1565 | if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) { | |
fc36a67e | 1566 | /* |
1567 | * Just to be extra cautious. Maybe in some | |
1568 | * additional cases sv_setsv is safe, too. | |
1569 | */ | |
76e3520e | 1570 | SV* newref = newSVsv(tmpRef); |
748a9306 | 1571 | SvOBJECT_on(newref); |
a8dc4fe8 | 1572 | SvSTASH_set(newref, (HV*)SvREFCNT_inc(SvSTASH(tmpRef))); |
748a9306 LW |
1573 | return newref; |
1574 | } | |
1575 | } | |
1576 | break; | |
a0d0e21e | 1577 | case abs_amg: |
b267980d | 1578 | if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg]) |
a0d0e21e | 1579 | && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) { |
748a9306 | 1580 | SV* nullsv=sv_2mortal(newSViv(0)); |
a0d0e21e | 1581 | if (off1==lt_amg) { |
748a9306 | 1582 | SV* lessp = amagic_call(left,nullsv, |
a0d0e21e LW |
1583 | lt_amg,AMGf_noright); |
1584 | logic = SvTRUE(lessp); | |
1585 | } else { | |
748a9306 | 1586 | SV* lessp = amagic_call(left,nullsv, |
a0d0e21e LW |
1587 | ncmp_amg,AMGf_noright); |
1588 | logic = (SvNV(lessp) < 0); | |
1589 | } | |
1590 | if (logic) { | |
1591 | if (off==subtr_amg) { | |
1592 | right = left; | |
748a9306 | 1593 | left = nullsv; |
a0d0e21e LW |
1594 | lr = 1; |
1595 | } | |
1596 | } else { | |
1597 | return left; | |
1598 | } | |
1599 | } | |
1600 | break; | |
1601 | case neg_amg: | |
155aba94 | 1602 | if ((cv = cvp[off=subtr_amg])) { |
a0d0e21e LW |
1603 | right = left; |
1604 | left = sv_2mortal(newSViv(0)); | |
1605 | lr = 1; | |
1606 | } | |
1607 | break; | |
f216259d | 1608 | case int_amg: |
f5284f61 | 1609 | case iter_amg: /* XXXX Eventually should do to_gv. */ |
b267980d NIS |
1610 | /* FAIL safe */ |
1611 | return NULL; /* Delegate operation to standard mechanisms. */ | |
1612 | break; | |
f5284f61 IZ |
1613 | case to_sv_amg: |
1614 | case to_av_amg: | |
1615 | case to_hv_amg: | |
1616 | case to_gv_amg: | |
1617 | case to_cv_amg: | |
1618 | /* FAIL safe */ | |
b267980d | 1619 | return left; /* Delegate operation to standard mechanisms. */ |
f5284f61 | 1620 | break; |
a0d0e21e LW |
1621 | default: |
1622 | goto not_found; | |
1623 | } | |
1624 | if (!cv) goto not_found; | |
1625 | } else if (!(AMGf_noright & flags) && SvAMAGIC(right) | |
25716404 GS |
1626 | && (stash = SvSTASH(SvRV(right))) |
1627 | && (mg = mg_find((SV*)stash, PERL_MAGIC_overload_table)) | |
b267980d | 1628 | && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr) |
a6006777 | 1629 | ? (amtp = (AMT*)mg->mg_ptr)->table |
8ac85365 | 1630 | : (CV **) NULL)) |
a0d0e21e LW |
1631 | && (cv = cvp[off=method])) { /* Method for right |
1632 | * argument found */ | |
1633 | lr=1; | |
b267980d NIS |
1634 | } else if (((ocvp && oamtp->fallback > AMGfallNEVER |
1635 | && (cvp=ocvp) && (lr = -1)) | |
a0d0e21e LW |
1636 | || (cvp && amtp->fallback > AMGfallNEVER && (lr=1))) |
1637 | && !(flags & AMGf_unary)) { | |
1638 | /* We look for substitution for | |
1639 | * comparison operations and | |
fc36a67e | 1640 | * concatenation */ |
a0d0e21e LW |
1641 | if (method==concat_amg || method==concat_ass_amg |
1642 | || method==repeat_amg || method==repeat_ass_amg) { | |
1643 | return NULL; /* Delegate operation to string conversion */ | |
1644 | } | |
1645 | off = -1; | |
1646 | switch (method) { | |
1647 | case lt_amg: | |
1648 | case le_amg: | |
1649 | case gt_amg: | |
1650 | case ge_amg: | |
1651 | case eq_amg: | |
1652 | case ne_amg: | |
1653 | postpr = 1; off=ncmp_amg; break; | |
1654 | case slt_amg: | |
1655 | case sle_amg: | |
1656 | case sgt_amg: | |
1657 | case sge_amg: | |
1658 | case seq_amg: | |
1659 | case sne_amg: | |
1660 | postpr = 1; off=scmp_amg; break; | |
1661 | } | |
1662 | if (off != -1) cv = cvp[off]; | |
1663 | if (!cv) { | |
1664 | goto not_found; | |
1665 | } | |
1666 | } else { | |
a6006777 | 1667 | not_found: /* No method found, either report or croak */ |
b267980d NIS |
1668 | switch (method) { |
1669 | case to_sv_amg: | |
1670 | case to_av_amg: | |
1671 | case to_hv_amg: | |
1672 | case to_gv_amg: | |
1673 | case to_cv_amg: | |
1674 | /* FAIL safe */ | |
1675 | return left; /* Delegate operation to standard mechanisms. */ | |
1676 | break; | |
1677 | } | |
a0d0e21e LW |
1678 | if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */ |
1679 | notfound = 1; lr = -1; | |
1680 | } else if (cvp && (cv=cvp[nomethod_amg])) { | |
1681 | notfound = 1; lr = 1; | |
1682 | } else { | |
46fc3d4c | 1683 | SV *msg; |
774d564b | 1684 | if (off==-1) off=method; |
b267980d | 1685 | msg = sv_2mortal(Perl_newSVpvf(aTHX_ |
46fc3d4c | 1686 | "Operation `%s': no method found,%sargument %s%s%s%s", |
89ffc314 | 1687 | AMG_id2name(method + assignshift), |
e7ea3e70 | 1688 | (flags & AMGf_unary ? " " : "\n\tleft "), |
b267980d | 1689 | SvAMAGIC(left)? |
a0d0e21e LW |
1690 | "in overloaded package ": |
1691 | "has no overloaded magic", | |
b267980d | 1692 | SvAMAGIC(left)? |
a0d0e21e LW |
1693 | HvNAME(SvSTASH(SvRV(left))): |
1694 | "", | |
b267980d | 1695 | SvAMAGIC(right)? |
e7ea3e70 | 1696 | ",\n\tright argument in overloaded package ": |
b267980d | 1697 | (flags & AMGf_unary |
e7ea3e70 IZ |
1698 | ? "" |
1699 | : ",\n\tright argument has no overloaded magic"), | |
b267980d | 1700 | SvAMAGIC(right)? |
a0d0e21e | 1701 | HvNAME(SvSTASH(SvRV(right))): |
46fc3d4c | 1702 | "")); |
a0d0e21e | 1703 | if (amtp && amtp->fallback >= AMGfallYES) { |
cea2e8a9 | 1704 | DEBUG_o( Perl_deb(aTHX_ "%s", SvPVX(msg)) ); |
a0d0e21e | 1705 | } else { |
894356b3 | 1706 | Perl_croak(aTHX_ "%"SVf, msg); |
a0d0e21e LW |
1707 | } |
1708 | return NULL; | |
1709 | } | |
ee239bfe | 1710 | force_cpy = force_cpy || assign; |
a0d0e21e LW |
1711 | } |
1712 | } | |
497b47a8 | 1713 | #ifdef DEBUGGING |
a0d0e21e | 1714 | if (!notfound) { |
497b47a8 JH |
1715 | DEBUG_o(Perl_deb(aTHX_ |
1716 | "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n", | |
1717 | AMG_id2name(off), | |
1718 | method+assignshift==off? "" : | |
1719 | " (initially `", | |
1720 | method+assignshift==off? "" : | |
1721 | AMG_id2name(method+assignshift), | |
1722 | method+assignshift==off? "" : "')", | |
1723 | flags & AMGf_unary? "" : | |
1724 | lr==1 ? " for right argument": " for left argument", | |
1725 | flags & AMGf_unary? " for argument" : "", | |
25716404 | 1726 | stash ? HvNAME(stash) : "null", |
497b47a8 | 1727 | fl? ",\n\tassignment variant used": "") ); |
ee239bfe | 1728 | } |
497b47a8 | 1729 | #endif |
748a9306 LW |
1730 | /* Since we use shallow copy during assignment, we need |
1731 | * to dublicate the contents, probably calling user-supplied | |
1732 | * version of copy operator | |
1733 | */ | |
ee239bfe IZ |
1734 | /* We need to copy in following cases: |
1735 | * a) Assignment form was called. | |
1736 | * assignshift==1, assign==T, method + 1 == off | |
1737 | * b) Increment or decrement, called directly. | |
1738 | * assignshift==0, assign==0, method + 0 == off | |
1739 | * c) Increment or decrement, translated to assignment add/subtr. | |
b267980d | 1740 | * assignshift==0, assign==T, |
ee239bfe IZ |
1741 | * force_cpy == T |
1742 | * d) Increment or decrement, translated to nomethod. | |
b267980d | 1743 | * assignshift==0, assign==0, |
ee239bfe IZ |
1744 | * force_cpy == T |
1745 | * e) Assignment form translated to nomethod. | |
1746 | * assignshift==1, assign==T, method + 1 != off | |
1747 | * force_cpy == T | |
1748 | */ | |
1749 | /* off is method, method+assignshift, or a result of opcode substitution. | |
1750 | * In the latter case assignshift==0, so only notfound case is important. | |
1751 | */ | |
1752 | if (( (method + assignshift == off) | |
1753 | && (assign || (method == inc_amg) || (method == dec_amg))) | |
1754 | || force_cpy) | |
1755 | RvDEEPCP(left); | |
a0d0e21e LW |
1756 | { |
1757 | dSP; | |
1758 | BINOP myop; | |
1759 | SV* res; | |
54310121 | 1760 | bool oldcatch = CATCH_GET; |
a0d0e21e | 1761 | |
54310121 | 1762 | CATCH_SET(TRUE); |
a0d0e21e LW |
1763 | Zero(&myop, 1, BINOP); |
1764 | myop.op_last = (OP *) &myop; | |
1765 | myop.op_next = Nullop; | |
54310121 | 1766 | myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED; |
a0d0e21e | 1767 | |
e788e7d3 | 1768 | PUSHSTACKi(PERLSI_OVERLOAD); |
a0d0e21e | 1769 | ENTER; |
462e5cf6 | 1770 | SAVEOP(); |
533c011a | 1771 | PL_op = (OP *) &myop; |
3280af22 | 1772 | if (PERLDB_SUB && PL_curstash != PL_debstash) |
533c011a | 1773 | PL_op->op_private |= OPpENTERSUB_DB; |
a0d0e21e | 1774 | PUTBACK; |
cea2e8a9 | 1775 | pp_pushmark(); |
a0d0e21e | 1776 | |
924508f0 | 1777 | EXTEND(SP, notfound + 5); |
a0d0e21e LW |
1778 | PUSHs(lr>0? right: left); |
1779 | PUSHs(lr>0? left: right); | |
3280af22 | 1780 | PUSHs( lr > 0 ? &PL_sv_yes : ( assign ? &PL_sv_undef : &PL_sv_no )); |
a0d0e21e | 1781 | if (notfound) { |
89ffc314 | 1782 | PUSHs( sv_2mortal(newSVpv(AMG_id2name(method + assignshift),0))); |
a0d0e21e LW |
1783 | } |
1784 | PUSHs((SV*)cv); | |
1785 | PUTBACK; | |
1786 | ||
155aba94 | 1787 | if ((PL_op = Perl_pp_entersub(aTHX))) |
cea2e8a9 | 1788 | CALLRUNOPS(aTHX); |
a0d0e21e LW |
1789 | LEAVE; |
1790 | SPAGAIN; | |
1791 | ||
1792 | res=POPs; | |
ebafeae7 | 1793 | PUTBACK; |
d3acc0f7 | 1794 | POPSTACK; |
54310121 | 1795 | CATCH_SET(oldcatch); |
a0d0e21e | 1796 | |
a0d0e21e | 1797 | if (postpr) { |
9c5ffd7c | 1798 | int ans=0; |
a0d0e21e LW |
1799 | switch (method) { |
1800 | case le_amg: | |
1801 | case sle_amg: | |
1802 | ans=SvIV(res)<=0; break; | |
1803 | case lt_amg: | |
1804 | case slt_amg: | |
1805 | ans=SvIV(res)<0; break; | |
1806 | case ge_amg: | |
1807 | case sge_amg: | |
1808 | ans=SvIV(res)>=0; break; | |
1809 | case gt_amg: | |
1810 | case sgt_amg: | |
1811 | ans=SvIV(res)>0; break; | |
1812 | case eq_amg: | |
1813 | case seq_amg: | |
1814 | ans=SvIV(res)==0; break; | |
1815 | case ne_amg: | |
1816 | case sne_amg: | |
1817 | ans=SvIV(res)!=0; break; | |
1818 | case inc_amg: | |
1819 | case dec_amg: | |
bbce6d69 | 1820 | SvSetSV(left,res); return left; |
dc437b57 | 1821 | case not_amg: |
fe7ac86a | 1822 | ans=!SvTRUE(res); break; |
a0d0e21e | 1823 | } |
54310121 | 1824 | return boolSV(ans); |
748a9306 LW |
1825 | } else if (method==copy_amg) { |
1826 | if (!SvROK(res)) { | |
cea2e8a9 | 1827 | Perl_croak(aTHX_ "Copy method did not return a reference"); |
748a9306 LW |
1828 | } |
1829 | return SvREFCNT_inc(SvRV(res)); | |
a0d0e21e LW |
1830 | } else { |
1831 | return res; | |
1832 | } | |
1833 | } | |
1834 | } | |
c9d5ac95 GS |
1835 | |
1836 | /* | |
1837 | =for apidoc is_gv_magical | |
1838 | ||
1839 | Returns C<TRUE> if given the name of a magical GV. | |
1840 | ||
1841 | Currently only useful internally when determining if a GV should be | |
1842 | created even in rvalue contexts. | |
1843 | ||
1844 | C<flags> is not used at present but available for future extension to | |
1845 | allow selecting particular classes of magical variable. | |
1846 | ||
9e310c53 NC |
1847 | Currently assumes that C<name> is NUL terminated (as well as len being valid). |
1848 | This assumption is met by all callers within the perl core, which all pass | |
1849 | pointers returned by SvPV. | |
1850 | ||
c9d5ac95 GS |
1851 | =cut |
1852 | */ | |
1853 | bool | |
1854 | Perl_is_gv_magical(pTHX_ char *name, STRLEN len, U32 flags) | |
1855 | { | |
9e310c53 NC |
1856 | if (len > 1) { |
1857 | const char *name1 = name + 1; | |
1858 | switch (*name) { | |
1859 | case 'I': | |
1860 | if (len == 3 && name1[1] == 'S' && name[2] == 'A') | |
1861 | goto yes; | |
1862 | break; | |
1863 | case 'O': | |
1864 | if (len == 8 && strEQ(name1, "VERLOAD")) | |
1865 | goto yes; | |
1866 | break; | |
1867 | case 'S': | |
1868 | if (len == 3 && name[1] == 'I' && name[2] == 'G') | |
1869 | goto yes; | |
1870 | break; | |
1871 | /* Using ${^...} variables is likely to be sufficiently rare that | |
1872 | it seems sensible to avoid the space hit of also checking the | |
1873 | length. */ | |
1874 | case '\017': /* ${^OPEN} */ | |
1875 | if (strEQ(name1, "PEN")) | |
1876 | goto yes; | |
1877 | break; | |
1878 | case '\024': /* ${^TAINT} */ | |
1879 | if (strEQ(name1, "AINT")) | |
1880 | goto yes; | |
1881 | break; | |
1882 | case '\025': /* ${^UNICODE} */ | |
1883 | if (strEQ(name1, "NICODE")) | |
1884 | goto yes; | |
4c9c322d NC |
1885 | if (strEQ(name1, "TF8LOCALE")) |
1886 | goto yes; | |
9e310c53 NC |
1887 | break; |
1888 | case '\027': /* ${^WARNING_BITS} */ | |
1889 | if (strEQ(name1, "ARNING_BITS")) | |
1890 | goto yes; | |
1891 | break; | |
1892 | case '1': | |
1893 | case '2': | |
1894 | case '3': | |
1895 | case '4': | |
1896 | case '5': | |
1897 | case '6': | |
1898 | case '7': | |
1899 | case '8': | |
1900 | case '9': | |
c9d5ac95 | 1901 | { |
c9d5ac95 GS |
1902 | char *end = name + len; |
1903 | while (--end > name) { | |
1904 | if (!isDIGIT(*end)) | |
1905 | return FALSE; | |
1906 | } | |
9e310c53 NC |
1907 | goto yes; |
1908 | } | |
1909 | } | |
1910 | } else { | |
1911 | /* Because we're already assuming that name is NUL terminated | |
1912 | below, we can treat an empty name as "\0" */ | |
1913 | switch (*name) { | |
1914 | case '&': | |
1915 | case '`': | |
1916 | case '\'': | |
1917 | case ':': | |
1918 | case '?': | |
1919 | case '!': | |
1920 | case '-': | |
1921 | case '*': | |
1922 | case '#': | |
1923 | case '[': | |
1924 | case '^': | |
1925 | case '~': | |
1926 | case '=': | |
1927 | case '%': | |
1928 | case '.': | |
1929 | case '(': | |
1930 | case ')': | |
1931 | case '<': | |
1932 | case '>': | |
1933 | case ',': | |
1934 | case '\\': | |
1935 | case '/': | |
1936 | case '|': | |
1937 | case '+': | |
1938 | case ';': | |
1939 | case ']': | |
1940 | case '\001': /* $^A */ | |
1941 | case '\003': /* $^C */ | |
1942 | case '\004': /* $^D */ | |
1943 | case '\005': /* $^E */ | |
1944 | case '\006': /* $^F */ | |
1945 | case '\010': /* $^H */ | |
1946 | case '\011': /* $^I, NOT \t in EBCDIC */ | |
1947 | case '\014': /* $^L */ | |
1948 | case '\016': /* $^N */ | |
1949 | case '\017': /* $^O */ | |
1950 | case '\020': /* $^P */ | |
1951 | case '\023': /* $^S */ | |
1952 | case '\024': /* $^T */ | |
1953 | case '\026': /* $^V */ | |
1954 | case '\027': /* $^W */ | |
1955 | case '1': | |
1956 | case '2': | |
1957 | case '3': | |
1958 | case '4': | |
1959 | case '5': | |
1960 | case '6': | |
1961 | case '7': | |
1962 | case '8': | |
1963 | case '9': | |
1964 | yes: | |
1965 | return TRUE; | |
1966 | default: | |
1967 | break; | |
c9d5ac95 | 1968 | } |
c9d5ac95 GS |
1969 | } |
1970 | return FALSE; | |
1971 | } |