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