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