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