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