3 * Copyright (c) 1991-2001, 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)
64 tmplen = strlen(name) + 2;
65 if (tmplen < sizeof smallbuf)
68 New(603, tmpbuf, tmplen + 1, char);
71 strcpy(tmpbuf + 2, name);
72 gv = *(GV**)hv_fetch(PL_defstash, tmpbuf, tmplen, TRUE);
74 gv_init(gv, PL_defstash, tmpbuf, tmplen, FALSE);
75 sv_setpv(GvSV(gv), name);
77 hv_magic(GvHVn(gv_AVadd(gv)), Nullgv, 'L');
79 if (tmpbuf != smallbuf)
85 Perl_gv_init(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len, int multi)
88 bool doproto = SvTYPE(gv) > SVt_NULL;
89 char *proto = (doproto && SvPOK(gv)) ? SvPVX(gv) : NULL;
91 sv_upgrade((SV*)gv, SVt_PVGV);
100 Newz(602, gp, 1, GP);
101 GvGP(gv) = gp_ref(gp);
102 GvSV(gv) = NEWSV(72,0);
103 GvLINE(gv) = CopLINE(PL_curcop);
104 GvFILE(gv) = CopFILE(PL_curcop) ? CopFILE(PL_curcop) : "";
107 sv_magic((SV*)gv, (SV*)gv, '*', Nullch, 0);
108 GvSTASH(gv) = (HV*)SvREFCNT_inc(stash);
109 GvNAME(gv) = savepvn(name, len);
111 if (multi || doproto) /* doproto means it _was_ mentioned */
113 if (doproto) { /* Replicate part of newSUB here. */
116 /* XXX unsafe for threads if eval_owner isn't held */
117 start_subparse(0,0); /* Create CV in compcv. */
118 GvCV(gv) = PL_compcv;
122 CvGV(GvCV(gv)) = (GV*)SvREFCNT_inc(gv);
123 CvFILE(GvCV(gv)) = CopFILE(PL_curcop);
124 CvSTASH(GvCV(gv)) = PL_curstash;
126 CvOWNER(GvCV(gv)) = 0;
127 if (!CvMUTEXP(GvCV(gv))) {
128 New(666, CvMUTEXP(GvCV(gv)), 1, perl_mutex);
129 MUTEX_INIT(CvMUTEXP(GvCV(gv)));
131 #endif /* USE_THREADS */
133 sv_setpv((SV*)GvCV(gv), proto);
140 S_gv_init_sv(pTHX_ GV *gv, I32 sv_type)
156 =for apidoc gv_fetchmeth
158 Returns the glob with the given C<name> and a defined subroutine or
159 C<NULL>. The glob lives in the given C<stash>, or in the stashes
160 accessible via @ISA and @UNIVERSAL.
162 The argument C<level> should be either 0 or -1. If C<level==0>, as a
163 side-effect creates a glob with the given C<name> in the given C<stash>
164 which in the case of success contains an alias for the subroutine, and sets
165 up caching info for this glob. Similarly for all the searched stashes.
167 This function grants C<"SUPER"> token as a postfix of the stash name. The
168 GV returned from C<gv_fetchmeth> may be a method cache entry, which is not
169 visible to Perl code. So when calling C<call_sv>, you should not use
170 the GV directly; instead, you should use the method's CV, which can be
171 obtained from the GV with the C<GvCV> macro.
177 Perl_gv_fetchmeth(pTHX_ HV *stash, const char *name, STRLEN len, I32 level)
187 if ((level > 100) || (level < -100))
188 Perl_croak(aTHX_ "Recursive inheritance detected while looking for method '%s' in package '%s'",
189 name, HvNAME(stash));
191 DEBUG_o( Perl_deb(aTHX_ "Looking for method %s in package %s\n",name,HvNAME(stash)) );
193 gvp = (GV**)hv_fetch(stash, name, len, (level >= 0));
198 if (SvTYPE(topgv) != SVt_PVGV)
199 gv_init(topgv, stash, name, len, TRUE);
200 if ((cv = GvCV(topgv))) {
201 /* If genuine method or valid cache entry, use it */
202 if (!GvCVGEN(topgv) || GvCVGEN(topgv) == PL_sub_generation)
204 /* Stale cached entry: junk it */
206 GvCV(topgv) = cv = Nullcv;
209 else if (GvCVGEN(topgv) == PL_sub_generation)
210 return 0; /* cache indicates sub doesn't exist */
213 gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE);
214 av = (gvp && (gv = *gvp) && gv != (GV*)&PL_sv_undef) ? GvAV(gv) : Nullav;
216 /* create and re-create @.*::SUPER::ISA on demand */
217 if (!av || !SvMAGIC(av)) {
218 char* packname = HvNAME(stash);
219 STRLEN packlen = strlen(packname);
221 if (packlen >= 7 && strEQ(packname + packlen - 7, "::SUPER")) {
225 basestash = gv_stashpvn(packname, packlen, TRUE);
226 gvp = (GV**)hv_fetch(basestash, "ISA", 3, FALSE);
227 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
228 gvp = (GV**)hv_fetch(stash, "ISA", 3, TRUE);
229 if (!gvp || !(gv = *gvp))
230 Perl_croak(aTHX_ "Cannot create %s::ISA", HvNAME(stash));
231 if (SvTYPE(gv) != SVt_PVGV)
232 gv_init(gv, stash, "ISA", 3, TRUE);
233 SvREFCNT_dec(GvAV(gv));
234 GvAV(gv) = (AV*)SvREFCNT_inc(av);
240 SV** svp = AvARRAY(av);
241 /* NOTE: No support for tied ISA */
242 I32 items = AvFILLp(av) + 1;
245 HV* basestash = gv_stashsv(sv, FALSE);
247 if (ckWARN(WARN_MISC))
248 Perl_warner(aTHX_ WARN_MISC, "Can't locate package %s for @%s::ISA",
249 SvPVX(sv), HvNAME(stash));
252 gv = gv_fetchmeth(basestash, name, len,
253 (level >= 0) ? level + 1 : level - 1);
259 /* if at top level, try UNIVERSAL */
261 if (level == 0 || level == -1) {
264 if ((lastchance = gv_stashpvn("UNIVERSAL", 9, FALSE))) {
265 if ((gv = gv_fetchmeth(lastchance, name, len,
266 (level >= 0) ? level + 1 : level - 1)))
270 * Cache method in topgv if:
271 * 1. topgv has no synonyms (else inheritance crosses wires)
272 * 2. method isn't a stub (else AUTOLOAD fails spectacularly)
275 GvREFCNT(topgv) == 1 &&
277 (CvROOT(cv) || CvXSUB(cv)))
279 if ((cv = GvCV(topgv)))
281 GvCV(topgv) = (CV*)SvREFCNT_inc(GvCV(gv));
282 GvCVGEN(topgv) = PL_sub_generation;
286 else if (topgv && GvREFCNT(topgv) == 1) {
287 /* cache the fact that the method is not defined */
288 GvCVGEN(topgv) = PL_sub_generation;
297 =for apidoc gv_fetchmethod
299 See L<gv_fetchmethod_autoload>.
305 Perl_gv_fetchmethod(pTHX_ HV *stash, const char *name)
307 return gv_fetchmethod_autoload(stash, name, TRUE);
311 =for apidoc gv_fetchmethod_autoload
313 Returns the glob which contains the subroutine to call to invoke the method
314 on the C<stash>. In fact in the presence of autoloading this may be the
315 glob for "AUTOLOAD". In this case the corresponding variable $AUTOLOAD is
318 The third parameter of C<gv_fetchmethod_autoload> determines whether
319 AUTOLOAD lookup is performed if the given method is not present: non-zero
320 means yes, look for AUTOLOAD; zero means no, don't look for AUTOLOAD.
321 Calling C<gv_fetchmethod> is equivalent to calling C<gv_fetchmethod_autoload>
322 with a non-zero C<autoload> parameter.
324 These functions grant C<"SUPER"> token as a prefix of the method name. Note
325 that if you want to keep the returned glob for a long time, you need to
326 check for it being "AUTOLOAD", since at the later time the call may load a
327 different subroutine due to $AUTOLOAD changing its value. Use the glob
328 created via a side effect to do this.
330 These functions have the same side-effects and as C<gv_fetchmeth> with
331 C<level==0>. C<name> should be writable if contains C<':'> or C<'
332 ''>. The warning against passing the GV returned by C<gv_fetchmeth> to
333 C<call_sv> apply equally to these functions.
339 Perl_gv_fetchmethod_autoload(pTHX_ HV *stash, const char *name, I32 autoload)
341 register const char *nend;
342 const char *nsplit = 0;
345 for (nend = name; *nend; nend++) {
348 else if (*nend == ':' && *(nend + 1) == ':')
352 const char *origname = name;
356 if ((nsplit - origname) == 5 && strnEQ(origname, "SUPER", 5)) {
357 /* ->SUPER::method should really be looked up in original stash */
358 SV *tmpstr = sv_2mortal(Perl_newSVpvf(aTHX_ "%s::SUPER",
359 CopSTASHPV(PL_curcop)));
360 stash = gv_stashpvn(SvPVX(tmpstr), SvCUR(tmpstr), TRUE);
361 DEBUG_o( Perl_deb(aTHX_ "Treating %s as %s::%s\n",
362 origname, HvNAME(stash), name) );
365 stash = gv_stashpvn(origname, nsplit - origname, TRUE);
368 gv = gv_fetchmeth(stash, name, nend - name, 0);
370 if (strEQ(name,"import") || strEQ(name,"unimport"))
371 gv = (GV*)&PL_sv_yes;
373 gv = gv_autoload4(stash, name, nend - name, TRUE);
377 if (!CvROOT(cv) && !CvXSUB(cv)) {
385 if (GvCV(stubgv) != cv) /* orphaned import */
388 autogv = gv_autoload4(GvSTASH(stubgv),
389 GvNAME(stubgv), GvNAMELEN(stubgv), TRUE);
399 Perl_gv_autoload4(pTHX_ HV *stash, const char *name, STRLEN len, I32 method)
401 static char autoload[] = "AUTOLOAD";
402 static STRLEN autolen = 8;
409 if (len == autolen && strnEQ(name, autoload, autolen))
411 if (!(gv = gv_fetchmeth(stash, autoload, autolen, FALSE)))
415 if (!(CvROOT(cv) || CvXSUB(cv)))
419 * Inheriting AUTOLOAD for non-methods works ... for now.
421 if (ckWARN(WARN_DEPRECATED) && !method &&
422 (GvCVGEN(gv) || GvSTASH(gv) != stash))
423 Perl_warner(aTHX_ WARN_DEPRECATED,
424 "Use of inherited AUTOLOAD for non-method %s::%.*s() is deprecated",
425 HvNAME(stash), (int)len, name);
429 /* rather than lookup/init $AUTOLOAD here
430 * only to have the XSUB do another lookup for $AUTOLOAD
431 * and split that value on the last '::',
432 * pass along the same data via some unused fields in the CV
435 SvPVX(cv) = (char *)name; /* cast to loose constness warning */
442 * Given &FOO::AUTOLOAD, set $FOO::AUTOLOAD to desired function name.
443 * The subroutine's original name may not be "AUTOLOAD", so we don't
444 * use that, but for lack of anything better we will use the sub's
445 * original package to look up $AUTOLOAD.
447 varstash = GvSTASH(CvGV(cv));
448 vargv = *(GV**)hv_fetch(varstash, autoload, autolen, TRUE);
452 sv_lock((SV *)varstash);
455 gv_init(vargv, varstash, autoload, autolen, FALSE);
461 sv_setpv(varsv, HvNAME(stash));
462 sv_catpvn(varsv, "::", 2);
463 sv_catpvn(varsv, name, len);
464 SvTAINTED_off(varsv);
469 =for apidoc gv_stashpv
471 Returns a pointer to the stash for a specified package. C<name> should
472 be a valid UTF-8 string. If C<create> is set then the package will be
473 created if it does not already exist. If C<create> is not set and the
474 package does not exist then NULL is returned.
480 Perl_gv_stashpv(pTHX_ const char *name, I32 create)
482 return gv_stashpvn(name, strlen(name), create);
486 Perl_gv_stashpvn(pTHX_ const char *name, U32 namelen, I32 create)
493 if (namelen + 3 < sizeof smallbuf)
496 New(606, tmpbuf, namelen + 3, char);
497 Copy(name,tmpbuf,namelen,char);
498 tmpbuf[namelen++] = ':';
499 tmpbuf[namelen++] = ':';
500 tmpbuf[namelen] = '\0';
501 tmpgv = gv_fetchpv(tmpbuf, create, SVt_PVHV);
502 if (tmpbuf != smallbuf)
507 GvHV(tmpgv) = newHV();
510 HvNAME(stash) = savepv(name);
515 =for apidoc gv_stashsv
517 Returns a pointer to the stash for a specified package, which must be a
518 valid UTF-8 string. See C<gv_stashpv>.
524 Perl_gv_stashsv(pTHX_ SV *sv, I32 create)
529 return gv_stashpvn(ptr, len, create);
534 Perl_gv_fetchpv(pTHX_ const char *nambeg, I32 add, I32 sv_type)
536 register const char *name = nambeg;
540 register const char *namend;
543 if (*name == '*' && isALPHA(name[1])) /* accidental stringify on a GV? */
546 for (namend = name; *namend; namend++) {
547 if ((*namend == ':' && namend[1] == ':')
548 || (*namend == '\'' && namend[1]))
552 if (!stash || !SvREFCNT(stash)) /* symbol table under destruction */
560 if (len + 3 < sizeof smallbuf)
563 New(601, tmpbuf, len+3, char);
564 Copy(name, tmpbuf, len, char);
568 gvp = (GV**)hv_fetch(stash,tmpbuf,len,add);
569 gv = gvp ? *gvp : Nullgv;
570 if (gv && gv != (GV*)&PL_sv_undef) {
571 if (SvTYPE(gv) != SVt_PVGV)
572 gv_init(gv, stash, tmpbuf, len, (add & GV_ADDMULTI));
576 if (tmpbuf != smallbuf)
578 if (!gv || gv == (GV*)&PL_sv_undef)
581 if (!(stash = GvHV(gv)))
582 stash = GvHV(gv) = newHV();
585 HvNAME(stash) = savepvn(nambeg, namend - nambeg);
593 return gv ? gv : (GV*)*hv_fetch(PL_defstash, "main::", 6, TRUE);
600 /* No stash in name, so see how we can default */
603 if (isIDFIRST_lazy(name)) {
606 if (isUPPER(*name)) {
607 if (*name == 'S' && (
608 strEQ(name, "SIG") ||
609 strEQ(name, "STDIN") ||
610 strEQ(name, "STDOUT") ||
611 strEQ(name, "STDERR")))
613 else if (*name == 'I' && strEQ(name, "INC"))
615 else if (*name == 'E' && strEQ(name, "ENV"))
617 else if (*name == 'A' && (
618 strEQ(name, "ARGV") ||
619 strEQ(name, "ARGVOUT")))
622 else if (*name == '_' && !name[1])
627 else if ((COP*)PL_curcop == &PL_compiling) {
629 if (add && (PL_hints & HINT_STRICT_VARS) &&
630 sv_type != SVt_PVCV &&
631 sv_type != SVt_PVGV &&
632 sv_type != SVt_PVFM &&
633 sv_type != SVt_PVIO &&
634 !(len == 1 && sv_type == SVt_PV && strchr("ab",*name)) )
636 gvp = (GV**)hv_fetch(stash,name,len,0);
638 *gvp == (GV*)&PL_sv_undef ||
639 SvTYPE(*gvp) != SVt_PVGV)
643 else if ((sv_type == SVt_PV && !GvIMPORTED_SV(*gvp)) ||
644 (sv_type == SVt_PVAV && !GvIMPORTED_AV(*gvp)) ||
645 (sv_type == SVt_PVHV && !GvIMPORTED_HV(*gvp)) )
647 Perl_warn(aTHX_ "Variable \"%c%s\" is not imported",
648 sv_type == SVt_PVAV ? '@' :
649 sv_type == SVt_PVHV ? '%' : '$',
652 Perl_warn(aTHX_ "\t(Did you mean &%s instead?)\n", name);
658 stash = CopSTASH(PL_curcop);
664 /* By this point we should have a stash and a name */
668 qerror(Perl_mess(aTHX_
669 "Global symbol \"%s%s\" requires explicit package name",
670 (sv_type == SVt_PV ? "$"
671 : sv_type == SVt_PVAV ? "@"
672 : sv_type == SVt_PVHV ? "%"
674 stash = PL_nullstash;
680 if (!SvREFCNT(stash)) /* symbol table under destruction */
683 gvp = (GV**)hv_fetch(stash,name,len,add);
684 if (!gvp || *gvp == (GV*)&PL_sv_undef)
687 if (SvTYPE(gv) == SVt_PVGV) {
690 gv_init_sv(gv, sv_type);
693 } else if (add & GV_NOINIT) {
697 /* Adding a new symbol */
699 if (add & GV_ADDWARN && ckWARN_d(WARN_INTERNAL))
700 Perl_warner(aTHX_ WARN_INTERNAL, "Had to create %s unexpectedly", nambeg);
701 gv_init(gv, stash, name, len, add & GV_ADDMULTI);
702 gv_init_sv(gv, sv_type);
704 if (isLEXWARN_on && isALPHA(name[0]) && ! ckWARN(WARN_ONCE))
707 /* set up magic where warranted */
710 if (strEQ(name, "ARGV")) {
711 IoFLAGS(GvIOn(gv)) |= IOf_ARGV|IOf_START;
715 if (strnEQ(name, "EXPORT", 6))
719 if (strEQ(name, "ISA")) {
722 sv_magic((SV*)av, (SV*)gv, 'I', Nullch, 0);
723 /* NOTE: No support for tied ISA */
724 if ((add & GV_ADDMULTI) && strEQ(nambeg,"AnyDBM_File::ISA")
725 && AvFILLp(av) == -1)
728 av_push(av, newSVpvn(pname = "NDBM_File",9));
729 gv_stashpvn(pname, 9, TRUE);
730 av_push(av, newSVpvn(pname = "DB_File",7));
731 gv_stashpvn(pname, 7, TRUE);
732 av_push(av, newSVpvn(pname = "GDBM_File",9));
733 gv_stashpvn(pname, 9, TRUE);
734 av_push(av, newSVpvn(pname = "SDBM_File",9));
735 gv_stashpvn(pname, 9, TRUE);
736 av_push(av, newSVpvn(pname = "ODBM_File",9));
737 gv_stashpvn(pname, 9, TRUE);
742 if (strEQ(name, "OVERLOAD")) {
745 hv_magic(hv, Nullgv, 'A');
749 if (strEQ(name, "SIG")) {
753 Newz(73, PL_psig_ptr, SIG_SIZE, SV*);
754 Newz(73, PL_psig_name, SIG_SIZE, SV*);
755 Newz(73, PL_psig_pend, SIG_SIZE, int);
759 hv_magic(hv, Nullgv, 'S');
760 for (i = 1; i < SIG_SIZE; i++) {
762 init = hv_fetch(hv, PL_sig_name[i], strlen(PL_sig_name[i]), 1);
764 sv_setsv(*init, &PL_sv_undef);
772 if (strEQ(name, "VERSION"))
779 PL_sawampersand = TRUE;
785 PL_sawampersand = TRUE;
791 PL_sawampersand = TRUE;
797 sv_setpv(GvSV(gv),PL_chopset);
803 #ifdef COMPLEX_STATUS
804 (void)SvUPGRADE(GvSV(gv), SVt_PVLV);
811 if (sv_type > SVt_PV && PL_curcop != &PL_compiling) {
812 HV* stash = gv_stashpvn("Errno",5,FALSE);
813 if (!stash || !(gv_fetchmethod(stash, "TIEHASH"))) {
816 require_pv("Errno.pm");
818 stash = gv_stashpvn("Errno",5,FALSE);
819 if (!stash || !(gv_fetchmethod(stash, "TIEHASH")))
820 Perl_croak(aTHX_ "Can't use %%! because Errno.pm is not available");
829 sv_magic((SV*)av, Nullsv, 'D', Nullch, 0);
835 if (ckWARN(WARN_DEPRECATED) && len == 1 && sv_type == SVt_PV)
836 Perl_warner(aTHX_ WARN_DEPRECATED, "Use of $%s is deprecated", name);
851 case '\001': /* $^A */
852 case '\003': /* $^C */
853 case '\004': /* $^D */
854 case '\005': /* $^E */
855 case '\006': /* $^F */
856 case '\010': /* $^H */
857 case '\011': /* $^I, NOT \t in EBCDIC */
858 case '\020': /* $^P */
859 case '\024': /* $^T */
866 sv_setiv(GvSV(gv), (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0);
868 case '\017': /* $^O & $^OPEN */
869 if (len > 1 && strNE(name, "\017PEN"))
872 case '\023': /* $^S */
876 case '\027': /* $^W & $^WARNING_BITS */
877 if (len > 1 && strNE(name, "\027ARNING_BITS")
878 && strNE(name, "\027IDE_SYSTEM_CALLS"))
887 sv_magic((SV*)av, (SV*)av, 'D', Nullch, 0);
901 SvREADONLY_on(GvSV(gv));
903 sv_magic(GvSV(gv), (SV*)gv, 0, name, len);
906 case '\014': /* $^L */
909 sv_setpv(GvSV(gv),"\f");
910 PL_formfeed = GvSV(gv);
915 sv_setpv(GvSV(gv),"\034");
920 (void)SvUPGRADE(sv, SVt_PVNV);
921 Perl_sv_setpvf(aTHX_ sv,
922 #if defined(PERL_SUBVERSION) && (PERL_SUBVERSION > 0)
928 SvNVX(PL_patchlevel));
929 SvNVX(sv) = SvNVX(PL_patchlevel);
934 case '\026': /* $^V */
937 GvSV(gv) = SvREFCNT_inc(PL_patchlevel);
946 Perl_gv_fullname4(pTHX_ SV *sv, GV *gv, const char *prefix, bool keepmain)
948 HV *hv = GvSTASH(gv);
953 sv_setpv(sv, prefix ? prefix : "");
954 if (keepmain || strNE(HvNAME(hv), "main")) {
955 sv_catpv(sv,HvNAME(hv));
956 sv_catpvn(sv,"::", 2);
958 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
962 Perl_gv_fullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
964 HV *hv = GvSTASH(gv);
969 sv_setpv(sv, prefix ? prefix : "");
970 sv_catpv(sv,HvNAME(hv));
971 sv_catpvn(sv,"::", 2);
972 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
976 Perl_gv_efullname4(pTHX_ SV *sv, GV *gv, const char *prefix, bool keepmain)
981 gv_fullname4(sv, egv, prefix, keepmain);
985 Perl_gv_efullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
990 gv_fullname3(sv, egv, prefix);
993 /* XXX compatibility with versions <= 5.003. */
995 Perl_gv_fullname(pTHX_ SV *sv, GV *gv)
997 gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : "");
1000 /* XXX compatibility with versions <= 5.003. */
1002 Perl_gv_efullname(pTHX_ SV *sv, GV *gv)
1004 gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : "");
1013 io = (IO*)NEWSV(0,0);
1014 sv_upgrade((SV *)io,SVt_PVIO);
1017 iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV);
1018 /* unless exists($main::{FileHandle}) and defined(%main::FileHandle::) */
1019 if (!(iogv && GvHV(iogv) && HvARRAY(GvHV(iogv))))
1020 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
1021 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
1026 Perl_gv_check(pTHX_ HV *stash)
1033 if (!HvARRAY(stash))
1035 for (i = 0; i <= (I32) HvMAX(stash); i++) {
1036 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
1037 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
1038 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)) && HvNAME(hv))
1040 if (hv != PL_defstash && hv != stash)
1041 gv_check(hv); /* nested package */
1043 else if (isALPHA(*HeKEY(entry))) {
1045 gv = (GV*)HeVAL(entry);
1046 if (SvTYPE(gv) != SVt_PVGV || GvMULTI(gv))
1049 /* performance hack: if filename is absolute and it's a standard
1050 * module, don't bother warning */
1052 && PERL_FILE_IS_ABSOLUTE(file)
1053 && (instr(file, "/lib/") || instr(file, ".pm")))
1057 CopLINE_set(PL_curcop, GvLINE(gv));
1059 CopFILE(PL_curcop) = file; /* set for warning */
1061 CopFILEGV(PL_curcop) = gv_fetchfile(file);
1063 Perl_warner(aTHX_ WARN_ONCE,
1064 "Name \"%s::%s\" used only once: possible typo",
1065 HvNAME(stash), GvNAME(gv));
1072 Perl_newGVgen(pTHX_ char *pack)
1074 return gv_fetchpv(Perl_form(aTHX_ "%s::_GEN_%ld", pack, (long)PL_gensym++),
1078 /* hopefully this is only called on local symbol table entries */
1081 Perl_gp_ref(pTHX_ GP *gp)
1088 /* multi-named GPs cannot be used for method cache */
1089 SvREFCNT_dec(gp->gp_cv);
1094 /* Adding a new name to a subroutine invalidates method cache */
1095 PL_sub_generation++;
1102 Perl_gp_free(pTHX_ GV *gv)
1106 if (!gv || !(gp = GvGP(gv)))
1108 if (gp->gp_refcnt == 0) {
1109 if (ckWARN_d(WARN_INTERNAL))
1110 Perl_warner(aTHX_ WARN_INTERNAL,
1111 "Attempt to free unreferenced glob pointers");
1115 /* Deleting the name of a subroutine invalidates method cache */
1116 PL_sub_generation++;
1118 if (--gp->gp_refcnt > 0) {
1119 if (gp->gp_egv == gv)
1124 SvREFCNT_dec(gp->gp_sv);
1125 SvREFCNT_dec(gp->gp_av);
1126 SvREFCNT_dec(gp->gp_hv);
1127 SvREFCNT_dec(gp->gp_io);
1128 SvREFCNT_dec(gp->gp_cv);
1129 SvREFCNT_dec(gp->gp_form);
1135 #if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
1139 #ifdef MICROPORT /* Microport 2.4 hack */
1143 if (GvGP(gv)->gp_av)
1144 return GvGP(gv)->gp_av;
1146 return GvGP(gv_AVadd(gv))->gp_av;
1152 if (GvGP(gv)->gp_hv)
1153 return GvGP(gv)->gp_hv;
1155 return GvGP(gv_HVadd(gv))->gp_hv;
1157 #endif /* Microport 2.4 hack */
1160 Perl_magic_freeovrld(pTHX_ SV *sv, MAGIC *mg)
1162 AMT *amtp = (AMT*)mg->mg_ptr;
1163 if (amtp && AMT_AMAGIC(amtp)) {
1165 for (i = 1; i < NofAMmeth; i++) {
1166 CV *cv = amtp->table[i];
1168 SvREFCNT_dec((SV *) cv);
1169 amtp->table[i] = Nullcv;
1176 /* Updates and caches the CV's */
1179 Perl_Gv_AMupdate(pTHX_ HV *stash)
1183 MAGIC* mg=mg_find((SV*)stash,'c');
1184 AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL;
1188 if (mg && amtp->was_ok_am == PL_amagic_generation
1189 && amtp->was_ok_sub == PL_sub_generation)
1190 return AMT_OVERLOADED(amtp);
1191 sv_unmagic((SV*)stash, 'c');
1193 DEBUG_o( Perl_deb(aTHX_ "Recalcing overload magic in package %s\n",HvNAME(stash)) );
1196 amt.was_ok_am = PL_amagic_generation;
1197 amt.was_ok_sub = PL_sub_generation;
1198 amt.fallback = AMGfallNO;
1202 int filled = 0, have_ovl = 0;
1207 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
1209 /* Try to find via inheritance. */
1210 gv = gv_fetchmeth(stash, PL_AMG_names[0], 2, -1);
1215 lim = DESTROY_amg; /* Skip overloading entries. */
1216 else if (SvTRUE(sv))
1217 amt.fallback=AMGfallYES;
1219 amt.fallback=AMGfallNEVER;
1221 for (i = 1; i < lim; i++)
1222 amt.table[i] = Nullcv;
1223 for (; i < NofAMmeth; i++) {
1224 char *cooky = (char*)PL_AMG_names[i];
1225 /* Human-readable form, for debugging: */
1226 char *cp = (i >= DESTROY_amg ? cooky : AMG_id2name(i));
1227 STRLEN l = strlen(cooky);
1229 DEBUG_o( Perl_deb(aTHX_ "Checking overloading of `%s' in package `%.256s'\n",
1230 cp, HvNAME(stash)) );
1231 /* don't fill the cache while looking up! */
1232 gv = gv_fetchmeth(stash, cooky, l, -1);
1234 if (gv && (cv = GvCV(gv))) {
1235 if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil")
1236 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) {
1237 /* GvSV contains the name of the method. */
1240 DEBUG_o( Perl_deb(aTHX_ "Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n",
1241 SvPV(GvSV(gv), n_a), cp, HvNAME(stash)) );
1242 if (!SvPOK(GvSV(gv))
1243 || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)),
1246 /* Can be an import stub (created by `can'). */
1248 Perl_croak(aTHX_ "Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'",
1249 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1252 Perl_croak(aTHX_ "Can't resolve method `%.256s' overloading `%s' in package `%.256s'",
1253 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1256 cv = GvCV(gv = ngv);
1258 DEBUG_o( Perl_deb(aTHX_ "Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n",
1259 cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))),
1260 GvNAME(CvGV(cv))) );
1262 if (i < DESTROY_amg)
1265 amt.table[i]=(CV*)SvREFCNT_inc(cv);
1268 AMT_AMAGIC_on(&amt);
1270 AMT_OVERLOADED_on(&amt);
1271 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMT));
1275 /* Here we have no table: */
1277 AMT_AMAGIC_off(&amt);
1278 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMTS));
1284 Perl_gv_handler(pTHX_ HV *stash, I32 id)
1292 mg = mg_find((SV*)stash,'c');
1296 mg = mg_find((SV*)stash,'c');
1298 amtp = (AMT*)mg->mg_ptr;
1299 if ( amtp->was_ok_am != PL_amagic_generation
1300 || amtp->was_ok_sub != PL_sub_generation )
1302 if (AMT_AMAGIC(amtp))
1303 return amtp->table[id];
1309 Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags)
1313 CV **cvp=NULL, **ocvp=NULL;
1315 int fl=0, off, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
1316 int postpr = 0, force_cpy = 0, assignshift = assign ? 1 : 0;
1318 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
1319 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),'c'))
1320 && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1321 ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table
1323 && ((cv = cvp[off=method+assignshift])
1324 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
1326 (fl = 1, cv = cvp[off=method])))) {
1327 lr = -1; /* Call method for left argument */
1329 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
1332 /* look for substituted methods */
1333 /* In all the covered cases we should be called with assign==0. */
1337 if ((cv = cvp[off=add_ass_amg])
1338 || ((cv = cvp[off = add_amg]) && (force_cpy = 0, postpr = 1))) {
1339 right = &PL_sv_yes; lr = -1; assign = 1;
1344 if ((cv = cvp[off = subtr_ass_amg])
1345 || ((cv = cvp[off = subtr_amg]) && (force_cpy = 0, postpr=1))) {
1346 right = &PL_sv_yes; lr = -1; assign = 1;
1350 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
1353 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1356 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1359 (void)((cv = cvp[off=bool__amg])
1360 || (cv = cvp[off=numer_amg])
1361 || (cv = cvp[off=string_amg]));
1367 * SV* ref causes confusion with the interpreter variable of
1370 SV* tmpRef=SvRV(left);
1371 if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) {
1373 * Just to be extra cautious. Maybe in some
1374 * additional cases sv_setsv is safe, too.
1376 SV* newref = newSVsv(tmpRef);
1377 SvOBJECT_on(newref);
1378 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(tmpRef));
1384 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
1385 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
1386 SV* nullsv=sv_2mortal(newSViv(0));
1388 SV* lessp = amagic_call(left,nullsv,
1389 lt_amg,AMGf_noright);
1390 logic = SvTRUE(lessp);
1392 SV* lessp = amagic_call(left,nullsv,
1393 ncmp_amg,AMGf_noright);
1394 logic = (SvNV(lessp) < 0);
1397 if (off==subtr_amg) {
1408 if ((cv = cvp[off=subtr_amg])) {
1410 left = sv_2mortal(newSViv(0));
1415 case iter_amg: /* XXXX Eventually should do to_gv. */
1417 return NULL; /* Delegate operation to standard mechanisms. */
1425 return left; /* Delegate operation to standard mechanisms. */
1430 if (!cv) goto not_found;
1431 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1432 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c'))
1433 && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1434 ? (amtp = (AMT*)mg->mg_ptr)->table
1436 && (cv = cvp[off=method])) { /* Method for right
1439 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
1440 && (cvp=ocvp) && (lr = -1))
1441 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1442 && !(flags & AMGf_unary)) {
1443 /* We look for substitution for
1444 * comparison operations and
1446 if (method==concat_amg || method==concat_ass_amg
1447 || method==repeat_amg || method==repeat_ass_amg) {
1448 return NULL; /* Delegate operation to string conversion */
1458 postpr = 1; off=ncmp_amg; break;
1465 postpr = 1; off=scmp_amg; break;
1467 if (off != -1) cv = cvp[off];
1472 not_found: /* No method found, either report or croak */
1480 return left; /* Delegate operation to standard mechanisms. */
1483 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1484 notfound = 1; lr = -1;
1485 } else if (cvp && (cv=cvp[nomethod_amg])) {
1486 notfound = 1; lr = 1;
1489 if (off==-1) off=method;
1490 msg = sv_2mortal(Perl_newSVpvf(aTHX_
1491 "Operation `%s': no method found,%sargument %s%s%s%s",
1492 AMG_id2name(method + assignshift),
1493 (flags & AMGf_unary ? " " : "\n\tleft "),
1495 "in overloaded package ":
1496 "has no overloaded magic",
1498 HvNAME(SvSTASH(SvRV(left))):
1501 ",\n\tright argument in overloaded package ":
1504 : ",\n\tright argument has no overloaded magic"),
1506 HvNAME(SvSTASH(SvRV(right))):
1508 if (amtp && amtp->fallback >= AMGfallYES) {
1509 DEBUG_o( Perl_deb(aTHX_ "%s", SvPVX(msg)) );
1511 Perl_croak(aTHX_ "%"SVf, msg);
1515 force_cpy = force_cpy || assign;
1519 DEBUG_o( Perl_deb(aTHX_
1520 "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n",
1522 method+assignshift==off? "" :
1524 method+assignshift==off? "" :
1525 AMG_id2name(method+assignshift),
1526 method+assignshift==off? "" : "')",
1527 flags & AMGf_unary? "" :
1528 lr==1 ? " for right argument": " for left argument",
1529 flags & AMGf_unary? " for argument" : "",
1531 fl? ",\n\tassignment variant used": "") );
1533 /* Since we use shallow copy during assignment, we need
1534 * to dublicate the contents, probably calling user-supplied
1535 * version of copy operator
1537 /* We need to copy in following cases:
1538 * a) Assignment form was called.
1539 * assignshift==1, assign==T, method + 1 == off
1540 * b) Increment or decrement, called directly.
1541 * assignshift==0, assign==0, method + 0 == off
1542 * c) Increment or decrement, translated to assignment add/subtr.
1543 * assignshift==0, assign==T,
1545 * d) Increment or decrement, translated to nomethod.
1546 * assignshift==0, assign==0,
1548 * e) Assignment form translated to nomethod.
1549 * assignshift==1, assign==T, method + 1 != off
1552 /* off is method, method+assignshift, or a result of opcode substitution.
1553 * In the latter case assignshift==0, so only notfound case is important.
1555 if (( (method + assignshift == off)
1556 && (assign || (method == inc_amg) || (method == dec_amg)))
1563 bool oldcatch = CATCH_GET;
1566 Zero(&myop, 1, BINOP);
1567 myop.op_last = (OP *) &myop;
1568 myop.op_next = Nullop;
1569 myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED;
1571 PUSHSTACKi(PERLSI_OVERLOAD);
1574 PL_op = (OP *) &myop;
1575 if (PERLDB_SUB && PL_curstash != PL_debstash)
1576 PL_op->op_private |= OPpENTERSUB_DB;
1580 EXTEND(SP, notfound + 5);
1581 PUSHs(lr>0? right: left);
1582 PUSHs(lr>0? left: right);
1583 PUSHs( lr > 0 ? &PL_sv_yes : ( assign ? &PL_sv_undef : &PL_sv_no ));
1585 PUSHs( sv_2mortal(newSVpv(AMG_id2name(method + assignshift),0)));
1590 if ((PL_op = Perl_pp_entersub(aTHX)))
1598 CATCH_SET(oldcatch);
1605 ans=SvIV(res)<=0; break;
1608 ans=SvIV(res)<0; break;
1611 ans=SvIV(res)>=0; break;
1614 ans=SvIV(res)>0; break;
1617 ans=SvIV(res)==0; break;
1620 ans=SvIV(res)!=0; break;
1623 SvSetSV(left,res); return left;
1625 ans=!SvTRUE(res); break;
1628 } else if (method==copy_amg) {
1630 Perl_croak(aTHX_ "Copy method did not return a reference");
1632 return SvREFCNT_inc(SvRV(res));
1640 =for apidoc is_gv_magical
1642 Returns C<TRUE> if given the name of a magical GV.
1644 Currently only useful internally when determining if a GV should be
1645 created even in rvalue contexts.
1647 C<flags> is not used at present but available for future extension to
1648 allow selecting particular classes of magical variable.
1653 Perl_is_gv_magical(pTHX_ char *name, STRLEN len, U32 flags)
1660 if (len == 3 && strEQ(name, "ISA"))
1664 if (len == 8 && strEQ(name, "OVERLOAD"))
1668 if (len == 3 && strEQ(name, "SIG"))
1671 case '\017': /* $^O & $^OPEN */
1673 || (len == 4 && strEQ(name, "\027PEN")))
1678 case '\027': /* $^W & $^WARNING_BITS */
1680 || (len == 12 && strEQ(name, "\027ARNING_BITS"))
1681 || (len == 17 && strEQ(name, "\027IDE_SYSTEM_CALLS")))
1713 case '\001': /* $^A */
1714 case '\003': /* $^C */
1715 case '\004': /* $^D */
1716 case '\005': /* $^E */
1717 case '\006': /* $^F */
1718 case '\010': /* $^H */
1719 case '\011': /* $^I, NOT \t in EBCDIC */
1720 case '\014': /* $^L */
1721 case '\020': /* $^P */
1722 case '\023': /* $^S */
1723 case '\024': /* $^T */
1724 case '\026': /* $^V */
1738 char *end = name + len;
1739 while (--end > name) {