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