This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
prefer IO::Handle for IO if FileHandle:: is empty (as suggested by
[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 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 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 193 /* create and re-create @.*::SUPER::ISA on demand */
194 if (!av || !SvMAGIC(av)) {
9607fc9c 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 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 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 238 /* if at top level, try UNIVERSAL */
239
44a8e56a 240 if (level == 0 || level == -1) {
9607fc9c 241 HV* lastchance;
242
dc437b57 243 if (lastchance = gv_stashpvn("UNIVERSAL", 9, FALSE)) {
44a8e56a 244 if (gv = gv_fetchmeth(lastchance, name, len,
245 (level >= 0) ? level + 1 : level - 1)) {
246 gotcha:
dc848c6f 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 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 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 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 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 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 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 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 333 if (autogv)
334 gv = autogv;
335 }
336 }
44a8e56a 337
338 return gv;
339}
340
341GV*
8ac85365 342gv_autoload4(HV *stash, char *name, STRLEN len, I32 method)
44a8e56a 343{
d008e5eb 344 dTHR;
44a8e56a 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 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 365 "Use of inherited AUTOLOAD for non-method %s::%.*s() is deprecated",
366 HvNAME(stash), (int)len, name);
44a8e56a 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 389 return gv_stashpvn(name, strlen(name), create);
390}
391
392HV*
8ac85365 393gv_stashpvn(char *name, U32 namelen, I32 create)
dc437b57 394{
46fc3d4c 395 char smallbuf[256];
396 char *tmpbuf;
a0d0e21e
LW
397 HV *stash;
398 GV *tmpgv;
dc437b57 399
46fc3d4c 400 if (namelen + 3 < sizeof smallbuf)
401 tmpbuf = smallbuf;
402 else
403 New(606, tmpbuf, namelen + 3, char);
dc437b57 404 Copy(name,tmpbuf,namelen,char);
405 tmpbuf[namelen++] = ':';
406 tmpbuf[namelen++] = ':';
407 tmpbuf[namelen] = '\0';
46fc3d4c 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 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 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 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 711 case '?':
712 if (len > 1)
713 break;
714#ifdef COMPLEX_STATUS
07f14f54 715 (void)SvUPGRADE(GvSV(gv), SVt_PVLV);
ff0cee69 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':
49460fe6 764 case '\003':
93a17b20 765 case '\004':
ad8898e0 766 case '\005':
748a9306 767 case '\006':
a0d0e21e 768 case '\010':
9d116dd7 769 case '\011': /* NOT \t in EBCDIC */
ad8898e0 770 case '\017':
93a17b20
LW
771 case '\020':
772 case '\024':
773 case '\027':
463ee0b2
LW
774 if (len > 1)
775 break;
776 goto magicalize;
6cef1e77
IZ
777 case '\023':
778 if (len > 1)
779 break;
780 goto ro_magicalize;
463ee0b2 781
a0d0e21e 782 case '+':
6cef1e77
IZ
783 if (len > 1)
784 break;
785 else {
786 AV* av = GvAVn(gv);
787 sv_magic((SV*)av, (SV*)av, 'D', Nullch, 0);
788 }
789 /* FALL THROUGH */
463ee0b2
LW
790 case '1':
791 case '2':
792 case '3':
793 case '4':
794 case '5':
795 case '6':
796 case '7':
797 case '8':
798 case '9':
a0d0e21e
LW
799 ro_magicalize:
800 SvREADONLY_on(GvSV(gv));
93a17b20 801 magicalize:
463ee0b2 802 sv_magic(GvSV(gv), (SV*)gv, 0, name, len);
93a17b20
LW
803 break;
804
805 case '\014':
463ee0b2
LW
806 if (len > 1)
807 break;
93a17b20 808 sv_setpv(GvSV(gv),"\f");
3280af22 809 PL_formfeed = GvSV(gv);
93a17b20
LW
810 break;
811 case ';':
463ee0b2
LW
812 if (len > 1)
813 break;
93a17b20
LW
814 sv_setpv(GvSV(gv),"\034");
815 break;
463ee0b2
LW
816 case ']':
817 if (len == 1) {
f86702cc 818 SV *sv = GvSV(gv);
07f14f54 819 (void)SvUPGRADE(sv, SVt_PVNV);
3280af22 820 sv_setpv(sv, PL_patchlevel);
f86702cc 821 (void)sv_2nv(sv);
822 SvREADONLY_on(sv);
93a17b20
LW
823 }
824 break;
79072805 825 }
93a17b20 826 return gv;
79072805
LW
827}
828
829void
8ac85365 830gv_fullname3(SV *sv, GV *gv, char *prefix)
79072805
LW
831{
832 HV *hv = GvSTASH(gv);
f967eb5f 833 if (!hv) {
834 SvOK_off(sv);
79072805 835 return;
f967eb5f 836 }
837 sv_setpv(sv, prefix ? prefix : "");
79072805 838 sv_catpv(sv,HvNAME(hv));
463ee0b2 839 sv_catpvn(sv,"::", 2);
79072805
LW
840 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
841}
842
843void
8ac85365 844gv_efullname3(SV *sv, GV *gv, char *prefix)
79072805 845{
f967eb5f 846 GV *egv = GvEGV(gv);
748a9306
LW
847 if (!egv)
848 egv = gv;
f6aff53a 849 gv_fullname3(sv, egv, prefix);
850}
851
852/* XXX compatibility with versions <= 5.003. */
853void
8ac85365 854gv_fullname(SV *sv, GV *gv)
f6aff53a 855{
856 gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : "");
857}
858
859/* XXX compatibility with versions <= 5.003. */
860void
8ac85365 861gv_efullname(SV *sv, GV *gv)
f6aff53a 862{
863 gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : "");
79072805
LW
864}
865
866IO *
8ac85365 867newIO(void)
79072805 868{
11343788 869 dTHR;
79072805 870 IO *io;
8990e307
LW
871 GV *iogv;
872
873 io = (IO*)NEWSV(0,0);
a0d0e21e 874 sv_upgrade((SV *)io,SVt_PVIO);
8990e307
LW
875 SvREFCNT(io) = 1;
876 SvOBJECT_on(io);
c9de509e 877 iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV);
5f2d631d
GS
878 /* unless exists($main::{FileHandle}) and defined(%main::FileHandle::) */
879 if (!(iogv && GvHV(iogv) && HvARRAY(GvHV(iogv))))
c9de509e 880 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
8990e307 881 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
79072805
LW
882 return io;
883}
884
885void
8ac85365 886gv_check(HV *stash)
79072805 887{
11343788 888 dTHR;
79072805
LW
889 register HE *entry;
890 register I32 i;
891 register GV *gv;
463ee0b2 892 HV *hv;
a0d0e21e 893 GV *filegv;
463ee0b2 894
8990e307
LW
895 if (!HvARRAY(stash))
896 return;
a0d0e21e 897 for (i = 0; i <= (I32) HvMAX(stash); i++) {
dc437b57 898 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
899 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
900 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)) && HvNAME(hv))
a0d0e21e 901 {
3280af22 902 if (hv != PL_defstash)
a0d0e21e
LW
903 gv_check(hv); /* nested package */
904 }
dc437b57 905 else if (isALPHA(*HeKEY(entry))) {
906 gv = (GV*)HeVAL(entry);
55d729e4 907 if (SvTYPE(gv) != SVt_PVGV || GvMULTI(gv))
463ee0b2 908 continue;
3280af22 909 PL_curcop->cop_line = GvLINE(gv);
a0d0e21e 910 filegv = GvFILEGV(gv);
3280af22 911 PL_curcop->cop_filegv = filegv;
a5f75d66 912 if (filegv && GvMULTI(filegv)) /* Filename began with slash */
8990e307 913 continue;
599cee73
PM
914 warner(WARN_ONCE,
915 "Name \"%s::%s\" used only once: possible typo",
a0d0e21e 916 HvNAME(stash), GvNAME(gv));
463ee0b2 917 }
79072805
LW
918 }
919 }
920}
921
922GV *
8ac85365 923newGVgen(char *pack)
79072805 924{
3280af22 925 return gv_fetchpv(form("%s::_GEN_%ld", pack, (long)PL_gensym++),
46fc3d4c 926 TRUE, SVt_PVGV);
79072805
LW
927}
928
929/* hopefully this is only called on local symbol table entries */
930
931GP*
8ac85365 932gp_ref(GP *gp)
79072805
LW
933{
934 gp->gp_refcnt++;
44a8e56a 935 if (gp->gp_cv) {
936 if (gp->gp_cvgen) {
937 /* multi-named GPs cannot be used for method cache */
938 SvREFCNT_dec(gp->gp_cv);
939 gp->gp_cv = Nullcv;
940 gp->gp_cvgen = 0;
941 }
942 else {
943 /* Adding a new name to a subroutine invalidates method cache */
3280af22 944 PL_sub_generation++;
44a8e56a 945 }
946 }
79072805 947 return gp;
79072805
LW
948}
949
950void
8ac85365 951gp_free(GV *gv)
79072805 952{
79072805 953 GP* gp;
377b8fbc 954 CV* cv;
79072805
LW
955
956 if (!gv || !(gp = GvGP(gv)))
957 return;
958 if (gp->gp_refcnt == 0) {
959 warn("Attempt to free unreferenced glob pointers");
960 return;
961 }
44a8e56a 962 if (gp->gp_cv) {
963 /* Deleting the name of a subroutine invalidates method cache */
3280af22 964 PL_sub_generation++;
44a8e56a 965 }
748a9306
LW
966 if (--gp->gp_refcnt > 0) {
967 if (gp->gp_egv == gv)
968 gp->gp_egv = 0;
79072805 969 return;
748a9306 970 }
79072805 971
8990e307
LW
972 SvREFCNT_dec(gp->gp_sv);
973 SvREFCNT_dec(gp->gp_av);
974 SvREFCNT_dec(gp->gp_hv);
377b8fbc 975 SvREFCNT_dec(gp->gp_io);
a6006777 976 SvREFCNT_dec(gp->gp_cv);
748a9306
LW
977 SvREFCNT_dec(gp->gp_form);
978
79072805
LW
979 Safefree(gp);
980 GvGP(gv) = 0;
981}
982
983#if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
984#define MICROPORT
985#endif
986
987#ifdef MICROPORT /* Microport 2.4 hack */
988AV *GvAVn(gv)
989register GV *gv;
990{
991 if (GvGP(gv)->gp_av)
992 return GvGP(gv)->gp_av;
993 else
994 return GvGP(gv_AVadd(gv))->gp_av;
995}
996
997HV *GvHVn(gv)
998register GV *gv;
999{
1000 if (GvGP(gv)->gp_hv)
1001 return GvGP(gv)->gp_hv;
1002 else
1003 return GvGP(gv_HVadd(gv))->gp_hv;
1004}
1005#endif /* Microport 2.4 hack */
a0d0e21e
LW
1006
1007#ifdef OVERLOAD
1008/* Updates and caches the CV's */
1009
1010bool
8ac85365 1011Gv_AMupdate(HV *stash)
a0d0e21e 1012{
11343788 1013 dTHR;
a0d0e21e
LW
1014 GV** gvp;
1015 HV* hv;
1016 GV* gv;
1017 CV* cv;
1018 MAGIC* mg=mg_find((SV*)stash,'c');
8ac85365 1019 AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL;
a6006777 1020 AMT amt;
a0d0e21e 1021
3280af22
NIS
1022 if (mg && amtp->was_ok_am == PL_amagic_generation
1023 && amtp->was_ok_sub == PL_sub_generation)
a6006777 1024 return AMT_AMAGIC(amtp);
1025 if (amtp && AMT_AMAGIC(amtp)) { /* Have table. */
748a9306 1026 int i;
a6006777 1027 for (i=1; i<NofAMmeth; i++) {
748a9306
LW
1028 if (amtp->table[i]) {
1029 SvREFCNT_dec(amtp->table[i]);
1030 }
1031 }
1032 }
a0d0e21e
LW
1033 sv_unmagic((SV*)stash, 'c');
1034
1035 DEBUG_o( deb("Recalcing overload magic in package %s\n",HvNAME(stash)) );
1036
3280af22
NIS
1037 amt.was_ok_am = PL_amagic_generation;
1038 amt.was_ok_sub = PL_sub_generation;
a6006777 1039 amt.fallback = AMGfallNO;
1040 amt.flags = 0;
1041
1042#ifdef OVERLOAD_VIA_HASH
1043 gvp=(GV**)hv_fetch(stash,"OVERLOAD",8,FALSE); /* A shortcut */
3280af22 1044 if (gvp && ((gv = *gvp) != (GV*)&PL_sv_undef && (hv = GvHV(gv)))) {
a0d0e21e
LW
1045 int filled=0;
1046 int i;
1047 char *cp;
a0d0e21e
LW
1048 SV* sv;
1049 SV** svp;
a0d0e21e 1050
22c35a8c 1051 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
a0d0e21e 1052
22c35a8c 1053 if (( cp = (char *)PL_AMG_names[0] ) &&
a6006777 1054 (svp = (SV**)hv_fetch(hv,cp,strlen(cp),FALSE)) && (sv = *svp)) {
a0d0e21e
LW
1055 if (SvTRUE(sv)) amt.fallback=AMGfallYES;
1056 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
1057 }
a6006777 1058 for (i = 1; i < NofAMmeth; i++) {
1059 cv = 0;
22c35a8c 1060 cp = (char *)PL_AMG_names[i];
a6006777 1061
1062 svp = (SV**)hv_fetch(hv, cp, strlen(cp), FALSE);
3280af22 1063 if (svp && ((sv = *svp) != &PL_sv_undef)) {
a0d0e21e
LW
1064 switch (SvTYPE(sv)) {
1065 default:
1066 if (!SvROK(sv)) {
1067 if (!SvOK(sv)) break;
6b88bc9c 1068 gv = gv_fetchmethod(stash, SvPV(sv, PL_na));
a0d0e21e
LW
1069 if (gv) cv = GvCV(gv);
1070 break;
1071 }
1072 cv = (CV*)SvRV(sv);
1073 if (SvTYPE(cv) == SVt_PVCV)
1074 break;
1075 /* FALL THROUGH */
1076 case SVt_PVHV:
1077 case SVt_PVAV:
a6006777 1078 croak("Not a subroutine reference in overload table");
a0d0e21e
LW
1079 return FALSE;
1080 case SVt_PVCV:
8ebc5c01 1081 cv = (CV*)sv;
1082 break;
a0d0e21e 1083 case SVt_PVGV:
8ebc5c01 1084 if (!(cv = GvCVu((GV*)sv)))
1085 cv = sv_2cv(sv, &stash, &gv, TRUE);
1086 break;
a0d0e21e
LW
1087 }
1088 if (cv) filled=1;
1089 else {
a6006777 1090 croak("Method for operation %s not found in package %.256s during blessing\n",
a0d0e21e
LW
1091 cp,HvNAME(stash));
1092 return FALSE;
1093 }
1094 }
a6006777 1095#else
1096 {
1097 int filled = 0;
1098 int i;
9607fc9c 1099 const char *cp;
a6006777 1100 SV* sv = NULL;
1101 SV** svp;
1102
22c35a8c 1103 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
a6006777 1104
22c35a8c 1105 if ( cp = PL_AMG_names[0] ) {
a6006777 1106 /* Try to find via inheritance. */
774d564b 1107 gv = gv_fetchmeth(stash, "()", 2, -1); /* A cookie: "()". */
a6006777 1108 if (gv) sv = GvSV(gv);
1109
774d564b 1110 if (!gv) goto no_table;
a6006777 1111 else if (SvTRUE(sv)) amt.fallback=AMGfallYES;
1112 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
1113 }
1114
1115 for (i = 1; i < NofAMmeth; i++) {
22c35a8c 1116 SV *cookie = sv_2mortal(newSVpvf("(%s", cp = PL_AMG_names[i]));
44a8e56a 1117 DEBUG_o( deb("Checking overloading of `%s' in package `%.256s'\n",
1118 cp, HvNAME(stash)) );
46fc3d4c 1119 /* don't fill the cache while looking up! */
1120 gv = gv_fetchmeth(stash, SvPVX(cookie), SvCUR(cookie), -1);
1121 cv = 0;
44a8e56a 1122 if(gv && (cv = GvCV(gv))) {
44a8e56a 1123 if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil")
1124 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) {
1125 /* GvSV contains the name of the method. */
1126 GV *ngv;
1127
1128 DEBUG_o( deb("Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n",
3280af22 1129 SvPV(GvSV(gv), PL_na), cp, HvNAME(stash)) );
dc848c6f 1130 if (!SvPOK(GvSV(gv))
1131 || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)),
1132 FALSE)))
1133 {
44a8e56a 1134 /* Can be an import stub (created by `can'). */
1135 if (GvCVGEN(gv)) {
1136 croak("Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'",
1137 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1138 cp, HvNAME(stash));
1139 } else
1140 croak("Cannot resolve method `%.256s' overloading `%s' in package `%.256s'",
1141 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1142 cp, HvNAME(stash));
1143 }
dc848c6f 1144 cv = GvCV(gv = ngv);
44a8e56a 1145 }
1146 DEBUG_o( deb("Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n",
1147 cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))),
1148 GvNAME(CvGV(cv))) );
1149 filled = 1;
1150 }
a6006777 1151#endif
1152 amt.table[i]=(CV*)SvREFCNT_inc(cv);
a0d0e21e 1153 }
a0d0e21e 1154 if (filled) {
a6006777 1155 AMT_AMAGIC_on(&amt);
1156 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMT));
a0d0e21e
LW
1157 return TRUE;
1158 }
1159 }
a6006777 1160 /* Here we have no table: */
774d564b 1161 no_table:
a6006777 1162 AMT_AMAGIC_off(&amt);
1163 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMTS));
a0d0e21e
LW
1164 return FALSE;
1165}
1166
a0d0e21e 1167SV*
8ac85365 1168amagic_call(SV *left, SV *right, int method, int flags)
a0d0e21e 1169{
11343788 1170 dTHR;
a0d0e21e
LW
1171 MAGIC *mg;
1172 CV *cv;
1173 CV **cvp=NULL, **ocvp=NULL;
1174 AMT *amtp, *oamtp;
1175 int fl=0, off, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
ee239bfe 1176 int postpr = 0, force_cpy = 0, assignshift = assign ? 1 : 0;
a0d0e21e
LW
1177 HV* stash;
1178 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
1179 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),'c'))
a6006777 1180 && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1181 ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table
8ac85365 1182 : (CV **) NULL))
748a9306
LW
1183 && ((cv = cvp[off=method+assignshift])
1184 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
1185 * usual method */
1186 (fl = 1, cv = cvp[off=method])))) {
a0d0e21e
LW
1187 lr = -1; /* Call method for left argument */
1188 } else {
1189 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
1190 int logic;
1191
1192 /* look for substituted methods */
ee239bfe 1193 /* In all the covered cases we should be called with assign==0. */
a0d0e21e
LW
1194 switch (method) {
1195 case inc_amg:
ee239bfe
IZ
1196 force_cpy = 1;
1197 if ((cv = cvp[off=add_ass_amg])
1198 || ((cv = cvp[off = add_amg]) && (force_cpy = 0, postpr = 1))) {
3280af22 1199 right = &PL_sv_yes; lr = -1; assign = 1;
a0d0e21e
LW
1200 }
1201 break;
1202 case dec_amg:
ee239bfe
IZ
1203 force_cpy = 1;
1204 if ((cv = cvp[off = subtr_ass_amg])
1205 || ((cv = cvp[off = subtr_amg]) && (force_cpy = 0, postpr=1))) {
3280af22 1206 right = &PL_sv_yes; lr = -1; assign = 1;
a0d0e21e
LW
1207 }
1208 break;
1209 case bool__amg:
1210 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
1211 break;
1212 case numer_amg:
1213 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1214 break;
1215 case string_amg:
1216 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1217 break;
dc437b57 1218 case not_amg:
1219 (void)((cv = cvp[off=bool__amg])
1220 || (cv = cvp[off=numer_amg])
1221 || (cv = cvp[off=string_amg]));
1222 postpr = 1;
1223 break;
748a9306
LW
1224 case copy_amg:
1225 {
76e3520e
GS
1226 /*
1227 * SV* ref causes confusion with the interpreter variable of
1228 * the same name
1229 */
1230 SV* tmpRef=SvRV(left);
1231 if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) {
fc36a67e 1232 /*
1233 * Just to be extra cautious. Maybe in some
1234 * additional cases sv_setsv is safe, too.
1235 */
76e3520e 1236 SV* newref = newSVsv(tmpRef);
748a9306 1237 SvOBJECT_on(newref);
76e3520e 1238 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(tmpRef));
748a9306
LW
1239 return newref;
1240 }
1241 }
1242 break;
a0d0e21e 1243 case abs_amg:
748a9306 1244 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
a0d0e21e 1245 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
748a9306 1246 SV* nullsv=sv_2mortal(newSViv(0));
a0d0e21e 1247 if (off1==lt_amg) {
748a9306 1248 SV* lessp = amagic_call(left,nullsv,
a0d0e21e
LW
1249 lt_amg,AMGf_noright);
1250 logic = SvTRUE(lessp);
1251 } else {
748a9306 1252 SV* lessp = amagic_call(left,nullsv,
a0d0e21e
LW
1253 ncmp_amg,AMGf_noright);
1254 logic = (SvNV(lessp) < 0);
1255 }
1256 if (logic) {
1257 if (off==subtr_amg) {
1258 right = left;
748a9306 1259 left = nullsv;
a0d0e21e
LW
1260 lr = 1;
1261 }
1262 } else {
1263 return left;
1264 }
1265 }
1266 break;
1267 case neg_amg:
1268 if (cv = cvp[off=subtr_amg]) {
1269 right = left;
1270 left = sv_2mortal(newSViv(0));
1271 lr = 1;
1272 }
1273 break;
f5284f61
IZ
1274 case iter_amg: /* XXXX Eventually should do to_gv. */
1275 case to_sv_amg:
1276 case to_av_amg:
1277 case to_hv_amg:
1278 case to_gv_amg:
1279 case to_cv_amg:
1280 /* FAIL safe */
1281 return NULL; /* Delegate operation to standard mechanisms. */
1282 break;
a0d0e21e
LW
1283 default:
1284 goto not_found;
1285 }
1286 if (!cv) goto not_found;
1287 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1288 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c'))
a6006777 1289 && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1290 ? (amtp = (AMT*)mg->mg_ptr)->table
8ac85365 1291 : (CV **) NULL))
a0d0e21e
LW
1292 && (cv = cvp[off=method])) { /* Method for right
1293 * argument found */
1294 lr=1;
748a9306 1295 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
4633a7c4 1296 && (cvp=ocvp) && (lr = -1))
a0d0e21e
LW
1297 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1298 && !(flags & AMGf_unary)) {
1299 /* We look for substitution for
1300 * comparison operations and
fc36a67e 1301 * concatenation */
a0d0e21e
LW
1302 if (method==concat_amg || method==concat_ass_amg
1303 || method==repeat_amg || method==repeat_ass_amg) {
1304 return NULL; /* Delegate operation to string conversion */
1305 }
1306 off = -1;
1307 switch (method) {
1308 case lt_amg:
1309 case le_amg:
1310 case gt_amg:
1311 case ge_amg:
1312 case eq_amg:
1313 case ne_amg:
1314 postpr = 1; off=ncmp_amg; break;
1315 case slt_amg:
1316 case sle_amg:
1317 case sgt_amg:
1318 case sge_amg:
1319 case seq_amg:
1320 case sne_amg:
1321 postpr = 1; off=scmp_amg; break;
1322 }
1323 if (off != -1) cv = cvp[off];
1324 if (!cv) {
1325 goto not_found;
1326 }
1327 } else {
a6006777 1328 not_found: /* No method found, either report or croak */
a0d0e21e
LW
1329 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1330 notfound = 1; lr = -1;
1331 } else if (cvp && (cv=cvp[nomethod_amg])) {
1332 notfound = 1; lr = 1;
1333 } else {
46fc3d4c 1334 SV *msg;
774d564b 1335 if (off==-1) off=method;
46fc3d4c 1336 msg = sv_2mortal(newSVpvf(
1337 "Operation `%s': no method found,%sargument %s%s%s%s",
22c35a8c 1338 PL_AMG_names[method + assignshift],
e7ea3e70 1339 (flags & AMGf_unary ? " " : "\n\tleft "),
a0d0e21e
LW
1340 SvAMAGIC(left)?
1341 "in overloaded package ":
1342 "has no overloaded magic",
1343 SvAMAGIC(left)?
1344 HvNAME(SvSTASH(SvRV(left))):
1345 "",
1346 SvAMAGIC(right)?
e7ea3e70
IZ
1347 ",\n\tright argument in overloaded package ":
1348 (flags & AMGf_unary
1349 ? ""
1350 : ",\n\tright argument has no overloaded magic"),
a0d0e21e
LW
1351 SvAMAGIC(right)?
1352 HvNAME(SvSTASH(SvRV(right))):
46fc3d4c 1353 ""));
a0d0e21e 1354 if (amtp && amtp->fallback >= AMGfallYES) {
46fc3d4c 1355 DEBUG_o( deb("%s", SvPVX(msg)) );
a0d0e21e 1356 } else {
fc36a67e 1357 croak("%_", msg);
a0d0e21e
LW
1358 }
1359 return NULL;
1360 }
ee239bfe 1361 force_cpy = force_cpy || assign;
a0d0e21e
LW
1362 }
1363 }
1364 if (!notfound) {
774d564b 1365 DEBUG_o( deb(
46fc3d4c 1366 "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n",
22c35a8c 1367 PL_AMG_names[off],
748a9306
LW
1368 method+assignshift==off? "" :
1369 " (initially `",
1370 method+assignshift==off? "" :
22c35a8c 1371 PL_AMG_names[method+assignshift],
748a9306
LW
1372 method+assignshift==off? "" : "')",
1373 flags & AMGf_unary? "" :
1374 lr==1 ? " for right argument": " for left argument",
1375 flags & AMGf_unary? " for argument" : "",
a0d0e21e
LW
1376 HvNAME(stash),
1377 fl? ",\n\tassignment variant used": "") );
ee239bfe 1378 }
748a9306
LW
1379 /* Since we use shallow copy during assignment, we need
1380 * to dublicate the contents, probably calling user-supplied
1381 * version of copy operator
1382 */
ee239bfe
IZ
1383 /* We need to copy in following cases:
1384 * a) Assignment form was called.
1385 * assignshift==1, assign==T, method + 1 == off
1386 * b) Increment or decrement, called directly.
1387 * assignshift==0, assign==0, method + 0 == off
1388 * c) Increment or decrement, translated to assignment add/subtr.
1389 * assignshift==0, assign==T,
1390 * force_cpy == T
1391 * d) Increment or decrement, translated to nomethod.
1392 * assignshift==0, assign==0,
1393 * force_cpy == T
1394 * e) Assignment form translated to nomethod.
1395 * assignshift==1, assign==T, method + 1 != off
1396 * force_cpy == T
1397 */
1398 /* off is method, method+assignshift, or a result of opcode substitution.
1399 * In the latter case assignshift==0, so only notfound case is important.
1400 */
1401 if (( (method + assignshift == off)
1402 && (assign || (method == inc_amg) || (method == dec_amg)))
1403 || force_cpy)
1404 RvDEEPCP(left);
a0d0e21e
LW
1405 {
1406 dSP;
1407 BINOP myop;
1408 SV* res;
54310121 1409 bool oldcatch = CATCH_GET;
a0d0e21e 1410
54310121 1411 CATCH_SET(TRUE);
a0d0e21e
LW
1412 Zero(&myop, 1, BINOP);
1413 myop.op_last = (OP *) &myop;
1414 myop.op_next = Nullop;
54310121 1415 myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED;
a0d0e21e 1416
e788e7d3 1417 PUSHSTACKi(PERLSI_OVERLOAD);
a0d0e21e 1418 ENTER;
462e5cf6 1419 SAVEOP();
533c011a 1420 PL_op = (OP *) &myop;
3280af22 1421 if (PERLDB_SUB && PL_curstash != PL_debstash)
533c011a 1422 PL_op->op_private |= OPpENTERSUB_DB;
a0d0e21e 1423 PUTBACK;
11343788 1424 pp_pushmark(ARGS);
a0d0e21e 1425
924508f0 1426 EXTEND(SP, notfound + 5);
a0d0e21e
LW
1427 PUSHs(lr>0? right: left);
1428 PUSHs(lr>0? left: right);
3280af22 1429 PUSHs( lr > 0 ? &PL_sv_yes : ( assign ? &PL_sv_undef : &PL_sv_no ));
a0d0e21e 1430 if (notfound) {
22c35a8c 1431 PUSHs( sv_2mortal(newSVpv((char *)PL_AMG_names[method + assignshift],0)));
a0d0e21e
LW
1432 }
1433 PUSHs((SV*)cv);
1434 PUTBACK;
1435
533c011a 1436 if (PL_op = pp_entersub(ARGS))
76e3520e 1437 CALLRUNOPS();
a0d0e21e
LW
1438 LEAVE;
1439 SPAGAIN;
1440
1441 res=POPs;
ebafeae7 1442 PUTBACK;
d3acc0f7 1443 POPSTACK;
54310121 1444 CATCH_SET(oldcatch);
a0d0e21e 1445
a0d0e21e
LW
1446 if (postpr) {
1447 int ans;
1448 switch (method) {
1449 case le_amg:
1450 case sle_amg:
1451 ans=SvIV(res)<=0; break;
1452 case lt_amg:
1453 case slt_amg:
1454 ans=SvIV(res)<0; break;
1455 case ge_amg:
1456 case sge_amg:
1457 ans=SvIV(res)>=0; break;
1458 case gt_amg:
1459 case sgt_amg:
1460 ans=SvIV(res)>0; break;
1461 case eq_amg:
1462 case seq_amg:
1463 ans=SvIV(res)==0; break;
1464 case ne_amg:
1465 case sne_amg:
1466 ans=SvIV(res)!=0; break;
1467 case inc_amg:
1468 case dec_amg:
bbce6d69 1469 SvSetSV(left,res); return left;
dc437b57 1470 case not_amg:
44a8e56a 1471 ans=!SvOK(res); break;
a0d0e21e 1472 }
54310121 1473 return boolSV(ans);
748a9306
LW
1474 } else if (method==copy_amg) {
1475 if (!SvROK(res)) {
a6006777 1476 croak("Copy method did not return a reference");
748a9306
LW
1477 }
1478 return SvREFCNT_inc(SvRV(res));
a0d0e21e
LW
1479 } else {
1480 return res;
1481 }
1482 }
1483}
1484#endif /* OVERLOAD */
4e35701f 1485