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 int sig_num[] = { SIG_NUM };
754 New(73, PL_psig_ptr, sizeof(sig_num)/sizeof(*sig_num), SV*);
755 New(73, PL_psig_name, sizeof(sig_num)/sizeof(*sig_num), SV*);
759 hv_magic(hv, Nullgv, 'S');
760 for (i = 1; PL_sig_name[i]; i++) {
762 init = hv_fetch(hv, PL_sig_name[i], strlen(PL_sig_name[i]), 1);
764 sv_setsv(*init, &PL_sv_undef);
771 if (strEQ(name, "VERSION"))
778 PL_sawampersand = TRUE;
784 PL_sawampersand = TRUE;
790 PL_sawampersand = TRUE;
796 sv_setpv(GvSV(gv),PL_chopset);
802 #ifdef COMPLEX_STATUS
803 (void)SvUPGRADE(GvSV(gv), SVt_PVLV);
810 if (sv_type > SVt_PV && PL_curcop != &PL_compiling) {
811 HV* stash = gv_stashpvn("Errno",5,FALSE);
812 if (!stash || !(gv_fetchmethod(stash, "TIEHASH"))) {
815 require_pv("Errno.pm");
817 stash = gv_stashpvn("Errno",5,FALSE);
818 if (!stash || !(gv_fetchmethod(stash, "TIEHASH")))
819 Perl_croak(aTHX_ "Can't use %%! because Errno.pm is not available");
828 sv_magic((SV*)av, Nullsv, 'D', Nullch, 0);
834 if (ckWARN(WARN_DEPRECATED) && len == 1 && sv_type == SVt_PV)
835 Perl_warner(aTHX_ WARN_DEPRECATED, "Use of $%s is deprecated", name);
850 case '\001': /* $^A */
851 case '\003': /* $^C */
852 case '\004': /* $^D */
853 case '\005': /* $^E */
854 case '\006': /* $^F */
855 case '\010': /* $^H */
856 case '\011': /* $^I, NOT \t in EBCDIC */
857 case '\020': /* $^P */
858 case '\024': /* $^T */
865 sv_setiv(GvSV(gv), (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0);
867 case '\017': /* $^O & $^OPEN */
868 if (len > 1 && strNE(name, "\017PEN"))
871 case '\023': /* $^S */
875 case '\027': /* $^W & $^WARNING_BITS */
876 if (len > 1 && strNE(name, "\027ARNING_BITS")
877 && strNE(name, "\027IDE_SYSTEM_CALLS"))
886 sv_magic((SV*)av, (SV*)av, 'D', Nullch, 0);
900 SvREADONLY_on(GvSV(gv));
902 sv_magic(GvSV(gv), (SV*)gv, 0, name, len);
905 case '\014': /* $^L */
908 sv_setpv(GvSV(gv),"\f");
909 PL_formfeed = GvSV(gv);
914 sv_setpv(GvSV(gv),"\034");
919 (void)SvUPGRADE(sv, SVt_PVNV);
920 Perl_sv_setpvf(aTHX_ sv,
921 #if defined(PERL_SUBVERSION) && (PERL_SUBVERSION > 0)
927 SvNVX(PL_patchlevel));
928 SvNVX(sv) = SvNVX(PL_patchlevel);
933 case '\026': /* $^V */
936 GvSV(gv) = SvREFCNT_inc(PL_patchlevel);
945 Perl_gv_fullname4(pTHX_ SV *sv, GV *gv, const char *prefix, bool keepmain)
947 HV *hv = GvSTASH(gv);
952 sv_setpv(sv, prefix ? prefix : "");
953 if (keepmain || strNE(HvNAME(hv), "main")) {
954 sv_catpv(sv,HvNAME(hv));
955 sv_catpvn(sv,"::", 2);
957 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
961 Perl_gv_fullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
963 HV *hv = GvSTASH(gv);
968 sv_setpv(sv, prefix ? prefix : "");
969 sv_catpv(sv,HvNAME(hv));
970 sv_catpvn(sv,"::", 2);
971 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
975 Perl_gv_efullname4(pTHX_ SV *sv, GV *gv, const char *prefix, bool keepmain)
980 gv_fullname4(sv, egv, prefix, keepmain);
984 Perl_gv_efullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
989 gv_fullname3(sv, egv, prefix);
992 /* XXX compatibility with versions <= 5.003. */
994 Perl_gv_fullname(pTHX_ SV *sv, GV *gv)
996 gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : "");
999 /* XXX compatibility with versions <= 5.003. */
1001 Perl_gv_efullname(pTHX_ SV *sv, GV *gv)
1003 gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : "");
1012 io = (IO*)NEWSV(0,0);
1013 sv_upgrade((SV *)io,SVt_PVIO);
1016 iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV);
1017 /* unless exists($main::{FileHandle}) and defined(%main::FileHandle::) */
1018 if (!(iogv && GvHV(iogv) && HvARRAY(GvHV(iogv))))
1019 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
1020 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
1025 Perl_gv_check(pTHX_ HV *stash)
1032 if (!HvARRAY(stash))
1034 for (i = 0; i <= (I32) HvMAX(stash); i++) {
1035 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
1036 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
1037 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)) && HvNAME(hv))
1039 if (hv != PL_defstash && hv != stash)
1040 gv_check(hv); /* nested package */
1042 else if (isALPHA(*HeKEY(entry))) {
1044 gv = (GV*)HeVAL(entry);
1045 if (SvTYPE(gv) != SVt_PVGV || GvMULTI(gv))
1048 /* performance hack: if filename is absolute and it's a standard
1049 * module, don't bother warning */
1051 && PERL_FILE_IS_ABSOLUTE(file)
1052 && (instr(file, "/lib/") || instr(file, ".pm")))
1056 CopLINE_set(PL_curcop, GvLINE(gv));
1058 CopFILE(PL_curcop) = file; /* set for warning */
1060 CopFILEGV(PL_curcop) = gv_fetchfile(file);
1062 Perl_warner(aTHX_ WARN_ONCE,
1063 "Name \"%s::%s\" used only once: possible typo",
1064 HvNAME(stash), GvNAME(gv));
1071 Perl_newGVgen(pTHX_ char *pack)
1073 return gv_fetchpv(Perl_form(aTHX_ "%s::_GEN_%ld", pack, (long)PL_gensym++),
1077 /* hopefully this is only called on local symbol table entries */
1080 Perl_gp_ref(pTHX_ GP *gp)
1087 /* multi-named GPs cannot be used for method cache */
1088 SvREFCNT_dec(gp->gp_cv);
1093 /* Adding a new name to a subroutine invalidates method cache */
1094 PL_sub_generation++;
1101 Perl_gp_free(pTHX_ GV *gv)
1105 if (!gv || !(gp = GvGP(gv)))
1107 if (gp->gp_refcnt == 0) {
1108 if (ckWARN_d(WARN_INTERNAL))
1109 Perl_warner(aTHX_ WARN_INTERNAL,
1110 "Attempt to free unreferenced glob pointers");
1114 /* Deleting the name of a subroutine invalidates method cache */
1115 PL_sub_generation++;
1117 if (--gp->gp_refcnt > 0) {
1118 if (gp->gp_egv == gv)
1123 SvREFCNT_dec(gp->gp_sv);
1124 SvREFCNT_dec(gp->gp_av);
1125 SvREFCNT_dec(gp->gp_hv);
1126 SvREFCNT_dec(gp->gp_io);
1127 SvREFCNT_dec(gp->gp_cv);
1128 SvREFCNT_dec(gp->gp_form);
1134 #if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
1138 #ifdef MICROPORT /* Microport 2.4 hack */
1142 if (GvGP(gv)->gp_av)
1143 return GvGP(gv)->gp_av;
1145 return GvGP(gv_AVadd(gv))->gp_av;
1151 if (GvGP(gv)->gp_hv)
1152 return GvGP(gv)->gp_hv;
1154 return GvGP(gv_HVadd(gv))->gp_hv;
1156 #endif /* Microport 2.4 hack */
1158 /* Updates and caches the CV's */
1161 Perl_Gv_AMupdate(pTHX_ HV *stash)
1165 MAGIC* mg=mg_find((SV*)stash,'c');
1166 AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL;
1170 if (mg && amtp->was_ok_am == PL_amagic_generation
1171 && amtp->was_ok_sub == PL_sub_generation)
1172 return AMT_OVERLOADED(amtp);
1173 if (amtp && AMT_AMAGIC(amtp)) { /* Have table. */
1175 for (i=1; i<NofAMmeth; i++) {
1176 if (amtp->table[i]) {
1177 SvREFCNT_dec(amtp->table[i]);
1181 sv_unmagic((SV*)stash, 'c');
1183 DEBUG_o( Perl_deb(aTHX_ "Recalcing overload magic in package %s\n",HvNAME(stash)) );
1185 amt.was_ok_am = PL_amagic_generation;
1186 amt.was_ok_sub = PL_sub_generation;
1187 amt.fallback = AMGfallNO;
1191 int filled = 0, have_ovl = 0;
1196 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
1198 /* Try to find via inheritance. */
1199 gv = gv_fetchmeth(stash, PL_AMG_names[0], 2, -1);
1204 lim = DESTROY_amg; /* Skip overloading entries. */
1205 else if (SvTRUE(sv))
1206 amt.fallback=AMGfallYES;
1208 amt.fallback=AMGfallNEVER;
1210 for (i = 1; i < lim; i++)
1211 amt.table[i] = Nullcv;
1212 for (; i < NofAMmeth; i++) {
1213 char *cooky = (char*)PL_AMG_names[i];
1214 /* Human-readable form, for debugging: */
1215 char *cp = (i >= DESTROY_amg ? cooky : AMG_id2name(i));
1216 STRLEN l = strlen(cooky);
1218 DEBUG_o( Perl_deb(aTHX_ "Checking overloading of `%s' in package `%.256s'\n",
1219 cp, HvNAME(stash)) );
1220 /* don't fill the cache while looking up! */
1221 gv = gv_fetchmeth(stash, cooky, l, -1);
1223 if (gv && (cv = GvCV(gv))) {
1224 if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil")
1225 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) {
1226 /* GvSV contains the name of the method. */
1229 DEBUG_o( Perl_deb(aTHX_ "Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n",
1230 SvPV(GvSV(gv), n_a), cp, HvNAME(stash)) );
1231 if (!SvPOK(GvSV(gv))
1232 || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)),
1235 /* Can be an import stub (created by `can'). */
1237 Perl_croak(aTHX_ "Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'",
1238 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1241 Perl_croak(aTHX_ "Can't resolve method `%.256s' overloading `%s' in package `%.256s'",
1242 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1245 cv = GvCV(gv = ngv);
1247 DEBUG_o( Perl_deb(aTHX_ "Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n",
1248 cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))),
1249 GvNAME(CvGV(cv))) );
1251 if (i < DESTROY_amg)
1254 amt.table[i]=(CV*)SvREFCNT_inc(cv);
1257 AMT_AMAGIC_on(&amt);
1259 AMT_OVERLOADED_on(&amt);
1260 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMT));
1264 /* Here we have no table: */
1266 AMT_AMAGIC_off(&amt);
1267 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMTS));
1273 Perl_gv_handler(pTHX_ HV *stash, I32 id)
1281 mg = mg_find((SV*)stash,'c');
1285 mg = mg_find((SV*)stash,'c');
1287 amtp = (AMT*)mg->mg_ptr;
1288 if ( amtp->was_ok_am != PL_amagic_generation
1289 || amtp->was_ok_sub != PL_sub_generation )
1291 if (AMT_AMAGIC(amtp))
1292 return amtp->table[id];
1298 Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags)
1302 CV **cvp=NULL, **ocvp=NULL;
1304 int fl=0, off, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
1305 int postpr = 0, force_cpy = 0, assignshift = assign ? 1 : 0;
1307 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
1308 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),'c'))
1309 && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1310 ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table
1312 && ((cv = cvp[off=method+assignshift])
1313 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
1315 (fl = 1, cv = cvp[off=method])))) {
1316 lr = -1; /* Call method for left argument */
1318 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
1321 /* look for substituted methods */
1322 /* In all the covered cases we should be called with assign==0. */
1326 if ((cv = cvp[off=add_ass_amg])
1327 || ((cv = cvp[off = add_amg]) && (force_cpy = 0, postpr = 1))) {
1328 right = &PL_sv_yes; lr = -1; assign = 1;
1333 if ((cv = cvp[off = subtr_ass_amg])
1334 || ((cv = cvp[off = subtr_amg]) && (force_cpy = 0, postpr=1))) {
1335 right = &PL_sv_yes; lr = -1; assign = 1;
1339 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
1342 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1345 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1348 (void)((cv = cvp[off=bool__amg])
1349 || (cv = cvp[off=numer_amg])
1350 || (cv = cvp[off=string_amg]));
1356 * SV* ref causes confusion with the interpreter variable of
1359 SV* tmpRef=SvRV(left);
1360 if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) {
1362 * Just to be extra cautious. Maybe in some
1363 * additional cases sv_setsv is safe, too.
1365 SV* newref = newSVsv(tmpRef);
1366 SvOBJECT_on(newref);
1367 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(tmpRef));
1373 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
1374 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
1375 SV* nullsv=sv_2mortal(newSViv(0));
1377 SV* lessp = amagic_call(left,nullsv,
1378 lt_amg,AMGf_noright);
1379 logic = SvTRUE(lessp);
1381 SV* lessp = amagic_call(left,nullsv,
1382 ncmp_amg,AMGf_noright);
1383 logic = (SvNV(lessp) < 0);
1386 if (off==subtr_amg) {
1397 if ((cv = cvp[off=subtr_amg])) {
1399 left = sv_2mortal(newSViv(0));
1403 case iter_amg: /* XXXX Eventually should do to_gv. */
1405 return NULL; /* Delegate operation to standard mechanisms. */
1413 return left; /* Delegate operation to standard mechanisms. */
1418 if (!cv) goto not_found;
1419 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1420 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c'))
1421 && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1422 ? (amtp = (AMT*)mg->mg_ptr)->table
1424 && (cv = cvp[off=method])) { /* Method for right
1427 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
1428 && (cvp=ocvp) && (lr = -1))
1429 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1430 && !(flags & AMGf_unary)) {
1431 /* We look for substitution for
1432 * comparison operations and
1434 if (method==concat_amg || method==concat_ass_amg
1435 || method==repeat_amg || method==repeat_ass_amg) {
1436 return NULL; /* Delegate operation to string conversion */
1446 postpr = 1; off=ncmp_amg; break;
1453 postpr = 1; off=scmp_amg; break;
1455 if (off != -1) cv = cvp[off];
1460 not_found: /* No method found, either report or croak */
1468 return left; /* Delegate operation to standard mechanisms. */
1471 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1472 notfound = 1; lr = -1;
1473 } else if (cvp && (cv=cvp[nomethod_amg])) {
1474 notfound = 1; lr = 1;
1477 if (off==-1) off=method;
1478 msg = sv_2mortal(Perl_newSVpvf(aTHX_
1479 "Operation `%s': no method found,%sargument %s%s%s%s",
1480 AMG_id2name(method + assignshift),
1481 (flags & AMGf_unary ? " " : "\n\tleft "),
1483 "in overloaded package ":
1484 "has no overloaded magic",
1486 HvNAME(SvSTASH(SvRV(left))):
1489 ",\n\tright argument in overloaded package ":
1492 : ",\n\tright argument has no overloaded magic"),
1494 HvNAME(SvSTASH(SvRV(right))):
1496 if (amtp && amtp->fallback >= AMGfallYES) {
1497 DEBUG_o( Perl_deb(aTHX_ "%s", SvPVX(msg)) );
1499 Perl_croak(aTHX_ "%"SVf, msg);
1503 force_cpy = force_cpy || assign;
1507 DEBUG_o( Perl_deb(aTHX_
1508 "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n",
1510 method+assignshift==off? "" :
1512 method+assignshift==off? "" :
1513 AMG_id2name(method+assignshift),
1514 method+assignshift==off? "" : "')",
1515 flags & AMGf_unary? "" :
1516 lr==1 ? " for right argument": " for left argument",
1517 flags & AMGf_unary? " for argument" : "",
1519 fl? ",\n\tassignment variant used": "") );
1521 /* Since we use shallow copy during assignment, we need
1522 * to dublicate the contents, probably calling user-supplied
1523 * version of copy operator
1525 /* We need to copy in following cases:
1526 * a) Assignment form was called.
1527 * assignshift==1, assign==T, method + 1 == off
1528 * b) Increment or decrement, called directly.
1529 * assignshift==0, assign==0, method + 0 == off
1530 * c) Increment or decrement, translated to assignment add/subtr.
1531 * assignshift==0, assign==T,
1533 * d) Increment or decrement, translated to nomethod.
1534 * assignshift==0, assign==0,
1536 * e) Assignment form translated to nomethod.
1537 * assignshift==1, assign==T, method + 1 != off
1540 /* off is method, method+assignshift, or a result of opcode substitution.
1541 * In the latter case assignshift==0, so only notfound case is important.
1543 if (( (method + assignshift == off)
1544 && (assign || (method == inc_amg) || (method == dec_amg)))
1551 bool oldcatch = CATCH_GET;
1554 Zero(&myop, 1, BINOP);
1555 myop.op_last = (OP *) &myop;
1556 myop.op_next = Nullop;
1557 myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED;
1559 PUSHSTACKi(PERLSI_OVERLOAD);
1562 PL_op = (OP *) &myop;
1563 if (PERLDB_SUB && PL_curstash != PL_debstash)
1564 PL_op->op_private |= OPpENTERSUB_DB;
1568 EXTEND(SP, notfound + 5);
1569 PUSHs(lr>0? right: left);
1570 PUSHs(lr>0? left: right);
1571 PUSHs( lr > 0 ? &PL_sv_yes : ( assign ? &PL_sv_undef : &PL_sv_no ));
1573 PUSHs( sv_2mortal(newSVpv(AMG_id2name(method + assignshift),0)));
1578 if ((PL_op = Perl_pp_entersub(aTHX)))
1586 CATCH_SET(oldcatch);
1593 ans=SvIV(res)<=0; break;
1596 ans=SvIV(res)<0; break;
1599 ans=SvIV(res)>=0; break;
1602 ans=SvIV(res)>0; break;
1605 ans=SvIV(res)==0; break;
1608 ans=SvIV(res)!=0; break;
1611 SvSetSV(left,res); return left;
1613 ans=!SvTRUE(res); break;
1616 } else if (method==copy_amg) {
1618 Perl_croak(aTHX_ "Copy method did not return a reference");
1620 return SvREFCNT_inc(SvRV(res));
1628 =for apidoc is_gv_magical
1630 Returns C<TRUE> if given the name of a magical GV.
1632 Currently only useful internally when determining if a GV should be
1633 created even in rvalue contexts.
1635 C<flags> is not used at present but available for future extension to
1636 allow selecting particular classes of magical variable.
1641 Perl_is_gv_magical(pTHX_ char *name, STRLEN len, U32 flags)
1648 if (len == 3 && strEQ(name, "ISA"))
1652 if (len == 8 && strEQ(name, "OVERLOAD"))
1656 if (len == 3 && strEQ(name, "SIG"))
1659 case '\017': /* $^O & $^OPEN */
1661 || (len == 4 && strEQ(name, "\027PEN")))
1666 case '\027': /* $^W & $^WARNING_BITS */
1668 || (len == 12 && strEQ(name, "\027ARNING_BITS"))
1669 || (len == 17 && strEQ(name, "\027IDE_SYSTEM_CALLS")))
1701 case '\001': /* $^A */
1702 case '\003': /* $^C */
1703 case '\004': /* $^D */
1704 case '\005': /* $^E */
1705 case '\006': /* $^F */
1706 case '\010': /* $^H */
1707 case '\011': /* $^I, NOT \t in EBCDIC */
1708 case '\014': /* $^L */
1709 case '\020': /* $^P */
1710 case '\023': /* $^S */
1711 case '\024': /* $^T */
1712 case '\026': /* $^V */
1726 char *end = name + len;
1727 while (--end > name) {