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