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