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)
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)))
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);
428 * Given &FOO::AUTOLOAD, set $FOO::AUTOLOAD to desired function name.
429 * The subroutine's original name may not be "AUTOLOAD", so we don't
430 * use that, but for lack of anything better we will use the sub's
431 * original package to look up $AUTOLOAD.
433 varstash = GvSTASH(CvGV(cv));
434 vargv = *(GV**)hv_fetch(varstash, autoload, autolen, TRUE);
438 sv_lock((SV *)varstash);
441 gv_init(vargv, varstash, autoload, autolen, FALSE);
447 sv_setpv(varsv, HvNAME(stash));
448 sv_catpvn(varsv, "::", 2);
449 sv_catpvn(varsv, name, len);
450 SvTAINTED_off(varsv);
455 =for apidoc gv_stashpv
457 Returns a pointer to the stash for a specified package. C<name> should
458 be a valid UTF-8 string. If C<create> is set then the package will be
459 created if it does not already exist. If C<create> is not set and the
460 package does not exist then NULL is returned.
466 Perl_gv_stashpv(pTHX_ const char *name, I32 create)
468 return gv_stashpvn(name, strlen(name), create);
472 Perl_gv_stashpvn(pTHX_ const char *name, U32 namelen, I32 create)
479 if (namelen + 3 < sizeof smallbuf)
482 New(606, tmpbuf, namelen + 3, char);
483 Copy(name,tmpbuf,namelen,char);
484 tmpbuf[namelen++] = ':';
485 tmpbuf[namelen++] = ':';
486 tmpbuf[namelen] = '\0';
487 tmpgv = gv_fetchpv(tmpbuf, create, SVt_PVHV);
488 if (tmpbuf != smallbuf)
493 GvHV(tmpgv) = newHV();
496 HvNAME(stash) = savepv(name);
501 =for apidoc gv_stashsv
503 Returns a pointer to the stash for a specified package, which must be a
504 valid UTF-8 string. See C<gv_stashpv>.
510 Perl_gv_stashsv(pTHX_ SV *sv, I32 create)
515 return gv_stashpvn(ptr, len, create);
520 Perl_gv_fetchpv(pTHX_ const char *nambeg, I32 add, I32 sv_type)
522 register const char *name = nambeg;
526 register const char *namend;
529 if (*name == '*' && isALPHA(name[1])) /* accidental stringify on a GV? */
532 for (namend = name; *namend; namend++) {
533 if ((*namend == ':' && namend[1] == ':')
534 || (*namend == '\'' && namend[1]))
538 if (!stash || !SvREFCNT(stash)) /* symbol table under destruction */
546 if (len + 3 < sizeof smallbuf)
549 New(601, tmpbuf, len+3, char);
550 Copy(name, tmpbuf, len, char);
554 gvp = (GV**)hv_fetch(stash,tmpbuf,len,add);
555 gv = gvp ? *gvp : Nullgv;
556 if (gv && gv != (GV*)&PL_sv_undef) {
557 if (SvTYPE(gv) != SVt_PVGV)
558 gv_init(gv, stash, tmpbuf, len, (add & GV_ADDMULTI));
562 if (tmpbuf != smallbuf)
564 if (!gv || gv == (GV*)&PL_sv_undef)
567 if (!(stash = GvHV(gv)))
568 stash = GvHV(gv) = newHV();
571 HvNAME(stash) = savepvn(nambeg, namend - nambeg);
579 return gv ? gv : (GV*)*hv_fetch(PL_defstash, "main::", 6, TRUE);
586 /* No stash in name, so see how we can default */
589 if (isIDFIRST_lazy(name)) {
592 if (isUPPER(*name)) {
593 if (*name == 'S' && (
594 strEQ(name, "SIG") ||
595 strEQ(name, "STDIN") ||
596 strEQ(name, "STDOUT") ||
597 strEQ(name, "STDERR")))
599 else if (*name == 'I' && strEQ(name, "INC"))
601 else if (*name == 'E' && strEQ(name, "ENV"))
603 else if (*name == 'A' && (
604 strEQ(name, "ARGV") ||
605 strEQ(name, "ARGVOUT")))
608 else if (*name == '_' && !name[1])
613 else if ((COP*)PL_curcop == &PL_compiling) {
615 if (add && (PL_hints & HINT_STRICT_VARS) &&
616 sv_type != SVt_PVCV &&
617 sv_type != SVt_PVGV &&
618 sv_type != SVt_PVFM &&
619 sv_type != SVt_PVIO &&
620 !(len == 1 && sv_type == SVt_PV && strchr("ab",*name)) )
622 gvp = (GV**)hv_fetch(stash,name,len,0);
624 *gvp == (GV*)&PL_sv_undef ||
625 SvTYPE(*gvp) != SVt_PVGV)
629 else if ((sv_type == SVt_PV && !GvIMPORTED_SV(*gvp)) ||
630 (sv_type == SVt_PVAV && !GvIMPORTED_AV(*gvp)) ||
631 (sv_type == SVt_PVHV && !GvIMPORTED_HV(*gvp)) )
633 Perl_warn(aTHX_ "Variable \"%c%s\" is not imported",
634 sv_type == SVt_PVAV ? '@' :
635 sv_type == SVt_PVHV ? '%' : '$',
638 Perl_warn(aTHX_ "\t(Did you mean &%s instead?)\n", name);
644 stash = CopSTASH(PL_curcop);
650 /* By this point we should have a stash and a name */
654 qerror(Perl_mess(aTHX_
655 "Global symbol \"%s%s\" requires explicit package name",
656 (sv_type == SVt_PV ? "$"
657 : sv_type == SVt_PVAV ? "@"
658 : sv_type == SVt_PVHV ? "%"
660 stash = PL_nullstash;
666 if (!SvREFCNT(stash)) /* symbol table under destruction */
669 gvp = (GV**)hv_fetch(stash,name,len,add);
670 if (!gvp || *gvp == (GV*)&PL_sv_undef)
673 if (SvTYPE(gv) == SVt_PVGV) {
676 gv_init_sv(gv, sv_type);
679 } else if (add & GV_NOINIT) {
683 /* Adding a new symbol */
685 if (add & GV_ADDWARN && ckWARN_d(WARN_INTERNAL))
686 Perl_warner(aTHX_ WARN_INTERNAL, "Had to create %s unexpectedly", nambeg);
687 gv_init(gv, stash, name, len, add & GV_ADDMULTI);
688 gv_init_sv(gv, sv_type);
690 if (isLEXWARN_on && isALPHA(name[0]) && ! ckWARN(WARN_ONCE))
693 /* set up magic where warranted */
696 if (strEQ(name, "ARGV")) {
697 IoFLAGS(GvIOn(gv)) |= IOf_ARGV|IOf_START;
701 if (strnEQ(name, "EXPORT", 6))
705 if (strEQ(name, "ISA")) {
708 sv_magic((SV*)av, (SV*)gv, 'I', Nullch, 0);
709 /* NOTE: No support for tied ISA */
710 if ((add & GV_ADDMULTI) && strEQ(nambeg,"AnyDBM_File::ISA")
711 && AvFILLp(av) == -1)
714 av_push(av, newSVpvn(pname = "NDBM_File",9));
715 gv_stashpvn(pname, 9, TRUE);
716 av_push(av, newSVpvn(pname = "DB_File",7));
717 gv_stashpvn(pname, 7, TRUE);
718 av_push(av, newSVpvn(pname = "GDBM_File",9));
719 gv_stashpvn(pname, 9, TRUE);
720 av_push(av, newSVpvn(pname = "SDBM_File",9));
721 gv_stashpvn(pname, 9, TRUE);
722 av_push(av, newSVpvn(pname = "ODBM_File",9));
723 gv_stashpvn(pname, 9, TRUE);
728 if (strEQ(name, "OVERLOAD")) {
731 hv_magic(hv, Nullgv, 'A');
735 if (strEQ(name, "SIG")) {
739 int sig_num[] = { SIG_NUM };
740 New(73, PL_psig_ptr, sizeof(sig_num)/sizeof(*sig_num), SV*);
741 New(73, PL_psig_name, sizeof(sig_num)/sizeof(*sig_num), SV*);
745 hv_magic(hv, Nullgv, 'S');
746 for (i = 1; PL_sig_name[i]; i++) {
748 init = hv_fetch(hv, PL_sig_name[i], strlen(PL_sig_name[i]), 1);
750 sv_setsv(*init, &PL_sv_undef);
757 if (strEQ(name, "VERSION"))
764 PL_sawampersand = TRUE;
770 PL_sawampersand = TRUE;
776 PL_sawampersand = TRUE;
782 sv_setpv(GvSV(gv),PL_chopset);
788 #ifdef COMPLEX_STATUS
789 (void)SvUPGRADE(GvSV(gv), SVt_PVLV);
796 if (sv_type > SVt_PV && PL_curcop != &PL_compiling) {
797 HV* stash = gv_stashpvn("Errno",5,FALSE);
798 if (!stash || !(gv_fetchmethod(stash, "TIEHASH"))) {
801 require_pv("Errno.pm");
803 stash = gv_stashpvn("Errno",5,FALSE);
804 if (!stash || !(gv_fetchmethod(stash, "TIEHASH")))
805 Perl_croak(aTHX_ "Can't use %%! because Errno.pm is not available");
814 sv_magic((SV*)av, Nullsv, 'D', Nullch, 0);
820 if (ckWARN(WARN_DEPRECATED) && len == 1 && sv_type == SVt_PV)
821 Perl_warner(aTHX_ WARN_DEPRECATED, "Use of $%s is deprecated", name);
836 case '\001': /* $^A */
837 case '\003': /* $^C */
838 case '\004': /* $^D */
839 case '\005': /* $^E */
840 case '\006': /* $^F */
841 case '\010': /* $^H */
842 case '\011': /* $^I, NOT \t in EBCDIC */
843 case '\020': /* $^P */
844 case '\024': /* $^T */
851 sv_setiv(GvSV(gv), (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0);
853 case '\017': /* $^O & $^OPEN */
854 if (len > 1 && strNE(name, "\017PEN"))
857 case '\023': /* $^S */
861 case '\027': /* $^W & $^WARNING_BITS */
862 if (len > 1 && strNE(name, "\027ARNING_BITS")
863 && strNE(name, "\027IDE_SYSTEM_CALLS"))
872 sv_magic((SV*)av, (SV*)av, 'D', Nullch, 0);
886 SvREADONLY_on(GvSV(gv));
888 sv_magic(GvSV(gv), (SV*)gv, 0, name, len);
891 case '\014': /* $^L */
894 sv_setpv(GvSV(gv),"\f");
895 PL_formfeed = GvSV(gv);
900 sv_setpv(GvSV(gv),"\034");
905 (void)SvUPGRADE(sv, SVt_PVNV);
906 Perl_sv_setpvf(aTHX_ sv,
907 #if defined(PERL_SUBVERSION) && (PERL_SUBVERSION > 0)
913 SvNVX(PL_patchlevel));
914 SvNVX(sv) = SvNVX(PL_patchlevel);
919 case '\026': /* $^V */
922 GvSV(gv) = SvREFCNT_inc(PL_patchlevel);
931 Perl_gv_fullname4(pTHX_ SV *sv, GV *gv, const char *prefix, bool keepmain)
933 HV *hv = GvSTASH(gv);
938 sv_setpv(sv, prefix ? prefix : "");
939 if (keepmain || strNE(HvNAME(hv), "main")) {
940 sv_catpv(sv,HvNAME(hv));
941 sv_catpvn(sv,"::", 2);
943 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
947 Perl_gv_fullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
949 HV *hv = GvSTASH(gv);
954 sv_setpv(sv, prefix ? prefix : "");
955 sv_catpv(sv,HvNAME(hv));
956 sv_catpvn(sv,"::", 2);
957 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
961 Perl_gv_efullname4(pTHX_ SV *sv, GV *gv, const char *prefix, bool keepmain)
966 gv_fullname4(sv, egv, prefix, keepmain);
970 Perl_gv_efullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
975 gv_fullname3(sv, egv, prefix);
978 /* XXX compatibility with versions <= 5.003. */
980 Perl_gv_fullname(pTHX_ SV *sv, GV *gv)
982 gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : "");
985 /* XXX compatibility with versions <= 5.003. */
987 Perl_gv_efullname(pTHX_ SV *sv, GV *gv)
989 gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : "");
998 io = (IO*)NEWSV(0,0);
999 sv_upgrade((SV *)io,SVt_PVIO);
1002 iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV);
1003 /* unless exists($main::{FileHandle}) and defined(%main::FileHandle::) */
1004 if (!(iogv && GvHV(iogv) && HvARRAY(GvHV(iogv))))
1005 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
1006 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
1011 Perl_gv_check(pTHX_ HV *stash)
1018 if (!HvARRAY(stash))
1020 for (i = 0; i <= (I32) HvMAX(stash); i++) {
1021 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
1022 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
1023 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)) && HvNAME(hv))
1025 if (hv != PL_defstash && hv != stash)
1026 gv_check(hv); /* nested package */
1028 else if (isALPHA(*HeKEY(entry))) {
1030 gv = (GV*)HeVAL(entry);
1031 if (SvTYPE(gv) != SVt_PVGV || GvMULTI(gv))
1034 /* performance hack: if filename is absolute and it's a standard
1035 * module, don't bother warning */
1037 && PERL_FILE_IS_ABSOLUTE(file)
1038 && (instr(file, "/lib/") || instr(file, ".pm")))
1042 CopLINE_set(PL_curcop, GvLINE(gv));
1044 CopFILE(PL_curcop) = file; /* set for warning */
1046 CopFILEGV(PL_curcop) = gv_fetchfile(file);
1048 Perl_warner(aTHX_ WARN_ONCE,
1049 "Name \"%s::%s\" used only once: possible typo",
1050 HvNAME(stash), GvNAME(gv));
1057 Perl_newGVgen(pTHX_ char *pack)
1059 return gv_fetchpv(Perl_form(aTHX_ "%s::_GEN_%ld", pack, (long)PL_gensym++),
1063 /* hopefully this is only called on local symbol table entries */
1066 Perl_gp_ref(pTHX_ GP *gp)
1073 /* multi-named GPs cannot be used for method cache */
1074 SvREFCNT_dec(gp->gp_cv);
1079 /* Adding a new name to a subroutine invalidates method cache */
1080 PL_sub_generation++;
1087 Perl_gp_free(pTHX_ GV *gv)
1091 if (!gv || !(gp = GvGP(gv)))
1093 if (gp->gp_refcnt == 0) {
1094 if (ckWARN_d(WARN_INTERNAL))
1095 Perl_warner(aTHX_ WARN_INTERNAL,
1096 "Attempt to free unreferenced glob pointers");
1100 /* Deleting the name of a subroutine invalidates method cache */
1101 PL_sub_generation++;
1103 if (--gp->gp_refcnt > 0) {
1104 if (gp->gp_egv == gv)
1109 SvREFCNT_dec(gp->gp_sv);
1110 SvREFCNT_dec(gp->gp_av);
1111 SvREFCNT_dec(gp->gp_hv);
1112 SvREFCNT_dec(gp->gp_io);
1113 SvREFCNT_dec(gp->gp_cv);
1114 SvREFCNT_dec(gp->gp_form);
1120 #if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
1124 #ifdef MICROPORT /* Microport 2.4 hack */
1128 if (GvGP(gv)->gp_av)
1129 return GvGP(gv)->gp_av;
1131 return GvGP(gv_AVadd(gv))->gp_av;
1137 if (GvGP(gv)->gp_hv)
1138 return GvGP(gv)->gp_hv;
1140 return GvGP(gv_HVadd(gv))->gp_hv;
1142 #endif /* Microport 2.4 hack */
1144 /* Updates and caches the CV's */
1147 Perl_Gv_AMupdate(pTHX_ HV *stash)
1151 MAGIC* mg=mg_find((SV*)stash,'c');
1152 AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL;
1156 if (mg && amtp->was_ok_am == PL_amagic_generation
1157 && amtp->was_ok_sub == PL_sub_generation)
1158 return AMT_OVERLOADED(amtp);
1159 if (amtp && AMT_AMAGIC(amtp)) { /* Have table. */
1161 for (i=1; i<NofAMmeth; i++) {
1162 if (amtp->table[i]) {
1163 SvREFCNT_dec(amtp->table[i]);
1167 sv_unmagic((SV*)stash, 'c');
1169 DEBUG_o( Perl_deb(aTHX_ "Recalcing overload magic in package %s\n",HvNAME(stash)) );
1171 amt.was_ok_am = PL_amagic_generation;
1172 amt.was_ok_sub = PL_sub_generation;
1173 amt.fallback = AMGfallNO;
1177 int filled = 0, have_ovl = 0;
1182 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
1184 /* Try to find via inheritance. */
1185 gv = gv_fetchmeth(stash, PL_AMG_names[0], 2, -1);
1190 lim = DESTROY_amg; /* Skip overloading entries. */
1191 else if (SvTRUE(sv))
1192 amt.fallback=AMGfallYES;
1194 amt.fallback=AMGfallNEVER;
1196 for (i = 1; i < lim; i++)
1197 amt.table[i] = Nullcv;
1198 for (; i < NofAMmeth; i++) {
1199 char *cooky = (char*)PL_AMG_names[i];
1200 /* Human-readable form, for debugging: */
1201 char *cp = (i >= DESTROY_amg ? cooky : AMG_id2name(i));
1202 STRLEN l = strlen(cooky);
1204 DEBUG_o( Perl_deb(aTHX_ "Checking overloading of `%s' in package `%.256s'\n",
1205 cp, HvNAME(stash)) );
1206 /* don't fill the cache while looking up! */
1207 gv = gv_fetchmeth(stash, cooky, l, -1);
1209 if (gv && (cv = GvCV(gv))) {
1210 if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil")
1211 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) {
1212 /* GvSV contains the name of the method. */
1215 DEBUG_o( Perl_deb(aTHX_ "Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n",
1216 SvPV(GvSV(gv), n_a), cp, HvNAME(stash)) );
1217 if (!SvPOK(GvSV(gv))
1218 || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)),
1221 /* Can be an import stub (created by `can'). */
1223 Perl_croak(aTHX_ "Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'",
1224 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1227 Perl_croak(aTHX_ "Can't resolve method `%.256s' overloading `%s' in package `%.256s'",
1228 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1231 cv = GvCV(gv = ngv);
1233 DEBUG_o( Perl_deb(aTHX_ "Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n",
1234 cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))),
1235 GvNAME(CvGV(cv))) );
1237 if (i < DESTROY_amg)
1240 amt.table[i]=(CV*)SvREFCNT_inc(cv);
1243 AMT_AMAGIC_on(&amt);
1245 AMT_OVERLOADED_on(&amt);
1246 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMT));
1250 /* Here we have no table: */
1252 AMT_AMAGIC_off(&amt);
1253 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMTS));
1259 Perl_gv_handler(pTHX_ HV *stash, I32 id)
1267 mg = mg_find((SV*)stash,'c');
1271 mg = mg_find((SV*)stash,'c');
1273 amtp = (AMT*)mg->mg_ptr;
1274 if ( amtp->was_ok_am != PL_amagic_generation
1275 || amtp->was_ok_sub != PL_sub_generation )
1277 if (AMT_AMAGIC(amtp))
1278 return amtp->table[id];
1284 Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags)
1288 CV **cvp=NULL, **ocvp=NULL;
1290 int fl=0, off, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
1291 int postpr = 0, force_cpy = 0, assignshift = assign ? 1 : 0;
1293 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
1294 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),'c'))
1295 && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1296 ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table
1298 && ((cv = cvp[off=method+assignshift])
1299 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
1301 (fl = 1, cv = cvp[off=method])))) {
1302 lr = -1; /* Call method for left argument */
1304 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
1307 /* look for substituted methods */
1308 /* In all the covered cases we should be called with assign==0. */
1312 if ((cv = cvp[off=add_ass_amg])
1313 || ((cv = cvp[off = add_amg]) && (force_cpy = 0, postpr = 1))) {
1314 right = &PL_sv_yes; lr = -1; assign = 1;
1319 if ((cv = cvp[off = subtr_ass_amg])
1320 || ((cv = cvp[off = subtr_amg]) && (force_cpy = 0, postpr=1))) {
1321 right = &PL_sv_yes; lr = -1; assign = 1;
1325 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
1328 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1331 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1334 (void)((cv = cvp[off=bool__amg])
1335 || (cv = cvp[off=numer_amg])
1336 || (cv = cvp[off=string_amg]));
1342 * SV* ref causes confusion with the interpreter variable of
1345 SV* tmpRef=SvRV(left);
1346 if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) {
1348 * Just to be extra cautious. Maybe in some
1349 * additional cases sv_setsv is safe, too.
1351 SV* newref = newSVsv(tmpRef);
1352 SvOBJECT_on(newref);
1353 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(tmpRef));
1359 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
1360 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
1361 SV* nullsv=sv_2mortal(newSViv(0));
1363 SV* lessp = amagic_call(left,nullsv,
1364 lt_amg,AMGf_noright);
1365 logic = SvTRUE(lessp);
1367 SV* lessp = amagic_call(left,nullsv,
1368 ncmp_amg,AMGf_noright);
1369 logic = (SvNV(lessp) < 0);
1372 if (off==subtr_amg) {
1383 if ((cv = cvp[off=subtr_amg])) {
1385 left = sv_2mortal(newSViv(0));
1389 case iter_amg: /* XXXX Eventually should do to_gv. */
1391 return NULL; /* Delegate operation to standard mechanisms. */
1399 return left; /* Delegate operation to standard mechanisms. */
1404 if (!cv) goto not_found;
1405 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1406 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c'))
1407 && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1408 ? (amtp = (AMT*)mg->mg_ptr)->table
1410 && (cv = cvp[off=method])) { /* Method for right
1413 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
1414 && (cvp=ocvp) && (lr = -1))
1415 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1416 && !(flags & AMGf_unary)) {
1417 /* We look for substitution for
1418 * comparison operations and
1420 if (method==concat_amg || method==concat_ass_amg
1421 || method==repeat_amg || method==repeat_ass_amg) {
1422 return NULL; /* Delegate operation to string conversion */
1432 postpr = 1; off=ncmp_amg; break;
1439 postpr = 1; off=scmp_amg; break;
1441 if (off != -1) cv = cvp[off];
1446 not_found: /* No method found, either report or croak */
1454 return left; /* Delegate operation to standard mechanisms. */
1457 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1458 notfound = 1; lr = -1;
1459 } else if (cvp && (cv=cvp[nomethod_amg])) {
1460 notfound = 1; lr = 1;
1463 if (off==-1) off=method;
1464 msg = sv_2mortal(Perl_newSVpvf(aTHX_
1465 "Operation `%s': no method found,%sargument %s%s%s%s",
1466 AMG_id2name(method + assignshift),
1467 (flags & AMGf_unary ? " " : "\n\tleft "),
1469 "in overloaded package ":
1470 "has no overloaded magic",
1472 HvNAME(SvSTASH(SvRV(left))):
1475 ",\n\tright argument in overloaded package ":
1478 : ",\n\tright argument has no overloaded magic"),
1480 HvNAME(SvSTASH(SvRV(right))):
1482 if (amtp && amtp->fallback >= AMGfallYES) {
1483 DEBUG_o( Perl_deb(aTHX_ "%s", SvPVX(msg)) );
1485 Perl_croak(aTHX_ "%"SVf, msg);
1489 force_cpy = force_cpy || assign;
1493 DEBUG_o( Perl_deb(aTHX_
1494 "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n",
1496 method+assignshift==off? "" :
1498 method+assignshift==off? "" :
1499 AMG_id2name(method+assignshift),
1500 method+assignshift==off? "" : "')",
1501 flags & AMGf_unary? "" :
1502 lr==1 ? " for right argument": " for left argument",
1503 flags & AMGf_unary? " for argument" : "",
1505 fl? ",\n\tassignment variant used": "") );
1507 /* Since we use shallow copy during assignment, we need
1508 * to dublicate the contents, probably calling user-supplied
1509 * version of copy operator
1511 /* We need to copy in following cases:
1512 * a) Assignment form was called.
1513 * assignshift==1, assign==T, method + 1 == off
1514 * b) Increment or decrement, called directly.
1515 * assignshift==0, assign==0, method + 0 == off
1516 * c) Increment or decrement, translated to assignment add/subtr.
1517 * assignshift==0, assign==T,
1519 * d) Increment or decrement, translated to nomethod.
1520 * assignshift==0, assign==0,
1522 * e) Assignment form translated to nomethod.
1523 * assignshift==1, assign==T, method + 1 != off
1526 /* off is method, method+assignshift, or a result of opcode substitution.
1527 * In the latter case assignshift==0, so only notfound case is important.
1529 if (( (method + assignshift == off)
1530 && (assign || (method == inc_amg) || (method == dec_amg)))
1537 bool oldcatch = CATCH_GET;
1540 Zero(&myop, 1, BINOP);
1541 myop.op_last = (OP *) &myop;
1542 myop.op_next = Nullop;
1543 myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED;
1545 PUSHSTACKi(PERLSI_OVERLOAD);
1548 PL_op = (OP *) &myop;
1549 if (PERLDB_SUB && PL_curstash != PL_debstash)
1550 PL_op->op_private |= OPpENTERSUB_DB;
1554 EXTEND(SP, notfound + 5);
1555 PUSHs(lr>0? right: left);
1556 PUSHs(lr>0? left: right);
1557 PUSHs( lr > 0 ? &PL_sv_yes : ( assign ? &PL_sv_undef : &PL_sv_no ));
1559 PUSHs( sv_2mortal(newSVpv(AMG_id2name(method + assignshift),0)));
1564 if ((PL_op = Perl_pp_entersub(aTHX)))
1572 CATCH_SET(oldcatch);
1579 ans=SvIV(res)<=0; break;
1582 ans=SvIV(res)<0; break;
1585 ans=SvIV(res)>=0; break;
1588 ans=SvIV(res)>0; break;
1591 ans=SvIV(res)==0; break;
1594 ans=SvIV(res)!=0; break;
1597 SvSetSV(left,res); return left;
1599 ans=!SvTRUE(res); break;
1602 } else if (method==copy_amg) {
1604 Perl_croak(aTHX_ "Copy method did not return a reference");
1606 return SvREFCNT_inc(SvRV(res));
1614 =for apidoc is_gv_magical
1616 Returns C<TRUE> if given the name of a magical GV.
1618 Currently only useful internally when determining if a GV should be
1619 created even in rvalue contexts.
1621 C<flags> is not used at present but available for future extension to
1622 allow selecting particular classes of magical variable.
1627 Perl_is_gv_magical(pTHX_ char *name, STRLEN len, U32 flags)
1634 if (len == 3 && strEQ(name, "ISA"))
1638 if (len == 8 && strEQ(name, "OVERLOAD"))
1642 if (len == 3 && strEQ(name, "SIG"))
1645 case '\017': /* $^O & $^OPEN */
1647 || (len == 4 && strEQ(name, "\027PEN")))
1652 case '\027': /* $^W & $^WARNING_BITS */
1654 || (len == 12 && strEQ(name, "\027ARNING_BITS"))
1655 || (len == 17 && strEQ(name, "\027IDE_SYSTEM_CALLS")))
1687 case '\001': /* $^A */
1688 case '\003': /* $^C */
1689 case '\004': /* $^D */
1690 case '\005': /* $^E */
1691 case '\006': /* $^F */
1692 case '\010': /* $^H */
1693 case '\011': /* $^I, NOT \t in EBCDIC */
1694 case '\014': /* $^L */
1695 case '\020': /* $^P */
1696 case '\023': /* $^S */
1697 case '\024': /* $^T */
1698 case '\026': /* $^V */
1712 char *end = name + len;
1713 while (--end > name) {