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