This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
missed a file
[perl5.git] / gv.c
CommitLineData
a0d0e21e 1/* gv.c
79072805 2 *
4eb8286e 3 * Copyright (c) 1991-1999, Larry Wall
79072805
LW
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
LW
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
LW
17 */
18
19#include "EXTERN.h"
864dbfa3 20#define PERL_IN_GV_C
79072805
LW
21#include "perl.h"
22
23GV *
864dbfa3 24Perl_gv_AVadd(pTHX_ register GV *gv)
79072805 25{
a0d0e21e
LW
26 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
27 croak("Bad symbol for array");
79072805
LW
28 if (!GvAV(gv))
29 GvAV(gv) = newAV();
30 return gv;
31}
32
33GV *
864dbfa3 34Perl_gv_HVadd(pTHX_ register GV *gv)
79072805 35{
a0d0e21e
LW
36 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
37 croak("Bad symbol for hash");
79072805 38 if (!GvHV(gv))
463ee0b2 39 GvHV(gv) = newHV();
79072805
LW
40 return gv;
41}
42
43GV *
864dbfa3 44Perl_gv_IOadd(pTHX_ register GV *gv)
a0d0e21e
LW
45{
46 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
47 croak("Bad symbol for filehandle");
48 if (!GvIOp(gv))
49 GvIOp(gv) = newIO();
50 return gv;
51}
52
53GV *
864dbfa3 54Perl_gv_fetchfile(pTHX_ const char *name)
79072805 55{
e858de61 56 dTHR;
53d95988
CS
57 char smallbuf[256];
58 char *tmpbuf;
8ebc5c01 59 STRLEN tmplen;
79072805
LW
60 GV *gv;
61
53d95988
CS
62 tmplen = strlen(name) + 2;
63 if (tmplen < sizeof smallbuf)
64 tmpbuf = smallbuf;
65 else
66 New(603, tmpbuf, tmplen + 1, char);
67 tmpbuf[0] = '_';
68 tmpbuf[1] = '<';
69 strcpy(tmpbuf + 2, name);
3280af22 70 gv = *(GV**)hv_fetch(PL_defstash, tmpbuf, tmplen, TRUE);
8ebc5c01 71 if (!isGV(gv))
3280af22 72 gv_init(gv, PL_defstash, tmpbuf, tmplen, FALSE);
53d95988
CS
73 if (tmpbuf != smallbuf)
74 Safefree(tmpbuf);
79072805 75 sv_setpv(GvSV(gv), name);
8ebc5c01 76 if (*name == '/' && (instr(name, "/lib/") || instr(name, ".pm")))
a5f75d66 77 GvMULTI_on(gv);
84902520 78 if (PERLDB_LINE)
93a17b20 79 hv_magic(GvHVn(gv_AVadd(gv)), gv, 'L');
79072805
LW
80 return gv;
81}
82
463ee0b2 83void
864dbfa3 84Perl_gv_init(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len, int multi)
463ee0b2 85{
0f15f207 86 dTHR;
463ee0b2 87 register GP *gp;
55d729e4
GS
88 bool doproto = SvTYPE(gv) > SVt_NULL;
89 char *proto = (doproto && SvPOK(gv)) ? SvPVX(gv) : NULL;
463ee0b2 90
dc437b57 91 sv_upgrade((SV*)gv, SVt_PVGV);
55d729e4
GS
92 if (SvLEN(gv)) {
93 if (proto) {
94 SvPVX(gv) = NULL;
95 SvLEN(gv) = 0;
96 SvPOK_off(gv);
97 } else
98 Safefree(SvPVX(gv));
99 }
44a8e56a 100 Newz(602, gp, 1, GP);
8990e307 101 GvGP(gv) = gp_ref(gp);
463ee0b2 102 GvSV(gv) = NEWSV(72,0);
3280af22
NIS
103 GvLINE(gv) = PL_curcop->cop_line;
104 GvFILEGV(gv) = PL_curcop->cop_filegv;
005a453c 105 GvCVGEN(gv) = 0;
463ee0b2
LW
106 GvEGV(gv) = gv;
107 sv_magic((SV*)gv, (SV*)gv, '*', name, len);
85aff577 108 GvSTASH(gv) = (HV*)SvREFCNT_inc(stash);
a0d0e21e 109 GvNAME(gv) = savepvn(name, len);
463ee0b2 110 GvNAMELEN(gv) = len;
23ad5bf5 111 if (multi || doproto) /* doproto means it _was_ mentioned */
a5f75d66 112 GvMULTI_on(gv);
55d729e4 113 if (doproto) { /* Replicate part of newSUB here. */
57ff9a15 114 SvIOK_off(gv);
55d729e4 115 ENTER;
b099ddc0 116 /* XXX unsafe for threads if eval_owner isn't held */
55d729e4 117 start_subparse(0,0); /* Create CV in compcv. */
3280af22 118 GvCV(gv) = PL_compcv;
55d729e4
GS
119 LEAVE;
120
3280af22 121 PL_sub_generation++;
55d729e4 122 CvGV(GvCV(gv)) = (GV*)SvREFCNT_inc(gv);
3280af22
NIS
123 CvFILEGV(GvCV(gv)) = PL_curcop->cop_filegv;
124 CvSTASH(GvCV(gv)) = PL_curstash;
55d729e4
GS
125#ifdef USE_THREADS
126 CvOWNER(GvCV(gv)) = 0;
1cfa4ec7 127 if (!CvMUTEXP(GvCV(gv))) {
b0a484d2 128 New(666, CvMUTEXP(GvCV(gv)), 1, perl_mutex);
1cfa4ec7
GS
129 MUTEX_INIT(CvMUTEXP(GvCV(gv)));
130 }
55d729e4
GS
131#endif /* USE_THREADS */
132 if (proto) {
133 sv_setpv((SV*)GvCV(gv), proto);
134 Safefree(proto);
135 }
136 }
463ee0b2
LW
137}
138
76e3520e 139STATIC void
864dbfa3 140gv_init_sv(pTHX_ GV *gv, I32 sv_type)
a0d0e21e
LW
141{
142 switch (sv_type) {
143 case SVt_PVIO:
144 (void)GvIOn(gv);
145 break;
146 case SVt_PVAV:
147 (void)GvAVn(gv);
148 break;
149 case SVt_PVHV:
150 (void)GvHVn(gv);
151 break;
152 }
153}
154
79072805 155GV *
864dbfa3 156Perl_gv_fetchmeth(pTHX_ HV *stash, const char *name, STRLEN len, I32 level)
79072805
LW
157{
158 AV* av;
463ee0b2 159 GV* topgv;
79072805 160 GV* gv;
463ee0b2 161 GV** gvp;
748a9306 162 CV* cv;
a0d0e21e
LW
163
164 if (!stash)
165 return 0;
44a8e56a 166 if ((level > 100) || (level < -100))
3e0ccd42
JP
167 croak("Recursive inheritance detected while looking for method '%s' in package '%s'",
168 name, HvNAME(stash));
463ee0b2 169
463ee0b2 170 DEBUG_o( deb("Looking for method %s in package %s\n",name,HvNAME(stash)) );
44a8e56a
PP
171
172 gvp = (GV**)hv_fetch(stash, name, len, (level >= 0));
173 if (!gvp)
174 topgv = Nullgv;
175 else {
176 topgv = *gvp;
177 if (SvTYPE(topgv) != SVt_PVGV)
178 gv_init(topgv, stash, name, len, TRUE);
179 if (cv = GvCV(topgv)) {
180 /* If genuine method or valid cache entry, use it */
3280af22 181 if (!GvCVGEN(topgv) || GvCVGEN(topgv) == PL_sub_generation)
7a4c00b4 182 return topgv;
44a8e56a
PP
183 /* Stale cached entry: junk it */
184 SvREFCNT_dec(cv);
185 GvCV(topgv) = cv = Nullcv;
186 GvCVGEN(topgv) = 0;
748a9306 187 }
3280af22 188 else if (GvCVGEN(topgv) == PL_sub_generation)
005a453c 189 return 0; /* cache indicates sub doesn't exist */
463ee0b2 190 }
79072805 191
9607fc9c 192 gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE);
3280af22 193 av = (gvp && (gv = *gvp) && gv != (GV*)&PL_sv_undef) ? GvAV(gv) : Nullav;
9607fc9c 194
fb73857a
PP
195 /* create and re-create @.*::SUPER::ISA on demand */
196 if (!av || !SvMAGIC(av)) {
9607fc9c
PP
197 char* packname = HvNAME(stash);
198 STRLEN packlen = strlen(packname);
199
200 if (packlen >= 7 && strEQ(packname + packlen - 7, "::SUPER")) {
201 HV* basestash;
202
203 packlen -= 7;
204 basestash = gv_stashpvn(packname, packlen, TRUE);
205 gvp = (GV**)hv_fetch(basestash, "ISA", 3, FALSE);
3280af22 206 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
e858de61 207 dTHR; /* just for SvREFCNT_dec */
9607fc9c
PP
208 gvp = (GV**)hv_fetch(stash, "ISA", 3, TRUE);
209 if (!gvp || !(gv = *gvp))
210 croak("Cannot create %s::ISA", HvNAME(stash));
211 if (SvTYPE(gv) != SVt_PVGV)
212 gv_init(gv, stash, "ISA", 3, TRUE);
213 SvREFCNT_dec(GvAV(gv));
214 GvAV(gv) = (AV*)SvREFCNT_inc(av);
215 }
216 }
217 }
218
219 if (av) {
79072805 220 SV** svp = AvARRAY(av);
93965878
NIS
221 /* NOTE: No support for tied ISA */
222 I32 items = AvFILLp(av) + 1;
79072805 223 while (items--) {
79072805 224 SV* sv = *svp++;
a0d0e21e 225 HV* basestash = gv_stashsv(sv, FALSE);
9bbf4081 226 if (!basestash) {
d008e5eb 227 dTHR; /* just for ckWARN */
599cee73
PM
228 if (ckWARN(WARN_MISC))
229 warner(WARN_MISC, "Can't locate package %s for @%s::ISA",
463ee0b2 230 SvPVX(sv), HvNAME(stash));
79072805
LW
231 continue;
232 }
44a8e56a
PP
233 gv = gv_fetchmeth(basestash, name, len,
234 (level >= 0) ? level + 1 : level - 1);
235 if (gv)
236 goto gotcha;
79072805
LW
237 }
238 }
a0d0e21e 239
9607fc9c
PP
240 /* if at top level, try UNIVERSAL */
241
44a8e56a 242 if (level == 0 || level == -1) {
9607fc9c
PP
243 HV* lastchance;
244
dc437b57 245 if (lastchance = gv_stashpvn("UNIVERSAL", 9, FALSE)) {
44a8e56a
PP
246 if (gv = gv_fetchmeth(lastchance, name, len,
247 (level >= 0) ? level + 1 : level - 1)) {
248 gotcha:
dc848c6f
PP
249 /*
250 * Cache method in topgv if:
251 * 1. topgv has no synonyms (else inheritance crosses wires)
252 * 2. method isn't a stub (else AUTOLOAD fails spectacularly)
253 */
254 if (topgv &&
255 GvREFCNT(topgv) == 1 &&
256 (cv = GvCV(gv)) &&
257 (CvROOT(cv) || CvXSUB(cv)))
258 {
44a8e56a
PP
259 if (cv = GvCV(topgv))
260 SvREFCNT_dec(cv);
261 GvCV(topgv) = (CV*)SvREFCNT_inc(GvCV(gv));
3280af22 262 GvCVGEN(topgv) = PL_sub_generation;
44a8e56a 263 }
a0d0e21e
LW
264 return gv;
265 }
005a453c
JP
266 else if (topgv && GvREFCNT(topgv) == 1) {
267 /* cache the fact that the method is not defined */
3280af22 268 GvCVGEN(topgv) = PL_sub_generation;
005a453c 269 }
a0d0e21e
LW
270 }
271 }
272
79072805
LW
273 return 0;
274}
275
276GV *
864dbfa3 277Perl_gv_fetchmethod(pTHX_ HV *stash, const char *name)
463ee0b2 278{
dc848c6f
PP
279 return gv_fetchmethod_autoload(stash, name, TRUE);
280}
281
282GV *
864dbfa3 283Perl_gv_fetchmethod_autoload(pTHX_ HV *stash, const char *name, I32 autoload)
dc848c6f 284{
0f15f207 285 dTHR;
08105a92
GS
286 register const char *nend;
287 const char *nsplit = 0;
a0d0e21e 288 GV* gv;
463ee0b2
LW
289
290 for (nend = name; *nend; nend++) {
9607fc9c 291 if (*nend == '\'')
a0d0e21e 292 nsplit = nend;
9607fc9c
PP
293 else if (*nend == ':' && *(nend + 1) == ':')
294 nsplit = ++nend;
a0d0e21e
LW
295 }
296 if (nsplit) {
08105a92 297 const char *origname = name;
a0d0e21e 298 name = nsplit + 1;
a0d0e21e
LW
299 if (*nsplit == ':')
300 --nsplit;
9607fc9c
PP
301 if ((nsplit - origname) == 5 && strnEQ(origname, "SUPER", 5)) {
302 /* ->SUPER::method should really be looked up in original stash */
fc36a67e 303 SV *tmpstr = sv_2mortal(newSVpvf("%s::SUPER",
3280af22 304 HvNAME(PL_curcop->cop_stash)));
9607fc9c
PP
305 stash = gv_stashpvn(SvPVX(tmpstr), SvCUR(tmpstr), TRUE);
306 DEBUG_o( deb("Treating %s as %s::%s\n",
307 origname, HvNAME(stash), name) );
4633a7c4 308 }
9607fc9c
PP
309 else
310 stash = gv_stashpvn(origname, nsplit - origname, TRUE);
4633a7c4
LW
311 }
312
9607fc9c 313 gv = gv_fetchmeth(stash, name, nend - name, 0);
a0d0e21e 314 if (!gv) {
702887b4 315 if (strEQ(name,"import"))
3280af22 316 gv = (GV*)&PL_sv_yes;
dc848c6f 317 else if (autoload)
54310121 318 gv = gv_autoload4(stash, name, nend - name, TRUE);
463ee0b2 319 }
dc848c6f
PP
320 else if (autoload) {
321 CV* cv = GvCV(gv);
09280a33
CS
322 if (!CvROOT(cv) && !CvXSUB(cv)) {
323 GV* stubgv;
324 GV* autogv;
325
326 if (CvANON(cv))
327 stubgv = gv;
328 else {
329 stubgv = CvGV(cv);
330 if (GvCV(stubgv) != cv) /* orphaned import */
331 stubgv = gv;
332 }
333 autogv = gv_autoload4(GvSTASH(stubgv),
334 GvNAME(stubgv), GvNAMELEN(stubgv), TRUE);
dc848c6f
PP
335 if (autogv)
336 gv = autogv;
337 }
338 }
44a8e56a
PP
339
340 return gv;
341}
342
343GV*
864dbfa3 344Perl_gv_autoload4(pTHX_ HV *stash, const char *name, STRLEN len, I32 method)
44a8e56a 345{
d008e5eb 346 dTHR;
44a8e56a
PP
347 static char autoload[] = "AUTOLOAD";
348 static STRLEN autolen = 8;
349 GV* gv;
350 CV* cv;
351 HV* varstash;
352 GV* vargv;
353 SV* varsv;
354
355 if (len == autolen && strnEQ(name, autoload, autolen))
356 return Nullgv;
dc848c6f
PP
357 if (!(gv = gv_fetchmeth(stash, autoload, autolen, FALSE)))
358 return Nullgv;
359 cv = GvCV(gv);
360
361 /*
362 * Inheriting AUTOLOAD for non-methods works ... for now.
363 */
599cee73
PM
364 if (ckWARN(WARN_DEPRECATED) && !method &&
365 (GvCVGEN(gv) || GvSTASH(gv) != stash))
366 warner(WARN_DEPRECATED,
dc848c6f
PP
367 "Use of inherited AUTOLOAD for non-method %s::%.*s() is deprecated",
368 HvNAME(stash), (int)len, name);
44a8e56a
PP
369
370 /*
371 * Given &FOO::AUTOLOAD, set $FOO::AUTOLOAD to desired function name.
372 * The subroutine's original name may not be "AUTOLOAD", so we don't
373 * use that, but for lack of anything better we will use the sub's
374 * original package to look up $AUTOLOAD.
375 */
376 varstash = GvSTASH(CvGV(cv));
377 vargv = *(GV**)hv_fetch(varstash, autoload, autolen, TRUE);
378 if (!isGV(vargv))
379 gv_init(vargv, varstash, autoload, autolen, FALSE);
380 varsv = GvSV(vargv);
381 sv_setpv(varsv, HvNAME(stash));
382 sv_catpvn(varsv, "::", 2);
383 sv_catpvn(varsv, name, len);
384 SvTAINTED_off(varsv);
a0d0e21e
LW
385 return gv;
386}
387
388HV*
864dbfa3 389Perl_gv_stashpv(pTHX_ const char *name, I32 create)
a0d0e21e 390{
dc437b57
PP
391 return gv_stashpvn(name, strlen(name), create);
392}
393
394HV*
864dbfa3 395Perl_gv_stashpvn(pTHX_ const char *name, U32 namelen, I32 create)
dc437b57 396{
46fc3d4c
PP
397 char smallbuf[256];
398 char *tmpbuf;
a0d0e21e
LW
399 HV *stash;
400 GV *tmpgv;
dc437b57 401
46fc3d4c
PP
402 if (namelen + 3 < sizeof smallbuf)
403 tmpbuf = smallbuf;
404 else
405 New(606, tmpbuf, namelen + 3, char);
dc437b57
PP
406 Copy(name,tmpbuf,namelen,char);
407 tmpbuf[namelen++] = ':';
408 tmpbuf[namelen++] = ':';
409 tmpbuf[namelen] = '\0';
46fc3d4c
PP
410 tmpgv = gv_fetchpv(tmpbuf, create, SVt_PVHV);
411 if (tmpbuf != smallbuf)
412 Safefree(tmpbuf);
a0d0e21e
LW
413 if (!tmpgv)
414 return 0;
415 if (!GvHV(tmpgv))
416 GvHV(tmpgv) = newHV();
417 stash = GvHV(tmpgv);
418 if (!HvNAME(stash))
419 HvNAME(stash) = savepv(name);
420 return stash;
463ee0b2
LW
421}
422
a0d0e21e 423HV*
864dbfa3 424Perl_gv_stashsv(pTHX_ SV *sv, I32 create)
a0d0e21e 425{
dc437b57
PP
426 register char *ptr;
427 STRLEN len;
428 ptr = SvPV(sv,len);
429 return gv_stashpvn(ptr, len, create);
a0d0e21e
LW
430}
431
432
463ee0b2 433GV *
864dbfa3 434Perl_gv_fetchpv(pTHX_ const char *nambeg, I32 add, I32 sv_type)
79072805 435{
11343788 436 dTHR;
08105a92 437 register const char *name = nambeg;
463ee0b2 438 register GV *gv = 0;
79072805 439 GV**gvp;
79072805 440 I32 len;
08105a92 441 register const char *namend;
463ee0b2 442 HV *stash = 0;
9607fc9c 443 U32 add_gvflags = 0;
79072805 444
c07a80fd
PP
445 if (*name == '*' && isALPHA(name[1])) /* accidental stringify on a GV? */
446 name++;
447
79072805 448 for (namend = name; *namend; namend++) {
463ee0b2
LW
449 if ((*namend == '\'' && namend[1]) ||
450 (*namend == ':' && namend[1] == ':'))
451 {
463ee0b2 452 if (!stash)
3280af22 453 stash = PL_defstash;
dc437b57 454 if (!stash || !SvREFCNT(stash)) /* symbol table under destruction */
a0d0e21e 455 return Nullgv;
463ee0b2 456
85e6fe83
LW
457 len = namend - name;
458 if (len > 0) {
3c78fafa 459 char smallbuf[256];
62b57502 460 char *tmpbuf;
62b57502 461
3c78fafa
GS
462 if (len + 3 < sizeof smallbuf)
463 tmpbuf = smallbuf;
62b57502
MB
464 else
465 New(601, tmpbuf, len+3, char);
a0d0e21e
LW
466 Copy(name, tmpbuf, len, char);
467 tmpbuf[len++] = ':';
468 tmpbuf[len++] = ':';
469 tmpbuf[len] = '\0';
463ee0b2 470 gvp = (GV**)hv_fetch(stash,tmpbuf,len,add);
6fa846a0 471 gv = gvp ? *gvp : Nullgv;
3280af22 472 if (gv && gv != (GV*)&PL_sv_undef) {
6fa846a0 473 if (SvTYPE(gv) != SVt_PVGV)
0f303493 474 gv_init(gv, stash, tmpbuf, len, (add & GV_ADDMULTI));
6fa846a0
GS
475 else
476 GvMULTI_on(gv);
477 }
3c78fafa 478 if (tmpbuf != smallbuf)
62b57502 479 Safefree(tmpbuf);
3280af22 480 if (!gv || gv == (GV*)&PL_sv_undef)
a0d0e21e 481 return Nullgv;
85e6fe83 482
463ee0b2
LW
483 if (!(stash = GvHV(gv)))
484 stash = GvHV(gv) = newHV();
85e6fe83 485
463ee0b2 486 if (!HvNAME(stash))
a0d0e21e 487 HvNAME(stash) = savepvn(nambeg, namend - nambeg);
463ee0b2
LW
488 }
489
490 if (*namend == ':')
491 namend++;
492 namend++;
493 name = namend;
494 if (!*name)
3280af22 495 return gv ? gv : (GV*)*hv_fetch(PL_defstash, "main::", 6, TRUE);
79072805 496 }
79072805 497 }
a0d0e21e
LW
498 len = namend - name;
499 if (!len)
500 len = 1;
463ee0b2
LW
501
502 /* No stash in name, so see how we can default */
503
504 if (!stash) {
b86a2fa7
GS
505 if (isIDFIRST(*name)
506 || (IN_UTF8 && ((*name & 0xc0) == 0xc0) && isIDFIRST_utf8((U8*)name)))
507 {
9607fc9c
PP
508 bool global = FALSE;
509
463ee0b2 510 if (isUPPER(*name)) {
9d116dd7
JH
511 if (*name == 'S' && (
512 strEQ(name, "SIG") ||
513 strEQ(name, "STDIN") ||
514 strEQ(name, "STDOUT") ||
515 strEQ(name, "STDERR")))
516 global = TRUE;
517 else if (*name == 'I' && strEQ(name, "INC"))
518 global = TRUE;
519 else if (*name == 'E' && strEQ(name, "ENV"))
520 global = TRUE;
463ee0b2
LW
521 else if (*name == 'A' && (
522 strEQ(name, "ARGV") ||
9d116dd7 523 strEQ(name, "ARGVOUT")))
463ee0b2
LW
524 global = TRUE;
525 }
526 else if (*name == '_' && !name[1])
527 global = TRUE;
9607fc9c 528
463ee0b2 529 if (global)
3280af22
NIS
530 stash = PL_defstash;
531 else if ((COP*)PL_curcop == &PL_compiling) {
532 stash = PL_curstash;
533 if (add && (PL_hints & HINT_STRICT_VARS) &&
748a9306
LW
534 sv_type != SVt_PVCV &&
535 sv_type != SVt_PVGV &&
4633a7c4 536 sv_type != SVt_PVFM &&
c07a80fd 537 sv_type != SVt_PVIO &&
377b8fbc 538 !(len == 1 && sv_type == SVt_PV && strchr("ab",*name)) )
748a9306 539 {
4633a7c4
LW
540 gvp = (GV**)hv_fetch(stash,name,len,0);
541 if (!gvp ||
3280af22 542 *gvp == (GV*)&PL_sv_undef ||
a5f75d66
AD
543 SvTYPE(*gvp) != SVt_PVGV)
544 {
4633a7c4 545 stash = 0;
a5f75d66
AD
546 }
547 else if (sv_type == SVt_PV && !GvIMPORTED_SV(*gvp) ||
548 sv_type == SVt_PVAV && !GvIMPORTED_AV(*gvp) ||
549 sv_type == SVt_PVHV && !GvIMPORTED_HV(*gvp) )
4633a7c4 550 {
a5f75d66 551 warn("Variable \"%c%s\" is not imported",
4633a7c4
LW
552 sv_type == SVt_PVAV ? '@' :
553 sv_type == SVt_PVHV ? '%' : '$',
554 name);
8ebc5c01 555 if (GvCVu(*gvp))
4633a7c4 556 warn("(Did you mean &%s instead?)\n", name);
a0d0e21e 557 stash = 0;
4633a7c4 558 }
a0d0e21e 559 }
85e6fe83 560 }
463ee0b2 561 else
3280af22 562 stash = PL_curcop->cop_stash;
463ee0b2
LW
563 }
564 else
3280af22 565 stash = PL_defstash;
463ee0b2
LW
566 }
567
568 /* By this point we should have a stash and a name */
569
a0d0e21e 570 if (!stash) {
93233ece 571 if (!add)
a0d0e21e 572 return Nullgv;
462551cc 573 {
2c4aebbd
T
574 char sv_type_char = ((sv_type == SVt_PV) ? '$'
575 : (sv_type == SVt_PVAV) ? '@'
576 : (sv_type == SVt_PVHV) ? '%'
577 : 0);
578 if (sv_type_char)
579 warn("Global symbol \"%c%s\" requires explicit package name",
580 sv_type_char, name);
581 else
93233ece
CS
582 warn("Global symbol \"%s\" requires explicit package name",
583 name);
a0d0e21e 584 }
3280af22
NIS
585 ++PL_error_count;
586 stash = PL_curstash ? PL_curstash : PL_defstash; /* avoid core dumps */
93233ece
CS
587 add_gvflags = ((sv_type == SVt_PV) ? GVf_IMPORTED_SV
588 : (sv_type == SVt_PVAV) ? GVf_IMPORTED_AV
589 : (sv_type == SVt_PVHV) ? GVf_IMPORTED_HV
590 : 0);
a0d0e21e
LW
591 }
592
593 if (!SvREFCNT(stash)) /* symbol table under destruction */
594 return Nullgv;
595
79072805 596 gvp = (GV**)hv_fetch(stash,name,len,add);
3280af22 597 if (!gvp || *gvp == (GV*)&PL_sv_undef)
79072805
LW
598 return Nullgv;
599 gv = *gvp;
600 if (SvTYPE(gv) == SVt_PVGV) {
a0d0e21e 601 if (add) {
a5f75d66 602 GvMULTI_on(gv);
a0d0e21e
LW
603 gv_init_sv(gv, sv_type);
604 }
79072805 605 return gv;
55d729e4
GS
606 } else if (add & GV_NOINIT) {
607 return gv;
79072805 608 }
93a17b20
LW
609
610 /* Adding a new symbol */
611
55d729e4 612 if (add & GV_ADDWARN)
a0d0e21e 613 warn("Had to create %s unexpectedly", nambeg);
55d729e4 614 gv_init(gv, stash, name, len, add & GV_ADDMULTI);
a0d0e21e 615 gv_init_sv(gv, sv_type);
9607fc9c 616 GvFLAGS(gv) |= add_gvflags;
93a17b20
LW
617
618 /* set up magic where warranted */
619 switch (*name) {
a0d0e21e
LW
620 case 'A':
621 if (strEQ(name, "ARGV")) {
622 IoFLAGS(GvIOn(gv)) |= IOf_ARGV|IOf_START;
623 }
624 break;
a0d0e21e
LW
625 case 'E':
626 if (strnEQ(name, "EXPORT", 6))
a5f75d66 627 GvMULTI_on(gv);
a0d0e21e 628 break;
463ee0b2
LW
629 case 'I':
630 if (strEQ(name, "ISA")) {
631 AV* av = GvAVn(gv);
a5f75d66 632 GvMULTI_on(gv);
a0d0e21e 633 sv_magic((SV*)av, (SV*)gv, 'I', Nullch, 0);
93965878 634 /* NOTE: No support for tied ISA */
55d729e4
GS
635 if ((add & GV_ADDMULTI) && strEQ(nambeg,"AnyDBM_File::ISA")
636 && AvFILLp(av) == -1)
85e6fe83 637 {
a0d0e21e 638 char *pname;
79cb57f6 639 av_push(av, newSVpvn(pname = "NDBM_File",9));
dc437b57 640 gv_stashpvn(pname, 9, TRUE);
79cb57f6 641 av_push(av, newSVpvn(pname = "DB_File",7));
dc437b57 642 gv_stashpvn(pname, 7, TRUE);
79cb57f6 643 av_push(av, newSVpvn(pname = "GDBM_File",9));
dc437b57 644 gv_stashpvn(pname, 9, TRUE);
79cb57f6 645 av_push(av, newSVpvn(pname = "SDBM_File",9));
dc437b57 646 gv_stashpvn(pname, 9, TRUE);
79cb57f6 647 av_push(av, newSVpvn(pname = "ODBM_File",9));
dc437b57 648 gv_stashpvn(pname, 9, TRUE);
85e6fe83 649 }
463ee0b2
LW
650 }
651 break;
a0d0e21e
LW
652 case 'O':
653 if (strEQ(name, "OVERLOAD")) {
654 HV* hv = GvHVn(gv);
a5f75d66 655 GvMULTI_on(gv);
ea0efc06 656 hv_magic(hv, gv, 'A');
a0d0e21e
LW
657 }
658 break;
93a17b20
LW
659 case 'S':
660 if (strEQ(name, "SIG")) {
661 HV *hv;
dc437b57 662 I32 i;
3280af22
NIS
663 PL_siggv = gv;
664 GvMULTI_on(PL_siggv);
665 hv = GvHVn(PL_siggv);
666 hv_magic(hv, PL_siggv, 'S');
22c35a8c 667 for(i=1;PL_sig_name[i];i++) {
dc437b57 668 SV ** init;
22c35a8c 669 init=hv_fetch(hv,PL_sig_name[i],strlen(PL_sig_name[i]),1);
dc437b57 670 if(init)
3280af22 671 sv_setsv(*init,&PL_sv_undef);
22c35a8c
GS
672 PL_psig_ptr[i] = 0;
673 PL_psig_name[i] = 0;
dc437b57 674 }
93a17b20
LW
675 }
676 break;
677
678 case '&':
463ee0b2
LW
679 if (len > 1)
680 break;
3280af22
NIS
681 PL_ampergv = gv;
682 PL_sawampersand = TRUE;
a0d0e21e 683 goto ro_magicalize;
93a17b20
LW
684
685 case '`':
463ee0b2
LW
686 if (len > 1)
687 break;
3280af22
NIS
688 PL_leftgv = gv;
689 PL_sawampersand = TRUE;
a0d0e21e 690 goto ro_magicalize;
93a17b20
LW
691
692 case '\'':
463ee0b2
LW
693 if (len > 1)
694 break;
3280af22
NIS
695 PL_rightgv = gv;
696 PL_sawampersand = TRUE;
a0d0e21e 697 goto ro_magicalize;
93a17b20
LW
698
699 case ':':
463ee0b2
LW
700 if (len > 1)
701 break;
3280af22 702 sv_setpv(GvSV(gv),PL_chopset);
93a17b20
LW
703 goto magicalize;
704
ff0cee69
PP
705 case '?':
706 if (len > 1)
707 break;
708#ifdef COMPLEX_STATUS
07f14f54 709 (void)SvUPGRADE(GvSV(gv), SVt_PVLV);
ff0cee69
PP
710#endif
711 goto magicalize;
712
067391ea 713 case '!':
4318d5a0 714 if (len > 1)
067391ea 715 break;
3280af22 716 if (sv_type > SVt_PV && PL_curcop != &PL_compiling) {
067391ea
GS
717 HV* stash = gv_stashpvn("Errno",5,FALSE);
718 if(!stash || !(gv_fetchmethod(stash, "TIEHASH"))) {
719 dSP;
720 PUTBACK;
721 perl_require_pv("Errno.pm");
722 SPAGAIN;
723 stash = gv_stashpvn("Errno",5,FALSE);
724 if (!stash || !(gv_fetchmethod(stash, "TIEHASH")))
1d2dff63 725 croak("Can't use %%! because Errno.pm is not available");
067391ea
GS
726 }
727 }
728 goto magicalize;
6cef1e77
IZ
729 case '-':
730 if (len > 1)
731 break;
732 else {
733 AV* av = GvAVn(gv);
734 sv_magic((SV*)av, Nullsv, 'D', Nullch, 0);
735 }
736 goto magicalize;
93a17b20 737 case '#':
a0d0e21e 738 case '*':
599cee73
PM
739 if (ckWARN(WARN_DEPRECATED) && len == 1 && sv_type == SVt_PV)
740 warner(WARN_DEPRECATED, "Use of $%s is deprecated", name);
a0d0e21e
LW
741 /* FALL THROUGH */
742 case '[':
93a17b20
LW
743 case '^':
744 case '~':
745 case '=':
93a17b20
LW
746 case '%':
747 case '.':
93a17b20
LW
748 case '(':
749 case ')':
750 case '<':
751 case '>':
752 case ',':
753 case '\\':
754 case '/':
93a17b20 755 case '|':
748a9306 756 case '\001':
599cee73 757 case '\002':
49460fe6 758 case '\003':
93a17b20 759 case '\004':
ad8898e0 760 case '\005':
748a9306 761 case '\006':
a0d0e21e 762 case '\010':
9d116dd7 763 case '\011': /* NOT \t in EBCDIC */
ad8898e0 764 case '\017':
93a17b20
LW
765 case '\020':
766 case '\024':
767 case '\027':
463ee0b2
LW
768 if (len > 1)
769 break;
770 goto magicalize;
6cef1e77
IZ
771 case '\023':
772 if (len > 1)
773 break;
774 goto ro_magicalize;
463ee0b2 775
a0d0e21e 776 case '+':
6cef1e77
IZ
777 if (len > 1)
778 break;
779 else {
780 AV* av = GvAVn(gv);
781 sv_magic((SV*)av, (SV*)av, 'D', Nullch, 0);
782 }
783 /* FALL THROUGH */
463ee0b2
LW
784 case '1':
785 case '2':
786 case '3':
787 case '4':
788 case '5':
789 case '6':
790 case '7':
791 case '8':
792 case '9':
a0d0e21e
LW
793 ro_magicalize:
794 SvREADONLY_on(GvSV(gv));
93a17b20 795 magicalize:
463ee0b2 796 sv_magic(GvSV(gv), (SV*)gv, 0, name, len);
93a17b20
LW
797 break;
798
799 case '\014':
463ee0b2
LW
800 if (len > 1)
801 break;
93a17b20 802 sv_setpv(GvSV(gv),"\f");
3280af22 803 PL_formfeed = GvSV(gv);
93a17b20
LW
804 break;
805 case ';':
463ee0b2
LW
806 if (len > 1)
807 break;
93a17b20
LW
808 sv_setpv(GvSV(gv),"\034");
809 break;
463ee0b2
LW
810 case ']':
811 if (len == 1) {
f86702cc 812 SV *sv = GvSV(gv);
07f14f54 813 (void)SvUPGRADE(sv, SVt_PVNV);
3280af22 814 sv_setpv(sv, PL_patchlevel);
f86702cc
PP
815 (void)sv_2nv(sv);
816 SvREADONLY_on(sv);
93a17b20
LW
817 }
818 break;
79072805 819 }
93a17b20 820 return gv;
79072805
LW
821}
822
823void
864dbfa3 824Perl_gv_fullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
79072805
LW
825{
826 HV *hv = GvSTASH(gv);
f967eb5f
PP
827 if (!hv) {
828 SvOK_off(sv);
79072805 829 return;
f967eb5f
PP
830 }
831 sv_setpv(sv, prefix ? prefix : "");
79072805 832 sv_catpv(sv,HvNAME(hv));
463ee0b2 833 sv_catpvn(sv,"::", 2);
79072805
LW
834 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
835}
836
837void
864dbfa3 838Perl_gv_efullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
79072805 839{
f967eb5f 840 GV *egv = GvEGV(gv);
748a9306
LW
841 if (!egv)
842 egv = gv;
f6aff53a
PP
843 gv_fullname3(sv, egv, prefix);
844}
845
846/* XXX compatibility with versions <= 5.003. */
847void
864dbfa3 848Perl_gv_fullname(pTHX_ SV *sv, GV *gv)
f6aff53a
PP
849{
850 gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : "");
851}
852
853/* XXX compatibility with versions <= 5.003. */
854void
864dbfa3 855Perl_gv_efullname(pTHX_ SV *sv, GV *gv)
f6aff53a
PP
856{
857 gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : "");
79072805
LW
858}
859
860IO *
864dbfa3 861Perl_newIO(pTHX)
79072805 862{
11343788 863 dTHR;
79072805 864 IO *io;
8990e307
LW
865 GV *iogv;
866
867 io = (IO*)NEWSV(0,0);
a0d0e21e 868 sv_upgrade((SV *)io,SVt_PVIO);
8990e307
LW
869 SvREFCNT(io) = 1;
870 SvOBJECT_on(io);
c9de509e 871 iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV);
5f2d631d
GS
872 /* unless exists($main::{FileHandle}) and defined(%main::FileHandle::) */
873 if (!(iogv && GvHV(iogv) && HvARRAY(GvHV(iogv))))
c9de509e 874 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
8990e307 875 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
79072805
LW
876 return io;
877}
878
879void
864dbfa3 880Perl_gv_check(pTHX_ HV *stash)
79072805 881{
11343788 882 dTHR;
79072805
LW
883 register HE *entry;
884 register I32 i;
885 register GV *gv;
463ee0b2 886 HV *hv;
a0d0e21e 887 GV *filegv;
463ee0b2 888
8990e307
LW
889 if (!HvARRAY(stash))
890 return;
a0d0e21e 891 for (i = 0; i <= (I32) HvMAX(stash); i++) {
dc437b57
PP
892 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
893 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
894 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)) && HvNAME(hv))
a0d0e21e 895 {
19b6c847 896 if (hv != PL_defstash && hv != stash)
a0d0e21e
LW
897 gv_check(hv); /* nested package */
898 }
dc437b57
PP
899 else if (isALPHA(*HeKEY(entry))) {
900 gv = (GV*)HeVAL(entry);
55d729e4 901 if (SvTYPE(gv) != SVt_PVGV || GvMULTI(gv))
463ee0b2 902 continue;
3280af22 903 PL_curcop->cop_line = GvLINE(gv);
a0d0e21e 904 filegv = GvFILEGV(gv);
3280af22 905 PL_curcop->cop_filegv = filegv;
a5f75d66 906 if (filegv && GvMULTI(filegv)) /* Filename began with slash */
8990e307 907 continue;
599cee73
PM
908 warner(WARN_ONCE,
909 "Name \"%s::%s\" used only once: possible typo",
a0d0e21e 910 HvNAME(stash), GvNAME(gv));
463ee0b2 911 }
79072805
LW
912 }
913 }
914}
915
916GV *
864dbfa3 917Perl_newGVgen(pTHX_ char *pack)
79072805 918{
3280af22 919 return gv_fetchpv(form("%s::_GEN_%ld", pack, (long)PL_gensym++),
46fc3d4c 920 TRUE, SVt_PVGV);
79072805
LW
921}
922
923/* hopefully this is only called on local symbol table entries */
924
925GP*
864dbfa3 926Perl_gp_ref(pTHX_ GP *gp)
79072805
LW
927{
928 gp->gp_refcnt++;
44a8e56a
PP
929 if (gp->gp_cv) {
930 if (gp->gp_cvgen) {
931 /* multi-named GPs cannot be used for method cache */
932 SvREFCNT_dec(gp->gp_cv);
933 gp->gp_cv = Nullcv;
934 gp->gp_cvgen = 0;
935 }
936 else {
937 /* Adding a new name to a subroutine invalidates method cache */
3280af22 938 PL_sub_generation++;
44a8e56a
PP
939 }
940 }
79072805 941 return gp;
79072805
LW
942}
943
944void
864dbfa3 945Perl_gp_free(pTHX_ GV *gv)
79072805 946{
79072805 947 GP* gp;
377b8fbc 948 CV* cv;
79072805
LW
949
950 if (!gv || !(gp = GvGP(gv)))
951 return;
952 if (gp->gp_refcnt == 0) {
953 warn("Attempt to free unreferenced glob pointers");
954 return;
955 }
44a8e56a
PP
956 if (gp->gp_cv) {
957 /* Deleting the name of a subroutine invalidates method cache */
3280af22 958 PL_sub_generation++;
44a8e56a 959 }
748a9306
LW
960 if (--gp->gp_refcnt > 0) {
961 if (gp->gp_egv == gv)
962 gp->gp_egv = 0;
79072805 963 return;
748a9306 964 }
79072805 965
8990e307
LW
966 SvREFCNT_dec(gp->gp_sv);
967 SvREFCNT_dec(gp->gp_av);
968 SvREFCNT_dec(gp->gp_hv);
377b8fbc 969 SvREFCNT_dec(gp->gp_io);
a6006777 970 SvREFCNT_dec(gp->gp_cv);
748a9306
LW
971 SvREFCNT_dec(gp->gp_form);
972
79072805
LW
973 Safefree(gp);
974 GvGP(gv) = 0;
975}
976
977#if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
978#define MICROPORT
979#endif
980
981#ifdef MICROPORT /* Microport 2.4 hack */
982AV *GvAVn(gv)
983register GV *gv;
984{
985 if (GvGP(gv)->gp_av)
986 return GvGP(gv)->gp_av;
987 else
988 return GvGP(gv_AVadd(gv))->gp_av;
989}
990
991HV *GvHVn(gv)
992register GV *gv;
993{
994 if (GvGP(gv)->gp_hv)
995 return GvGP(gv)->gp_hv;
996 else
997 return GvGP(gv_HVadd(gv))->gp_hv;
998}
999#endif /* Microport 2.4 hack */
a0d0e21e 1000
a0d0e21e
LW
1001/* Updates and caches the CV's */
1002
1003bool
864dbfa3 1004Perl_Gv_AMupdate(pTHX_ HV *stash)
a0d0e21e 1005{
11343788 1006 dTHR;
a0d0e21e
LW
1007 GV** gvp;
1008 HV* hv;
1009 GV* gv;
1010 CV* cv;
1011 MAGIC* mg=mg_find((SV*)stash,'c');
8ac85365 1012 AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL;
a6006777 1013 AMT amt;
2d8e6c8d 1014 STRLEN n_a;
a0d0e21e 1015
3280af22
NIS
1016 if (mg && amtp->was_ok_am == PL_amagic_generation
1017 && amtp->was_ok_sub == PL_sub_generation)
a6006777
PP
1018 return AMT_AMAGIC(amtp);
1019 if (amtp && AMT_AMAGIC(amtp)) { /* Have table. */
748a9306 1020 int i;
a6006777 1021 for (i=1; i<NofAMmeth; i++) {
748a9306
LW
1022 if (amtp->table[i]) {
1023 SvREFCNT_dec(amtp->table[i]);
1024 }
1025 }
1026 }
a0d0e21e
LW
1027 sv_unmagic((SV*)stash, 'c');
1028
1029 DEBUG_o( deb("Recalcing overload magic in package %s\n",HvNAME(stash)) );
1030
3280af22
NIS
1031 amt.was_ok_am = PL_amagic_generation;
1032 amt.was_ok_sub = PL_sub_generation;
a6006777
PP
1033 amt.fallback = AMGfallNO;
1034 amt.flags = 0;
1035
1036#ifdef OVERLOAD_VIA_HASH
1037 gvp=(GV**)hv_fetch(stash,"OVERLOAD",8,FALSE); /* A shortcut */
3280af22 1038 if (gvp && ((gv = *gvp) != (GV*)&PL_sv_undef && (hv = GvHV(gv)))) {
a0d0e21e
LW
1039 int filled=0;
1040 int i;
1041 char *cp;
a0d0e21e
LW
1042 SV* sv;
1043 SV** svp;
a0d0e21e 1044
22c35a8c 1045 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
a0d0e21e 1046
22c35a8c 1047 if (( cp = (char *)PL_AMG_names[0] ) &&
a6006777 1048 (svp = (SV**)hv_fetch(hv,cp,strlen(cp),FALSE)) && (sv = *svp)) {
a0d0e21e
LW
1049 if (SvTRUE(sv)) amt.fallback=AMGfallYES;
1050 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
1051 }
a6006777
PP
1052 for (i = 1; i < NofAMmeth; i++) {
1053 cv = 0;
22c35a8c 1054 cp = (char *)PL_AMG_names[i];
a6006777
PP
1055
1056 svp = (SV**)hv_fetch(hv, cp, strlen(cp), FALSE);
3280af22 1057 if (svp && ((sv = *svp) != &PL_sv_undef)) {
a0d0e21e
LW
1058 switch (SvTYPE(sv)) {
1059 default:
1060 if (!SvROK(sv)) {
1061 if (!SvOK(sv)) break;
2d8e6c8d 1062 gv = gv_fetchmethod(stash, SvPV(sv, n_a));
a0d0e21e
LW
1063 if (gv) cv = GvCV(gv);
1064 break;
1065 }
1066 cv = (CV*)SvRV(sv);
1067 if (SvTYPE(cv) == SVt_PVCV)
1068 break;
1069 /* FALL THROUGH */
1070 case SVt_PVHV:
1071 case SVt_PVAV:
a6006777 1072 croak("Not a subroutine reference in overload table");
a0d0e21e
LW
1073 return FALSE;
1074 case SVt_PVCV:
8ebc5c01
PP
1075 cv = (CV*)sv;
1076 break;
a0d0e21e 1077 case SVt_PVGV:
8ebc5c01 1078 if (!(cv = GvCVu((GV*)sv)))
f6ec51f7 1079 cv = sv_2cv(sv, &stash, &gv, FALSE);
8ebc5c01 1080 break;
a0d0e21e
LW
1081 }
1082 if (cv) filled=1;
1083 else {
a6006777 1084 croak("Method for operation %s not found in package %.256s during blessing\n",
a0d0e21e
LW
1085 cp,HvNAME(stash));
1086 return FALSE;
1087 }
1088 }
a6006777
PP
1089#else
1090 {
1091 int filled = 0;
1092 int i;
9607fc9c 1093 const char *cp;
a6006777
PP
1094 SV* sv = NULL;
1095 SV** svp;
1096
22c35a8c 1097 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
a6006777 1098
22c35a8c 1099 if ( cp = PL_AMG_names[0] ) {
a6006777 1100 /* Try to find via inheritance. */
774d564b 1101 gv = gv_fetchmeth(stash, "()", 2, -1); /* A cookie: "()". */
a6006777
PP
1102 if (gv) sv = GvSV(gv);
1103
774d564b 1104 if (!gv) goto no_table;
a6006777
PP
1105 else if (SvTRUE(sv)) amt.fallback=AMGfallYES;
1106 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
1107 }
1108
1109 for (i = 1; i < NofAMmeth; i++) {
22c35a8c 1110 SV *cookie = sv_2mortal(newSVpvf("(%s", cp = PL_AMG_names[i]));
44a8e56a
PP
1111 DEBUG_o( deb("Checking overloading of `%s' in package `%.256s'\n",
1112 cp, HvNAME(stash)) );
46fc3d4c
PP
1113 /* don't fill the cache while looking up! */
1114 gv = gv_fetchmeth(stash, SvPVX(cookie), SvCUR(cookie), -1);
1115 cv = 0;
44a8e56a 1116 if(gv && (cv = GvCV(gv))) {
44a8e56a
PP
1117 if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil")
1118 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) {
1119 /* GvSV contains the name of the method. */
1120 GV *ngv;
1121
1122 DEBUG_o( deb("Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n",
2d8e6c8d 1123 SvPV(GvSV(gv), n_a), cp, HvNAME(stash)) );
dc848c6f
PP
1124 if (!SvPOK(GvSV(gv))
1125 || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)),
1126 FALSE)))
1127 {
44a8e56a
PP
1128 /* Can be an import stub (created by `can'). */
1129 if (GvCVGEN(gv)) {
1130 croak("Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'",
1131 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1132 cp, HvNAME(stash));
1133 } else
3f4520fe 1134 croak("Can't resolve method `%.256s' overloading `%s' in package `%.256s'",
44a8e56a
PP
1135 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1136 cp, HvNAME(stash));
1137 }
dc848c6f 1138 cv = GvCV(gv = ngv);
44a8e56a
PP
1139 }
1140 DEBUG_o( deb("Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n",
1141 cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))),
1142 GvNAME(CvGV(cv))) );
1143 filled = 1;
1144 }
a6006777
PP
1145#endif
1146 amt.table[i]=(CV*)SvREFCNT_inc(cv);
a0d0e21e 1147 }
a0d0e21e 1148 if (filled) {
a6006777
PP
1149 AMT_AMAGIC_on(&amt);
1150 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMT));
a0d0e21e
LW
1151 return TRUE;
1152 }
1153 }
a6006777 1154 /* Here we have no table: */
774d564b 1155 no_table:
a6006777
PP
1156 AMT_AMAGIC_off(&amt);
1157 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMTS));
a0d0e21e
LW
1158 return FALSE;
1159}
1160
a0d0e21e 1161SV*
864dbfa3 1162Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags)
a0d0e21e 1163{
11343788 1164 dTHR;
a0d0e21e
LW
1165 MAGIC *mg;
1166 CV *cv;
1167 CV **cvp=NULL, **ocvp=NULL;
1168 AMT *amtp, *oamtp;
1169 int fl=0, off, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
ee239bfe 1170 int postpr = 0, force_cpy = 0, assignshift = assign ? 1 : 0;
a0d0e21e
LW
1171 HV* stash;
1172 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
1173 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),'c'))
a6006777
PP
1174 && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1175 ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table
8ac85365 1176 : (CV **) NULL))
748a9306
LW
1177 && ((cv = cvp[off=method+assignshift])
1178 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
1179 * usual method */
1180 (fl = 1, cv = cvp[off=method])))) {
a0d0e21e
LW
1181 lr = -1; /* Call method for left argument */
1182 } else {
1183 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
1184 int logic;
1185
1186 /* look for substituted methods */
ee239bfe 1187 /* In all the covered cases we should be called with assign==0. */
a0d0e21e
LW
1188 switch (method) {
1189 case inc_amg:
ee239bfe
IZ
1190 force_cpy = 1;
1191 if ((cv = cvp[off=add_ass_amg])
1192 || ((cv = cvp[off = add_amg]) && (force_cpy = 0, postpr = 1))) {
3280af22 1193 right = &PL_sv_yes; lr = -1; assign = 1;
a0d0e21e
LW
1194 }
1195 break;
1196 case dec_amg:
ee239bfe
IZ
1197 force_cpy = 1;
1198 if ((cv = cvp[off = subtr_ass_amg])
1199 || ((cv = cvp[off = subtr_amg]) && (force_cpy = 0, postpr=1))) {
3280af22 1200 right = &PL_sv_yes; lr = -1; assign = 1;
a0d0e21e
LW
1201 }
1202 break;
1203 case bool__amg:
1204 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
1205 break;
1206 case numer_amg:
1207 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1208 break;
1209 case string_amg:
1210 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1211 break;
dc437b57
PP
1212 case not_amg:
1213 (void)((cv = cvp[off=bool__amg])
1214 || (cv = cvp[off=numer_amg])
1215 || (cv = cvp[off=string_amg]));
1216 postpr = 1;
1217 break;
748a9306
LW
1218 case copy_amg:
1219 {
76e3520e
GS
1220 /*
1221 * SV* ref causes confusion with the interpreter variable of
1222 * the same name
1223 */
1224 SV* tmpRef=SvRV(left);
1225 if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) {
fc36a67e
PP
1226 /*
1227 * Just to be extra cautious. Maybe in some
1228 * additional cases sv_setsv is safe, too.
1229 */
76e3520e 1230 SV* newref = newSVsv(tmpRef);
748a9306 1231 SvOBJECT_on(newref);
76e3520e 1232 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(tmpRef));
748a9306
LW
1233 return newref;
1234 }
1235 }
1236 break;
a0d0e21e 1237 case abs_amg:
748a9306 1238 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
a0d0e21e 1239 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
748a9306 1240 SV* nullsv=sv_2mortal(newSViv(0));
a0d0e21e 1241 if (off1==lt_amg) {
748a9306 1242 SV* lessp = amagic_call(left,nullsv,
a0d0e21e
LW
1243 lt_amg,AMGf_noright);
1244 logic = SvTRUE(lessp);
1245 } else {
748a9306 1246 SV* lessp = amagic_call(left,nullsv,
a0d0e21e
LW
1247 ncmp_amg,AMGf_noright);
1248 logic = (SvNV(lessp) < 0);
1249 }
1250 if (logic) {
1251 if (off==subtr_amg) {
1252 right = left;
748a9306 1253 left = nullsv;
a0d0e21e
LW
1254 lr = 1;
1255 }
1256 } else {
1257 return left;
1258 }
1259 }
1260 break;
1261 case neg_amg:
1262 if (cv = cvp[off=subtr_amg]) {
1263 right = left;
1264 left = sv_2mortal(newSViv(0));
1265 lr = 1;
1266 }
1267 break;
f5284f61
IZ
1268 case iter_amg: /* XXXX Eventually should do to_gv. */
1269 case to_sv_amg:
1270 case to_av_amg:
1271 case to_hv_amg:
1272 case to_gv_amg:
1273 case to_cv_amg:
1274 /* FAIL safe */
1275 return NULL; /* Delegate operation to standard mechanisms. */
1276 break;
a0d0e21e
LW
1277 default:
1278 goto not_found;
1279 }
1280 if (!cv) goto not_found;
1281 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1282 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c'))
a6006777
PP
1283 && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1284 ? (amtp = (AMT*)mg->mg_ptr)->table
8ac85365 1285 : (CV **) NULL))
a0d0e21e
LW
1286 && (cv = cvp[off=method])) { /* Method for right
1287 * argument found */
1288 lr=1;
748a9306 1289 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
4633a7c4 1290 && (cvp=ocvp) && (lr = -1))
a0d0e21e
LW
1291 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1292 && !(flags & AMGf_unary)) {
1293 /* We look for substitution for
1294 * comparison operations and
fc36a67e 1295 * concatenation */
a0d0e21e
LW
1296 if (method==concat_amg || method==concat_ass_amg
1297 || method==repeat_amg || method==repeat_ass_amg) {
1298 return NULL; /* Delegate operation to string conversion */
1299 }
1300 off = -1;
1301 switch (method) {
1302 case lt_amg:
1303 case le_amg:
1304 case gt_amg:
1305 case ge_amg:
1306 case eq_amg:
1307 case ne_amg:
1308 postpr = 1; off=ncmp_amg; break;
1309 case slt_amg:
1310 case sle_amg:
1311 case sgt_amg:
1312 case sge_amg:
1313 case seq_amg:
1314 case sne_amg:
1315 postpr = 1; off=scmp_amg; break;
1316 }
1317 if (off != -1) cv = cvp[off];
1318 if (!cv) {
1319 goto not_found;
1320 }
1321 } else {
a6006777 1322 not_found: /* No method found, either report or croak */
a0d0e21e
LW
1323 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1324 notfound = 1; lr = -1;
1325 } else if (cvp && (cv=cvp[nomethod_amg])) {
1326 notfound = 1; lr = 1;
1327 } else {
46fc3d4c 1328 SV *msg;
774d564b 1329 if (off==-1) off=method;
46fc3d4c
PP
1330 msg = sv_2mortal(newSVpvf(
1331 "Operation `%s': no method found,%sargument %s%s%s%s",
22c35a8c 1332 PL_AMG_names[method + assignshift],
e7ea3e70 1333 (flags & AMGf_unary ? " " : "\n\tleft "),
a0d0e21e
LW
1334 SvAMAGIC(left)?
1335 "in overloaded package ":
1336 "has no overloaded magic",
1337 SvAMAGIC(left)?
1338 HvNAME(SvSTASH(SvRV(left))):
1339 "",
1340 SvAMAGIC(right)?
e7ea3e70
IZ
1341 ",\n\tright argument in overloaded package ":
1342 (flags & AMGf_unary
1343 ? ""
1344 : ",\n\tright argument has no overloaded magic"),
a0d0e21e
LW
1345 SvAMAGIC(right)?
1346 HvNAME(SvSTASH(SvRV(right))):
46fc3d4c 1347 ""));
a0d0e21e 1348 if (amtp && amtp->fallback >= AMGfallYES) {
46fc3d4c 1349 DEBUG_o( deb("%s", SvPVX(msg)) );
a0d0e21e 1350 } else {
fc36a67e 1351 croak("%_", msg);
a0d0e21e
LW
1352 }
1353 return NULL;
1354 }
ee239bfe 1355 force_cpy = force_cpy || assign;
a0d0e21e
LW
1356 }
1357 }
1358 if (!notfound) {
774d564b 1359 DEBUG_o( deb(
46fc3d4c 1360 "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n",
22c35a8c 1361 PL_AMG_names[off],
748a9306
LW
1362 method+assignshift==off? "" :
1363 " (initially `",
1364 method+assignshift==off? "" :
22c35a8c 1365 PL_AMG_names[method+assignshift],
748a9306
LW
1366 method+assignshift==off? "" : "')",
1367 flags & AMGf_unary? "" :
1368 lr==1 ? " for right argument": " for left argument",
1369 flags & AMGf_unary? " for argument" : "",
a0d0e21e
LW
1370 HvNAME(stash),
1371 fl? ",\n\tassignment variant used": "") );
ee239bfe 1372 }
748a9306
LW
1373 /* Since we use shallow copy during assignment, we need
1374 * to dublicate the contents, probably calling user-supplied
1375 * version of copy operator
1376 */
ee239bfe
IZ
1377 /* We need to copy in following cases:
1378 * a) Assignment form was called.
1379 * assignshift==1, assign==T, method + 1 == off
1380 * b) Increment or decrement, called directly.
1381 * assignshift==0, assign==0, method + 0 == off
1382 * c) Increment or decrement, translated to assignment add/subtr.
1383 * assignshift==0, assign==T,
1384 * force_cpy == T
1385 * d) Increment or decrement, translated to nomethod.
1386 * assignshift==0, assign==0,
1387 * force_cpy == T
1388 * e) Assignment form translated to nomethod.
1389 * assignshift==1, assign==T, method + 1 != off
1390 * force_cpy == T
1391 */
1392 /* off is method, method+assignshift, or a result of opcode substitution.
1393 * In the latter case assignshift==0, so only notfound case is important.
1394 */
1395 if (( (method + assignshift == off)
1396 && (assign || (method == inc_amg) || (method == dec_amg)))
1397 || force_cpy)
1398 RvDEEPCP(left);
a0d0e21e
LW
1399 {
1400 dSP;
1401 BINOP myop;
1402 SV* res;
54310121 1403 bool oldcatch = CATCH_GET;
a0d0e21e 1404
54310121 1405 CATCH_SET(TRUE);
a0d0e21e
LW
1406 Zero(&myop, 1, BINOP);
1407 myop.op_last = (OP *) &myop;
1408 myop.op_next = Nullop;
54310121 1409 myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED;
a0d0e21e 1410
e788e7d3 1411 PUSHSTACKi(PERLSI_OVERLOAD);
a0d0e21e 1412 ENTER;
462e5cf6 1413 SAVEOP();
533c011a 1414 PL_op = (OP *) &myop;
3280af22 1415 if (PERLDB_SUB && PL_curstash != PL_debstash)
533c011a 1416 PL_op->op_private |= OPpENTERSUB_DB;
a0d0e21e 1417 PUTBACK;
11343788 1418 pp_pushmark(ARGS);
a0d0e21e 1419
924508f0 1420 EXTEND(SP, notfound + 5);
a0d0e21e
LW
1421 PUSHs(lr>0? right: left);
1422 PUSHs(lr>0? left: right);
3280af22 1423 PUSHs( lr > 0 ? &PL_sv_yes : ( assign ? &PL_sv_undef : &PL_sv_no ));
a0d0e21e 1424 if (notfound) {
22c35a8c 1425 PUSHs( sv_2mortal(newSVpv((char *)PL_AMG_names[method + assignshift],0)));
a0d0e21e
LW
1426 }
1427 PUSHs((SV*)cv);
1428 PUTBACK;
1429
533c011a 1430 if (PL_op = pp_entersub(ARGS))
76e3520e 1431 CALLRUNOPS();
a0d0e21e
LW
1432 LEAVE;
1433 SPAGAIN;
1434
1435 res=POPs;
ebafeae7 1436 PUTBACK;
d3acc0f7 1437 POPSTACK;
54310121 1438 CATCH_SET(oldcatch);
a0d0e21e 1439
a0d0e21e
LW
1440 if (postpr) {
1441 int ans;
1442 switch (method) {
1443 case le_amg:
1444 case sle_amg:
1445 ans=SvIV(res)<=0; break;
1446 case lt_amg:
1447 case slt_amg:
1448 ans=SvIV(res)<0; break;
1449 case ge_amg:
1450 case sge_amg:
1451 ans=SvIV(res)>=0; break;
1452 case gt_amg:
1453 case sgt_amg:
1454 ans=SvIV(res)>0; break;
1455 case eq_amg:
1456 case seq_amg:
1457 ans=SvIV(res)==0; break;
1458 case ne_amg:
1459 case sne_amg:
1460 ans=SvIV(res)!=0; break;
1461 case inc_amg:
1462 case dec_amg:
bbce6d69 1463 SvSetSV(left,res); return left;
dc437b57 1464 case not_amg:
44a8e56a 1465 ans=!SvOK(res); break;
a0d0e21e 1466 }
54310121 1467 return boolSV(ans);
748a9306
LW
1468 } else if (method==copy_amg) {
1469 if (!SvROK(res)) {
a6006777 1470 croak("Copy method did not return a reference");
748a9306
LW
1471 }
1472 return SvREFCNT_inc(SvRV(res));
a0d0e21e
LW
1473 } else {
1474 return res;
1475 }
1476 }
1477}