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