3 * Copyright (c) 1991-2000, Larry Wall
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.
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,'
24 Perl_gv_AVadd(pTHX_ register GV *gv)
26 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
27 Perl_croak(aTHX_ "Bad symbol for array");
34 Perl_gv_HVadd(pTHX_ register GV *gv)
36 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
37 Perl_croak(aTHX_ "Bad symbol for hash");
44 Perl_gv_IOadd(pTHX_ register GV *gv)
46 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
47 Perl_croak(aTHX_ "Bad symbol for filehandle");
54 Perl_gv_fetchfile(pTHX_ const char *name)
65 tmplen = strlen(name) + 2;
66 if (tmplen < sizeof smallbuf)
69 New(603, tmpbuf, tmplen + 1, char);
72 strcpy(tmpbuf + 2, name);
73 gv = *(GV**)hv_fetch(PL_defstash, tmpbuf, tmplen, TRUE);
75 gv_init(gv, PL_defstash, tmpbuf, tmplen, FALSE);
76 sv_setpv(GvSV(gv), name);
78 hv_magic(GvHVn(gv_AVadd(gv)), gv, 'L');
80 if (tmpbuf != smallbuf)
86 Perl_gv_init(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len, int multi)
90 bool doproto = SvTYPE(gv) > SVt_NULL;
91 char *proto = (doproto && SvPOK(gv)) ? SvPVX(gv) : NULL;
93 sv_upgrade((SV*)gv, SVt_PVGV);
102 Newz(602, gp, 1, GP);
103 GvGP(gv) = gp_ref(gp);
104 GvSV(gv) = NEWSV(72,0);
105 GvLINE(gv) = CopLINE(PL_curcop);
106 GvFILE(gv) = CopFILE(PL_curcop) ? CopFILE(PL_curcop) : "";
109 sv_magic((SV*)gv, (SV*)gv, '*', name, len);
110 GvSTASH(gv) = (HV*)SvREFCNT_inc(stash);
111 GvNAME(gv) = savepvn(name, len);
113 if (multi || doproto) /* doproto means it _was_ mentioned */
115 if (doproto) { /* Replicate part of newSUB here. */
118 /* XXX unsafe for threads if eval_owner isn't held */
119 start_subparse(0,0); /* Create CV in compcv. */
120 GvCV(gv) = PL_compcv;
124 CvGV(GvCV(gv)) = (GV*)SvREFCNT_inc(gv);
125 CvFILE(GvCV(gv)) = CopFILE(PL_curcop);
126 CvSTASH(GvCV(gv)) = PL_curstash;
128 CvOWNER(GvCV(gv)) = 0;
129 if (!CvMUTEXP(GvCV(gv))) {
130 New(666, CvMUTEXP(GvCV(gv)), 1, perl_mutex);
131 MUTEX_INIT(CvMUTEXP(GvCV(gv)));
133 #endif /* USE_THREADS */
135 sv_setpv((SV*)GvCV(gv), proto);
142 S_gv_init_sv(pTHX_ GV *gv, I32 sv_type)
158 =for apidoc gv_fetchmeth
160 Returns the glob with the given C<name> and a defined subroutine or
161 C<NULL>. The glob lives in the given C<stash>, or in the stashes
162 accessible via @ISA and @UNIVERSAL.
164 The argument C<level> should be either 0 or -1. If C<level==0>, as a
165 side-effect creates a glob with the given C<name> in the given C<stash>
166 which in the case of success contains an alias for the subroutine, and sets
167 up caching info for this glob. Similarly for all the searched stashes.
169 This function grants C<"SUPER"> token as a postfix of the stash name. The
170 GV returned from C<gv_fetchmeth> may be a method cache entry, which is not
171 visible to Perl code. So when calling C<call_sv>, you should not use
172 the GV directly; instead, you should use the method's CV, which can be
173 obtained from the GV with the C<GvCV> macro.
179 Perl_gv_fetchmeth(pTHX_ HV *stash, const char *name, STRLEN len, I32 level)
189 if ((level > 100) || (level < -100))
190 Perl_croak(aTHX_ "Recursive inheritance detected while looking for method '%s' in package '%s'",
191 name, HvNAME(stash));
193 DEBUG_o( Perl_deb(aTHX_ "Looking for method %s in package %s\n",name,HvNAME(stash)) );
195 gvp = (GV**)hv_fetch(stash, name, len, (level >= 0));
200 if (SvTYPE(topgv) != SVt_PVGV)
201 gv_init(topgv, stash, name, len, TRUE);
202 if ((cv = GvCV(topgv))) {
203 /* If genuine method or valid cache entry, use it */
204 if (!GvCVGEN(topgv) || GvCVGEN(topgv) == PL_sub_generation)
206 /* Stale cached entry: junk it */
208 GvCV(topgv) = cv = Nullcv;
211 else if (GvCVGEN(topgv) == PL_sub_generation)
212 return 0; /* cache indicates sub doesn't exist */
215 gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE);
216 av = (gvp && (gv = *gvp) && gv != (GV*)&PL_sv_undef) ? GvAV(gv) : Nullav;
218 /* create and re-create @.*::SUPER::ISA on demand */
219 if (!av || !SvMAGIC(av)) {
220 char* packname = HvNAME(stash);
221 STRLEN packlen = strlen(packname);
223 if (packlen >= 7 && strEQ(packname + packlen - 7, "::SUPER")) {
227 basestash = gv_stashpvn(packname, packlen, TRUE);
228 gvp = (GV**)hv_fetch(basestash, "ISA", 3, FALSE);
229 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
230 dTHR; /* just for SvREFCNT_dec */
231 gvp = (GV**)hv_fetch(stash, "ISA", 3, TRUE);
232 if (!gvp || !(gv = *gvp))
233 Perl_croak(aTHX_ "Cannot create %s::ISA", HvNAME(stash));
234 if (SvTYPE(gv) != SVt_PVGV)
235 gv_init(gv, stash, "ISA", 3, TRUE);
236 SvREFCNT_dec(GvAV(gv));
237 GvAV(gv) = (AV*)SvREFCNT_inc(av);
243 SV** svp = AvARRAY(av);
244 /* NOTE: No support for tied ISA */
245 I32 items = AvFILLp(av) + 1;
248 HV* basestash = gv_stashsv(sv, FALSE);
250 dTHR; /* just for ckWARN */
251 if (ckWARN(WARN_MISC))
252 Perl_warner(aTHX_ WARN_MISC, "Can't locate package %s for @%s::ISA",
253 SvPVX(sv), HvNAME(stash));
256 gv = gv_fetchmeth(basestash, name, len,
257 (level >= 0) ? level + 1 : level - 1);
263 /* if at top level, try UNIVERSAL */
265 if (level == 0 || level == -1) {
268 if ((lastchance = gv_stashpvn("UNIVERSAL", 9, FALSE))) {
269 if ((gv = gv_fetchmeth(lastchance, name, len,
270 (level >= 0) ? level + 1 : level - 1)))
274 * Cache method in topgv if:
275 * 1. topgv has no synonyms (else inheritance crosses wires)
276 * 2. method isn't a stub (else AUTOLOAD fails spectacularly)
279 GvREFCNT(topgv) == 1 &&
281 (CvROOT(cv) || CvXSUB(cv)))
283 if ((cv = GvCV(topgv)))
285 GvCV(topgv) = (CV*)SvREFCNT_inc(GvCV(gv));
286 GvCVGEN(topgv) = PL_sub_generation;
290 else if (topgv && GvREFCNT(topgv) == 1) {
291 /* cache the fact that the method is not defined */
292 GvCVGEN(topgv) = PL_sub_generation;
301 =for apidoc gv_fetchmethod
303 See L<gv_fetchmethod_autoload>.
309 Perl_gv_fetchmethod(pTHX_ HV *stash, const char *name)
311 return gv_fetchmethod_autoload(stash, name, TRUE);
315 =for apidoc gv_fetchmethod_autoload
317 Returns the glob which contains the subroutine to call to invoke the method
318 on the C<stash>. In fact in the presence of autoloading this may be the
319 glob for "AUTOLOAD". In this case the corresponding variable $AUTOLOAD is
322 The third parameter of C<gv_fetchmethod_autoload> determines whether
323 AUTOLOAD lookup is performed if the given method is not present: non-zero
324 means yes, look for AUTOLOAD; zero means no, don't look for AUTOLOAD.
325 Calling C<gv_fetchmethod> is equivalent to calling C<gv_fetchmethod_autoload>
326 with a non-zero C<autoload> parameter.
328 These functions grant C<"SUPER"> token as a prefix of the method name. Note
329 that if you want to keep the returned glob for a long time, you need to
330 check for it being "AUTOLOAD", since at the later time the call may load a
331 different subroutine due to $AUTOLOAD changing its value. Use the glob
332 created via a side effect to do this.
334 These functions have the same side-effects and as C<gv_fetchmeth> with
335 C<level==0>. C<name> should be writable if contains C<':'> or C<'
336 ''>. The warning against passing the GV returned by C<gv_fetchmeth> to
337 C<call_sv> apply equally to these functions.
343 Perl_gv_fetchmethod_autoload(pTHX_ HV *stash, const char *name, I32 autoload)
346 register const char *nend;
347 const char *nsplit = 0;
350 for (nend = name; *nend; nend++) {
353 else if (*nend == ':' && *(nend + 1) == ':')
357 const char *origname = name;
361 if ((nsplit - origname) == 5 && strnEQ(origname, "SUPER", 5)) {
362 /* ->SUPER::method should really be looked up in original stash */
363 SV *tmpstr = sv_2mortal(Perl_newSVpvf(aTHX_ "%s::SUPER",
364 CopSTASHPV(PL_curcop)));
365 stash = gv_stashpvn(SvPVX(tmpstr), SvCUR(tmpstr), TRUE);
366 DEBUG_o( Perl_deb(aTHX_ "Treating %s as %s::%s\n",
367 origname, HvNAME(stash), name) );
370 stash = gv_stashpvn(origname, nsplit - origname, TRUE);
373 gv = gv_fetchmeth(stash, name, nend - name, 0);
375 if (strEQ(name,"import"))
376 gv = (GV*)&PL_sv_yes;
378 gv = gv_autoload4(stash, name, nend - name, TRUE);
382 if (!CvROOT(cv) && !CvXSUB(cv)) {
390 if (GvCV(stubgv) != cv) /* orphaned import */
393 autogv = gv_autoload4(GvSTASH(stubgv),
394 GvNAME(stubgv), GvNAMELEN(stubgv), TRUE);
404 Perl_gv_autoload4(pTHX_ HV *stash, const char *name, STRLEN len, I32 method)
407 static char autoload[] = "AUTOLOAD";
408 static STRLEN autolen = 8;
415 if (len == autolen && strnEQ(name, autoload, autolen))
417 if (!(gv = gv_fetchmeth(stash, autoload, autolen, FALSE)))
422 * Inheriting AUTOLOAD for non-methods works ... for now.
424 if (ckWARN(WARN_DEPRECATED) && !method &&
425 (GvCVGEN(gv) || GvSTASH(gv) != stash))
426 Perl_warner(aTHX_ WARN_DEPRECATED,
427 "Use of inherited AUTOLOAD for non-method %s::%.*s() is deprecated",
428 HvNAME(stash), (int)len, name);
431 * Given &FOO::AUTOLOAD, set $FOO::AUTOLOAD to desired function name.
432 * The subroutine's original name may not be "AUTOLOAD", so we don't
433 * use that, but for lack of anything better we will use the sub's
434 * original package to look up $AUTOLOAD.
436 varstash = GvSTASH(CvGV(cv));
437 vargv = *(GV**)hv_fetch(varstash, autoload, autolen, TRUE);
439 gv_init(vargv, varstash, autoload, autolen, FALSE);
441 sv_setpv(varsv, HvNAME(stash));
442 sv_catpvn(varsv, "::", 2);
443 sv_catpvn(varsv, name, len);
444 SvTAINTED_off(varsv);
449 =for apidoc gv_stashpv
451 Returns a pointer to the stash for a specified package. C<name> should
452 be a valid UTF-8 string. If C<create> is set then the package will be
453 created if it does not already exist. If C<create> is not set and the
454 package does not exist then NULL is returned.
460 Perl_gv_stashpv(pTHX_ const char *name, I32 create)
462 return gv_stashpvn(name, strlen(name), create);
466 Perl_gv_stashpvn(pTHX_ const char *name, U32 namelen, I32 create)
473 if (namelen + 3 < sizeof smallbuf)
476 New(606, tmpbuf, namelen + 3, char);
477 Copy(name,tmpbuf,namelen,char);
478 tmpbuf[namelen++] = ':';
479 tmpbuf[namelen++] = ':';
480 tmpbuf[namelen] = '\0';
481 tmpgv = gv_fetchpv(tmpbuf, create, SVt_PVHV);
482 if (tmpbuf != smallbuf)
487 GvHV(tmpgv) = newHV();
490 HvNAME(stash) = savepv(name);
495 =for apidoc gv_stashsv
497 Returns a pointer to the stash for a specified package, which must be a
498 valid UTF-8 string. See C<gv_stashpv>.
504 Perl_gv_stashsv(pTHX_ SV *sv, I32 create)
509 return gv_stashpvn(ptr, len, create);
514 Perl_gv_fetchpv(pTHX_ const char *nambeg, I32 add, I32 sv_type)
517 register const char *name = nambeg;
521 register const char *namend;
525 if (*name == '*' && isALPHA(name[1])) /* accidental stringify on a GV? */
528 for (namend = name; *namend; namend++) {
529 if ((*namend == ':' && namend[1] == ':')
530 || (*namend == '\'' && namend[1]))
534 if (!stash || !SvREFCNT(stash)) /* symbol table under destruction */
542 if (len + 3 < sizeof smallbuf)
545 New(601, tmpbuf, len+3, char);
546 Copy(name, tmpbuf, len, char);
550 gvp = (GV**)hv_fetch(stash,tmpbuf,len,add);
551 gv = gvp ? *gvp : Nullgv;
552 if (gv && gv != (GV*)&PL_sv_undef) {
553 if (SvTYPE(gv) != SVt_PVGV)
554 gv_init(gv, stash, tmpbuf, len, (add & GV_ADDMULTI));
558 if (tmpbuf != smallbuf)
560 if (!gv || gv == (GV*)&PL_sv_undef)
563 if (!(stash = GvHV(gv)))
564 stash = GvHV(gv) = newHV();
567 HvNAME(stash) = savepvn(nambeg, namend - nambeg);
575 return gv ? gv : (GV*)*hv_fetch(PL_defstash, "main::", 6, TRUE);
582 /* No stash in name, so see how we can default */
585 if (isIDFIRST_lazy(name)) {
588 if (isUPPER(*name)) {
589 if (*name == 'S' && (
590 strEQ(name, "SIG") ||
591 strEQ(name, "STDIN") ||
592 strEQ(name, "STDOUT") ||
593 strEQ(name, "STDERR")))
595 else if (*name == 'I' && strEQ(name, "INC"))
597 else if (*name == 'E' && strEQ(name, "ENV"))
599 else if (*name == 'A' && (
600 strEQ(name, "ARGV") ||
601 strEQ(name, "ARGVOUT")))
604 else if (*name == '_' && !name[1])
609 else if ((COP*)PL_curcop == &PL_compiling) {
611 if (add && (PL_hints & HINT_STRICT_VARS) &&
612 sv_type != SVt_PVCV &&
613 sv_type != SVt_PVGV &&
614 sv_type != SVt_PVFM &&
615 sv_type != SVt_PVIO &&
616 !(len == 1 && sv_type == SVt_PV && strchr("ab",*name)) )
618 gvp = (GV**)hv_fetch(stash,name,len,0);
620 *gvp == (GV*)&PL_sv_undef ||
621 SvTYPE(*gvp) != SVt_PVGV)
625 else if ((sv_type == SVt_PV && !GvIMPORTED_SV(*gvp)) ||
626 (sv_type == SVt_PVAV && !GvIMPORTED_AV(*gvp)) ||
627 (sv_type == SVt_PVHV && !GvIMPORTED_HV(*gvp)) )
629 Perl_warn(aTHX_ "Variable \"%c%s\" is not imported",
630 sv_type == SVt_PVAV ? '@' :
631 sv_type == SVt_PVHV ? '%' : '$',
634 Perl_warn(aTHX_ "(Did you mean &%s instead?)\n", name);
640 stash = CopSTASH(PL_curcop);
646 /* By this point we should have a stash and a name */
650 qerror(Perl_mess(aTHX_
651 "Global symbol \"%s%s\" requires explicit package name",
652 (sv_type == SVt_PV ? "$"
653 : sv_type == SVt_PVAV ? "@"
654 : sv_type == SVt_PVHV ? "%"
660 if (!SvREFCNT(stash)) /* symbol table under destruction */
663 gvp = (GV**)hv_fetch(stash,name,len,add);
664 if (!gvp || *gvp == (GV*)&PL_sv_undef)
667 if (SvTYPE(gv) == SVt_PVGV) {
670 gv_init_sv(gv, sv_type);
673 } else if (add & GV_NOINIT) {
677 /* Adding a new symbol */
679 if (add & GV_ADDWARN && ckWARN_d(WARN_INTERNAL))
680 Perl_warner(aTHX_ WARN_INTERNAL, "Had to create %s unexpectedly", nambeg);
681 gv_init(gv, stash, name, len, add & GV_ADDMULTI);
682 gv_init_sv(gv, sv_type);
683 GvFLAGS(gv) |= add_gvflags;
685 if (isLEXWARN_on && isALPHA(name[0]) && ! ckWARN(WARN_ONCE))
688 /* set up magic where warranted */
691 if (strEQ(name, "ARGV")) {
692 IoFLAGS(GvIOn(gv)) |= IOf_ARGV|IOf_START;
696 if (strnEQ(name, "EXPORT", 6))
700 if (strEQ(name, "ISA")) {
703 sv_magic((SV*)av, (SV*)gv, 'I', Nullch, 0);
704 /* NOTE: No support for tied ISA */
705 if ((add & GV_ADDMULTI) && strEQ(nambeg,"AnyDBM_File::ISA")
706 && AvFILLp(av) == -1)
709 av_push(av, newSVpvn(pname = "NDBM_File",9));
710 gv_stashpvn(pname, 9, TRUE);
711 av_push(av, newSVpvn(pname = "DB_File",7));
712 gv_stashpvn(pname, 7, TRUE);
713 av_push(av, newSVpvn(pname = "GDBM_File",9));
714 gv_stashpvn(pname, 9, TRUE);
715 av_push(av, newSVpvn(pname = "SDBM_File",9));
716 gv_stashpvn(pname, 9, TRUE);
717 av_push(av, newSVpvn(pname = "ODBM_File",9));
718 gv_stashpvn(pname, 9, TRUE);
723 if (strEQ(name, "OVERLOAD")) {
726 hv_magic(hv, gv, 'A');
730 if (strEQ(name, "SIG")) {
734 int sig_num[] = { SIG_NUM };
735 New(73, PL_psig_ptr, sizeof(sig_num)/sizeof(*sig_num), SV*);
736 New(73, PL_psig_name, sizeof(sig_num)/sizeof(*sig_num), SV*);
740 hv_magic(hv, gv, 'S');
741 for (i = 1; PL_sig_name[i]; i++) {
743 init = hv_fetch(hv, PL_sig_name[i], strlen(PL_sig_name[i]), 1);
745 sv_setsv(*init, &PL_sv_undef);
752 if (strEQ(name, "VERSION"))
759 PL_sawampersand = TRUE;
765 PL_sawampersand = TRUE;
771 PL_sawampersand = TRUE;
777 sv_setpv(GvSV(gv),PL_chopset);
783 #ifdef COMPLEX_STATUS
784 (void)SvUPGRADE(GvSV(gv), SVt_PVLV);
791 if (sv_type > SVt_PV && PL_curcop != &PL_compiling) {
792 HV* stash = gv_stashpvn("Errno",5,FALSE);
793 if (!stash || !(gv_fetchmethod(stash, "TIEHASH"))) {
796 require_pv("Errno.pm");
798 stash = gv_stashpvn("Errno",5,FALSE);
799 if (!stash || !(gv_fetchmethod(stash, "TIEHASH")))
800 Perl_croak(aTHX_ "Can't use %%! because Errno.pm is not available");
809 sv_magic((SV*)av, Nullsv, 'D', Nullch, 0);
814 if (ckWARN(WARN_DEPRECATED) && len == 1 && sv_type == SVt_PV)
815 Perl_warner(aTHX_ WARN_DEPRECATED, "Use of $%s is deprecated", name);
831 case '\001': /* $^A */
832 case '\003': /* $^C */
833 case '\004': /* $^D */
834 case '\005': /* $^E */
835 case '\006': /* $^F */
836 case '\010': /* $^H */
837 case '\011': /* $^I, NOT \t in EBCDIC */
838 case '\017': /* $^O */
839 case '\020': /* $^P */
840 case '\024': /* $^T */
844 case '\023': /* $^S */
848 case '\027': /* $^W & $^WARNING_BITS */
849 if (len > 1 && strNE(name, "\027ARNING_BITS")
850 && strNE(name, "\027IDE_SYSTEM_CALLS"))
859 sv_magic((SV*)av, (SV*)av, 'D', Nullch, 0);
872 SvREADONLY_on(GvSV(gv));
874 sv_magic(GvSV(gv), (SV*)gv, 0, name, len);
877 case '\014': /* $^L */
880 sv_setpv(GvSV(gv),"\f");
881 PL_formfeed = GvSV(gv);
886 sv_setpv(GvSV(gv),"\034");
891 (void)SvUPGRADE(sv, SVt_PVNV);
892 SvNVX(sv) = SvNVX(PL_patchlevel);
894 (void)SvPV_nolen(sv);
898 case '\026': /* $^V */
901 GvSV(gv) = SvREFCNT_inc(PL_patchlevel);
910 Perl_gv_fullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
912 HV *hv = GvSTASH(gv);
917 sv_setpv(sv, prefix ? prefix : "");
918 sv_catpv(sv,HvNAME(hv));
919 sv_catpvn(sv,"::", 2);
920 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
924 Perl_gv_efullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
929 gv_fullname3(sv, egv, prefix);
932 /* XXX compatibility with versions <= 5.003. */
934 Perl_gv_fullname(pTHX_ SV *sv, GV *gv)
936 gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : "");
939 /* XXX compatibility with versions <= 5.003. */
941 Perl_gv_efullname(pTHX_ SV *sv, GV *gv)
943 gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : "");
953 io = (IO*)NEWSV(0,0);
954 sv_upgrade((SV *)io,SVt_PVIO);
957 iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV);
958 /* unless exists($main::{FileHandle}) and defined(%main::FileHandle::) */
959 if (!(iogv && GvHV(iogv) && HvARRAY(GvHV(iogv))))
960 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
961 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
966 Perl_gv_check(pTHX_ HV *stash)
976 for (i = 0; i <= (I32) HvMAX(stash); i++) {
977 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
978 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
979 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)) && HvNAME(hv))
981 if (hv != PL_defstash && hv != stash)
982 gv_check(hv); /* nested package */
984 else if (isALPHA(*HeKEY(entry))) {
986 gv = (GV*)HeVAL(entry);
987 if (SvTYPE(gv) != SVt_PVGV || GvMULTI(gv))
990 /* performance hack: if filename is absolute and it's a standard
991 * module, don't bother warning */
993 && PERL_FILE_IS_ABSOLUTE(file)
994 && (instr(file, "/lib/") || instr(file, ".pm")))
998 CopLINE_set(PL_curcop, GvLINE(gv));
1000 CopFILE(PL_curcop) = file; /* set for warning */
1002 CopFILEGV(PL_curcop) = gv_fetchfile(file);
1004 Perl_warner(aTHX_ WARN_ONCE,
1005 "Name \"%s::%s\" used only once: possible typo",
1006 HvNAME(stash), GvNAME(gv));
1013 Perl_newGVgen(pTHX_ char *pack)
1015 return gv_fetchpv(Perl_form(aTHX_ "%s::_GEN_%ld", pack, (long)PL_gensym++),
1019 /* hopefully this is only called on local symbol table entries */
1022 Perl_gp_ref(pTHX_ GP *gp)
1029 /* multi-named GPs cannot be used for method cache */
1030 SvREFCNT_dec(gp->gp_cv);
1035 /* Adding a new name to a subroutine invalidates method cache */
1036 PL_sub_generation++;
1043 Perl_gp_free(pTHX_ GV *gv)
1048 if (!gv || !(gp = GvGP(gv)))
1050 if (gp->gp_refcnt == 0) {
1051 if (ckWARN_d(WARN_INTERNAL))
1052 Perl_warner(aTHX_ WARN_INTERNAL,
1053 "Attempt to free unreferenced glob pointers");
1057 /* Deleting the name of a subroutine invalidates method cache */
1058 PL_sub_generation++;
1060 if (--gp->gp_refcnt > 0) {
1061 if (gp->gp_egv == gv)
1066 SvREFCNT_dec(gp->gp_sv);
1067 SvREFCNT_dec(gp->gp_av);
1068 SvREFCNT_dec(gp->gp_hv);
1069 SvREFCNT_dec(gp->gp_io);
1070 SvREFCNT_dec(gp->gp_cv);
1071 SvREFCNT_dec(gp->gp_form);
1077 #if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
1081 #ifdef MICROPORT /* Microport 2.4 hack */
1085 if (GvGP(gv)->gp_av)
1086 return GvGP(gv)->gp_av;
1088 return GvGP(gv_AVadd(gv))->gp_av;
1094 if (GvGP(gv)->gp_hv)
1095 return GvGP(gv)->gp_hv;
1097 return GvGP(gv_HVadd(gv))->gp_hv;
1099 #endif /* Microport 2.4 hack */
1101 /* Updates and caches the CV's */
1104 Perl_Gv_AMupdate(pTHX_ HV *stash)
1109 MAGIC* mg=mg_find((SV*)stash,'c');
1110 AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL;
1113 #ifdef OVERLOAD_VIA_HASH
1118 if (mg && amtp->was_ok_am == PL_amagic_generation
1119 && amtp->was_ok_sub == PL_sub_generation)
1120 return AMT_AMAGIC(amtp);
1121 if (amtp && AMT_AMAGIC(amtp)) { /* Have table. */
1123 for (i=1; i<NofAMmeth; i++) {
1124 if (amtp->table[i]) {
1125 SvREFCNT_dec(amtp->table[i]);
1129 sv_unmagic((SV*)stash, 'c');
1131 DEBUG_o( Perl_deb(aTHX_ "Recalcing overload magic in package %s\n",HvNAME(stash)) );
1133 amt.was_ok_am = PL_amagic_generation;
1134 amt.was_ok_sub = PL_sub_generation;
1135 amt.fallback = AMGfallNO;
1138 #ifdef OVERLOAD_VIA_HASH
1139 gvp=(GV**)hv_fetch(stash,"OVERLOAD",8,FALSE); /* A shortcut */
1140 if (gvp && ((gv = *gvp) != (GV*)&PL_sv_undef && (hv = GvHV(gv)))) {
1147 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
1149 if (( cp = (char *)PL_AMG_names[0] ) &&
1150 (svp = (SV**)hv_fetch(hv,cp,strlen(cp),FALSE)) && (sv = *svp)) {
1151 if (SvTRUE(sv)) amt.fallback=AMGfallYES;
1152 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
1154 for (i = 1; i < NofAMmeth; i++) {
1156 cp = (char *)PL_AMG_names[i];
1158 svp = (SV**)hv_fetch(hv, cp, strlen(cp), FALSE);
1159 if (svp && ((sv = *svp) != &PL_sv_undef)) {
1160 switch (SvTYPE(sv)) {
1163 if (!SvOK(sv)) break;
1164 gv = gv_fetchmethod(stash, SvPV(sv, n_a));
1165 if (gv) cv = GvCV(gv);
1169 if (SvTYPE(cv) == SVt_PVCV)
1174 Perl_croak(aTHX_ "Not a subroutine reference in overload table");
1180 if (!(cv = GvCVu((GV*)sv)))
1181 cv = sv_2cv(sv, &stash, &gv, FALSE);
1186 Perl_croak(aTHX_ "Method for operation %s not found in package %.256s during blessing\n",
1198 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
1200 if ((cp = PL_AMG_names[0])) {
1201 /* Try to find via inheritance. */
1202 gv = gv_fetchmeth(stash, "()", 2, -1); /* A cookie: "()". */
1208 else if (SvTRUE(sv))
1209 amt.fallback=AMGfallYES;
1211 amt.fallback=AMGfallNEVER;
1214 for (i = 1; i < NofAMmeth; i++) {
1215 SV *cookie = sv_2mortal(Perl_newSVpvf(aTHX_ "(%s", cp = PL_AMG_names[i]));
1216 DEBUG_o( Perl_deb(aTHX_ "Checking overloading of `%s' in package `%.256s'\n",
1217 cp, HvNAME(stash)) );
1218 /* don't fill the cache while looking up! */
1219 gv = gv_fetchmeth(stash, SvPVX(cookie), SvCUR(cookie), -1);
1221 if(gv && (cv = GvCV(gv))) {
1222 if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil")
1223 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) {
1224 /* GvSV contains the name of the method. */
1227 DEBUG_o( Perl_deb(aTHX_ "Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n",
1228 SvPV(GvSV(gv), n_a), cp, HvNAME(stash)) );
1229 if (!SvPOK(GvSV(gv))
1230 || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)),
1233 /* Can be an import stub (created by `can'). */
1235 Perl_croak(aTHX_ "Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'",
1236 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1239 Perl_croak(aTHX_ "Can't resolve method `%.256s' overloading `%s' in package `%.256s'",
1240 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1243 cv = GvCV(gv = ngv);
1245 DEBUG_o( Perl_deb(aTHX_ "Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n",
1246 cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))),
1247 GvNAME(CvGV(cv))) );
1251 amt.table[i]=(CV*)SvREFCNT_inc(cv);
1254 AMT_AMAGIC_on(&amt);
1255 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMT));
1259 /* Here we have no table: */
1261 AMT_AMAGIC_off(&amt);
1262 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMTS));
1267 Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags)
1272 CV **cvp=NULL, **ocvp=NULL;
1274 int fl=0, off, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
1275 int postpr = 0, force_cpy = 0, assignshift = assign ? 1 : 0;
1277 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
1278 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),'c'))
1279 && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1280 ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table
1282 && ((cv = cvp[off=method+assignshift])
1283 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
1285 (fl = 1, cv = cvp[off=method])))) {
1286 lr = -1; /* Call method for left argument */
1288 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
1291 /* look for substituted methods */
1292 /* In all the covered cases we should be called with assign==0. */
1296 if ((cv = cvp[off=add_ass_amg])
1297 || ((cv = cvp[off = add_amg]) && (force_cpy = 0, postpr = 1))) {
1298 right = &PL_sv_yes; lr = -1; assign = 1;
1303 if ((cv = cvp[off = subtr_ass_amg])
1304 || ((cv = cvp[off = subtr_amg]) && (force_cpy = 0, postpr=1))) {
1305 right = &PL_sv_yes; lr = -1; assign = 1;
1309 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
1312 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1315 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1318 (void)((cv = cvp[off=bool__amg])
1319 || (cv = cvp[off=numer_amg])
1320 || (cv = cvp[off=string_amg]));
1326 * SV* ref causes confusion with the interpreter variable of
1329 SV* tmpRef=SvRV(left);
1330 if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) {
1332 * Just to be extra cautious. Maybe in some
1333 * additional cases sv_setsv is safe, too.
1335 SV* newref = newSVsv(tmpRef);
1336 SvOBJECT_on(newref);
1337 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(tmpRef));
1343 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
1344 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
1345 SV* nullsv=sv_2mortal(newSViv(0));
1347 SV* lessp = amagic_call(left,nullsv,
1348 lt_amg,AMGf_noright);
1349 logic = SvTRUE(lessp);
1351 SV* lessp = amagic_call(left,nullsv,
1352 ncmp_amg,AMGf_noright);
1353 logic = (SvNV(lessp) < 0);
1356 if (off==subtr_amg) {
1367 if ((cv = cvp[off=subtr_amg])) {
1369 left = sv_2mortal(newSViv(0));
1373 case iter_amg: /* XXXX Eventually should do to_gv. */
1380 return NULL; /* Delegate operation to standard mechanisms. */
1385 if (!cv) goto not_found;
1386 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1387 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c'))
1388 && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1389 ? (amtp = (AMT*)mg->mg_ptr)->table
1391 && (cv = cvp[off=method])) { /* Method for right
1394 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
1395 && (cvp=ocvp) && (lr = -1))
1396 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1397 && !(flags & AMGf_unary)) {
1398 /* We look for substitution for
1399 * comparison operations and
1401 if (method==concat_amg || method==concat_ass_amg
1402 || method==repeat_amg || method==repeat_ass_amg) {
1403 return NULL; /* Delegate operation to string conversion */
1413 postpr = 1; off=ncmp_amg; break;
1420 postpr = 1; off=scmp_amg; break;
1422 if (off != -1) cv = cvp[off];
1427 not_found: /* No method found, either report or croak */
1428 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1429 notfound = 1; lr = -1;
1430 } else if (cvp && (cv=cvp[nomethod_amg])) {
1431 notfound = 1; lr = 1;
1434 if (off==-1) off=method;
1435 msg = sv_2mortal(Perl_newSVpvf(aTHX_
1436 "Operation `%s': no method found,%sargument %s%s%s%s",
1437 PL_AMG_names[method + assignshift],
1438 (flags & AMGf_unary ? " " : "\n\tleft "),
1440 "in overloaded package ":
1441 "has no overloaded magic",
1443 HvNAME(SvSTASH(SvRV(left))):
1446 ",\n\tright argument in overloaded package ":
1449 : ",\n\tright argument has no overloaded magic"),
1451 HvNAME(SvSTASH(SvRV(right))):
1453 if (amtp && amtp->fallback >= AMGfallYES) {
1454 DEBUG_o( Perl_deb(aTHX_ "%s", SvPVX(msg)) );
1456 Perl_croak(aTHX_ "%"SVf, msg);
1460 force_cpy = force_cpy || assign;
1464 DEBUG_o( Perl_deb(aTHX_
1465 "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n",
1467 method+assignshift==off? "" :
1469 method+assignshift==off? "" :
1470 PL_AMG_names[method+assignshift],
1471 method+assignshift==off? "" : "')",
1472 flags & AMGf_unary? "" :
1473 lr==1 ? " for right argument": " for left argument",
1474 flags & AMGf_unary? " for argument" : "",
1476 fl? ",\n\tassignment variant used": "") );
1478 /* Since we use shallow copy during assignment, we need
1479 * to dublicate the contents, probably calling user-supplied
1480 * version of copy operator
1482 /* We need to copy in following cases:
1483 * a) Assignment form was called.
1484 * assignshift==1, assign==T, method + 1 == off
1485 * b) Increment or decrement, called directly.
1486 * assignshift==0, assign==0, method + 0 == off
1487 * c) Increment or decrement, translated to assignment add/subtr.
1488 * assignshift==0, assign==T,
1490 * d) Increment or decrement, translated to nomethod.
1491 * assignshift==0, assign==0,
1493 * e) Assignment form translated to nomethod.
1494 * assignshift==1, assign==T, method + 1 != off
1497 /* off is method, method+assignshift, or a result of opcode substitution.
1498 * In the latter case assignshift==0, so only notfound case is important.
1500 if (( (method + assignshift == off)
1501 && (assign || (method == inc_amg) || (method == dec_amg)))
1508 bool oldcatch = CATCH_GET;
1511 Zero(&myop, 1, BINOP);
1512 myop.op_last = (OP *) &myop;
1513 myop.op_next = Nullop;
1514 myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED;
1516 PUSHSTACKi(PERLSI_OVERLOAD);
1519 PL_op = (OP *) &myop;
1520 if (PERLDB_SUB && PL_curstash != PL_debstash)
1521 PL_op->op_private |= OPpENTERSUB_DB;
1525 EXTEND(SP, notfound + 5);
1526 PUSHs(lr>0? right: left);
1527 PUSHs(lr>0? left: right);
1528 PUSHs( lr > 0 ? &PL_sv_yes : ( assign ? &PL_sv_undef : &PL_sv_no ));
1530 PUSHs( sv_2mortal(newSVpv((char *)PL_AMG_names[method + assignshift],0)));
1535 if ((PL_op = Perl_pp_entersub(aTHX)))
1543 CATCH_SET(oldcatch);
1550 ans=SvIV(res)<=0; break;
1553 ans=SvIV(res)<0; break;
1556 ans=SvIV(res)>=0; break;
1559 ans=SvIV(res)>0; break;
1562 ans=SvIV(res)==0; break;
1565 ans=SvIV(res)!=0; break;
1568 SvSetSV(left,res); return left;
1570 ans=!SvTRUE(res); break;
1573 } else if (method==copy_amg) {
1575 Perl_croak(aTHX_ "Copy method did not return a reference");
1577 return SvREFCNT_inc(SvRV(res));