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