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