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");
49 #ifdef GV_SHARED_CHECK
51 Perl_croak(aTHX_ "Bad symbol for filehandle (GV is shared)");
60 Perl_gv_fetchfile(pTHX_ const char *name)
70 tmplen = strlen(name) + 2;
71 if (tmplen < sizeof smallbuf)
74 New(603, tmpbuf, tmplen + 1, char);
77 strcpy(tmpbuf + 2, name);
78 gv = *(GV**)hv_fetch(PL_defstash, tmpbuf, tmplen, TRUE);
80 gv_init(gv, PL_defstash, tmpbuf, tmplen, FALSE);
81 sv_setpv(GvSV(gv), name);
83 hv_magic(GvHVn(gv_AVadd(gv)), Nullgv, 'L');
85 if (tmpbuf != smallbuf)
91 Perl_gv_init(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len, int multi)
94 bool doproto = SvTYPE(gv) > SVt_NULL;
95 char *proto = (doproto && SvPOK(gv)) ? SvPVX(gv) : NULL;
97 sv_upgrade((SV*)gv, SVt_PVGV);
106 Newz(602, gp, 1, GP);
107 GvGP(gv) = gp_ref(gp);
108 GvSV(gv) = NEWSV(72,0);
109 GvLINE(gv) = CopLINE(PL_curcop);
110 GvFILE(gv) = CopFILE(PL_curcop) ? CopFILE(PL_curcop) : "";
113 sv_magic((SV*)gv, (SV*)gv, '*', Nullch, 0);
114 GvSTASH(gv) = (HV*)SvREFCNT_inc(stash);
115 GvNAME(gv) = savepvn(name, len);
117 if (multi || doproto) /* doproto means it _was_ mentioned */
119 if (doproto) { /* Replicate part of newSUB here. */
122 /* XXX unsafe for threads if eval_owner isn't held */
123 start_subparse(0,0); /* Create CV in compcv. */
124 GvCV(gv) = PL_compcv;
128 CvGV(GvCV(gv)) = (GV*)SvREFCNT_inc(gv);
129 CvFILE(GvCV(gv)) = CopFILE(PL_curcop);
130 CvSTASH(GvCV(gv)) = PL_curstash;
132 CvOWNER(GvCV(gv)) = 0;
133 if (!CvMUTEXP(GvCV(gv))) {
134 New(666, CvMUTEXP(GvCV(gv)), 1, perl_mutex);
135 MUTEX_INIT(CvMUTEXP(GvCV(gv)));
137 #endif /* USE_THREADS */
139 sv_setpv((SV*)GvCV(gv), proto);
146 S_gv_init_sv(pTHX_ GV *gv, I32 sv_type)
162 =for apidoc gv_fetchmeth
164 Returns the glob with the given C<name> and a defined subroutine or
165 C<NULL>. The glob lives in the given C<stash>, or in the stashes
166 accessible via @ISA and @UNIVERSAL.
168 The argument C<level> should be either 0 or -1. If C<level==0>, as a
169 side-effect creates a glob with the given C<name> in the given C<stash>
170 which in the case of success contains an alias for the subroutine, and sets
171 up caching info for this glob. Similarly for all the searched stashes.
173 This function grants C<"SUPER"> token as a postfix of the stash name. The
174 GV returned from C<gv_fetchmeth> may be a method cache entry, which is not
175 visible to Perl code. So when calling C<call_sv>, you should not use
176 the GV directly; instead, you should use the method's CV, which can be
177 obtained from the GV with the C<GvCV> macro.
183 Perl_gv_fetchmeth(pTHX_ HV *stash, const char *name, STRLEN len, I32 level)
193 if ((level > 100) || (level < -100))
194 Perl_croak(aTHX_ "Recursive inheritance detected while looking for method '%s' in package '%s'",
195 name, HvNAME(stash));
197 DEBUG_o( Perl_deb(aTHX_ "Looking for method %s in package %s\n",name,HvNAME(stash)) );
199 gvp = (GV**)hv_fetch(stash, name, len, (level >= 0));
204 if (SvTYPE(topgv) != SVt_PVGV)
205 gv_init(topgv, stash, name, len, TRUE);
206 if ((cv = GvCV(topgv))) {
207 /* If genuine method or valid cache entry, use it */
208 if (!GvCVGEN(topgv) || GvCVGEN(topgv) == PL_sub_generation)
210 /* Stale cached entry: junk it */
212 GvCV(topgv) = cv = Nullcv;
215 else if (GvCVGEN(topgv) == PL_sub_generation)
216 return 0; /* cache indicates sub doesn't exist */
219 gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE);
220 av = (gvp && (gv = *gvp) && gv != (GV*)&PL_sv_undef) ? GvAV(gv) : Nullav;
222 /* create and re-create @.*::SUPER::ISA on demand */
223 if (!av || !SvMAGIC(av)) {
224 char* packname = HvNAME(stash);
225 STRLEN packlen = strlen(packname);
227 if (packlen >= 7 && strEQ(packname + packlen - 7, "::SUPER")) {
231 basestash = gv_stashpvn(packname, packlen, TRUE);
232 gvp = (GV**)hv_fetch(basestash, "ISA", 3, FALSE);
233 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
234 gvp = (GV**)hv_fetch(stash, "ISA", 3, TRUE);
235 if (!gvp || !(gv = *gvp))
236 Perl_croak(aTHX_ "Cannot create %s::ISA", HvNAME(stash));
237 if (SvTYPE(gv) != SVt_PVGV)
238 gv_init(gv, stash, "ISA", 3, TRUE);
239 SvREFCNT_dec(GvAV(gv));
240 GvAV(gv) = (AV*)SvREFCNT_inc(av);
246 SV** svp = AvARRAY(av);
247 /* NOTE: No support for tied ISA */
248 I32 items = AvFILLp(av) + 1;
251 HV* basestash = gv_stashsv(sv, FALSE);
253 if (ckWARN(WARN_MISC))
254 Perl_warner(aTHX_ WARN_MISC, "Can't locate package %s for @%s::ISA",
255 SvPVX(sv), HvNAME(stash));
258 gv = gv_fetchmeth(basestash, name, len,
259 (level >= 0) ? level + 1 : level - 1);
265 /* if at top level, try UNIVERSAL */
267 if (level == 0 || level == -1) {
270 if ((lastchance = gv_stashpvn("UNIVERSAL", 9, FALSE))) {
271 if ((gv = gv_fetchmeth(lastchance, name, len,
272 (level >= 0) ? level + 1 : level - 1)))
276 * Cache method in topgv if:
277 * 1. topgv has no synonyms (else inheritance crosses wires)
278 * 2. method isn't a stub (else AUTOLOAD fails spectacularly)
281 GvREFCNT(topgv) == 1 &&
283 (CvROOT(cv) || CvXSUB(cv)))
285 if ((cv = GvCV(topgv)))
287 GvCV(topgv) = (CV*)SvREFCNT_inc(GvCV(gv));
288 GvCVGEN(topgv) = PL_sub_generation;
292 else if (topgv && GvREFCNT(topgv) == 1) {
293 /* cache the fact that the method is not defined */
294 GvCVGEN(topgv) = PL_sub_generation;
303 =for apidoc gv_fetchmethod
305 See L<gv_fetchmethod_autoload>.
311 Perl_gv_fetchmethod(pTHX_ HV *stash, const char *name)
313 return gv_fetchmethod_autoload(stash, name, TRUE);
317 =for apidoc gv_fetchmethod_autoload
319 Returns the glob which contains the subroutine to call to invoke the method
320 on the C<stash>. In fact in the presence of autoloading this may be the
321 glob for "AUTOLOAD". In this case the corresponding variable $AUTOLOAD is
324 The third parameter of C<gv_fetchmethod_autoload> determines whether
325 AUTOLOAD lookup is performed if the given method is not present: non-zero
326 means yes, look for AUTOLOAD; zero means no, don't look for AUTOLOAD.
327 Calling C<gv_fetchmethod> is equivalent to calling C<gv_fetchmethod_autoload>
328 with a non-zero C<autoload> parameter.
330 These functions grant C<"SUPER"> token as a prefix of the method name. Note
331 that if you want to keep the returned glob for a long time, you need to
332 check for it being "AUTOLOAD", since at the later time the call may load a
333 different subroutine due to $AUTOLOAD changing its value. Use the glob
334 created via a side effect to do this.
336 These functions have the same side-effects and as C<gv_fetchmeth> with
337 C<level==0>. C<name> should be writable if contains C<':'> or C<'
338 ''>. The warning against passing the GV returned by C<gv_fetchmeth> to
339 C<call_sv> apply equally to these functions.
345 Perl_gv_fetchmethod_autoload(pTHX_ HV *stash, const char *name, I32 autoload)
347 register const char *nend;
348 const char *nsplit = 0;
351 for (nend = name; *nend; nend++) {
354 else if (*nend == ':' && *(nend + 1) == ':')
358 const char *origname = name;
362 if ((nsplit - origname) == 5 && strnEQ(origname, "SUPER", 5)) {
363 /* ->SUPER::method should really be looked up in original stash */
364 SV *tmpstr = sv_2mortal(Perl_newSVpvf(aTHX_ "%s::SUPER",
365 CopSTASHPV(PL_curcop)));
366 stash = gv_stashpvn(SvPVX(tmpstr), SvCUR(tmpstr), TRUE);
367 DEBUG_o( Perl_deb(aTHX_ "Treating %s as %s::%s\n",
368 origname, HvNAME(stash), name) );
371 stash = gv_stashpvn(origname, nsplit - origname, TRUE);
374 gv = gv_fetchmeth(stash, name, nend - name, 0);
376 if (strEQ(name,"import") || strEQ(name,"unimport"))
377 gv = (GV*)&PL_sv_yes;
379 gv = gv_autoload4(stash, name, nend - name, TRUE);
383 if (!CvROOT(cv) && !CvXSUB(cv)) {
391 if (GvCV(stubgv) != cv) /* orphaned import */
394 autogv = gv_autoload4(GvSTASH(stubgv),
395 GvNAME(stubgv), GvNAMELEN(stubgv), TRUE);
405 Perl_gv_autoload4(pTHX_ HV *stash, const char *name, STRLEN len, I32 method)
407 static char autoload[] = "AUTOLOAD";
408 static STRLEN autolen = 8;
415 if (len == autolen && strnEQ(name, autoload, autolen))
417 if (!(gv = gv_fetchmeth(stash, autoload, autolen, FALSE)))
421 if (!(CvROOT(cv) || CvXSUB(cv)))
425 * Inheriting AUTOLOAD for non-methods works ... for now.
427 if (ckWARN(WARN_DEPRECATED) && !method &&
428 (GvCVGEN(gv) || GvSTASH(gv) != stash))
429 Perl_warner(aTHX_ WARN_DEPRECATED,
430 "Use of inherited AUTOLOAD for non-method %s::%.*s() is deprecated",
431 HvNAME(stash), (int)len, name);
435 /* rather than lookup/init $AUTOLOAD here
436 * only to have the XSUB do another lookup for $AUTOLOAD
437 * and split that value on the last '::',
438 * pass along the same data via some unused fields in the CV
441 SvPVX(cv) = (char *)name; /* cast to loose constness warning */
448 * Given &FOO::AUTOLOAD, set $FOO::AUTOLOAD to desired function name.
449 * The subroutine's original name may not be "AUTOLOAD", so we don't
450 * use that, but for lack of anything better we will use the sub's
451 * original package to look up $AUTOLOAD.
453 varstash = GvSTASH(CvGV(cv));
454 vargv = *(GV**)hv_fetch(varstash, autoload, autolen, TRUE);
458 sv_lock((SV *)varstash);
461 gv_init(vargv, varstash, autoload, autolen, FALSE);
467 sv_setpv(varsv, HvNAME(stash));
468 sv_catpvn(varsv, "::", 2);
469 sv_catpvn(varsv, name, len);
470 SvTAINTED_off(varsv);
475 =for apidoc gv_stashpv
477 Returns a pointer to the stash for a specified package. C<name> should
478 be a valid UTF-8 string. If C<create> is set then the package will be
479 created if it does not already exist. If C<create> is not set and the
480 package does not exist then NULL is returned.
486 Perl_gv_stashpv(pTHX_ const char *name, I32 create)
488 return gv_stashpvn(name, strlen(name), create);
492 Perl_gv_stashpvn(pTHX_ const char *name, U32 namelen, I32 create)
499 if (namelen + 3 < sizeof smallbuf)
502 New(606, tmpbuf, namelen + 3, char);
503 Copy(name,tmpbuf,namelen,char);
504 tmpbuf[namelen++] = ':';
505 tmpbuf[namelen++] = ':';
506 tmpbuf[namelen] = '\0';
507 tmpgv = gv_fetchpv(tmpbuf, create, SVt_PVHV);
508 if (tmpbuf != smallbuf)
513 GvHV(tmpgv) = newHV();
516 HvNAME(stash) = savepv(name);
521 =for apidoc gv_stashsv
523 Returns a pointer to the stash for a specified package, which must be a
524 valid UTF-8 string. See C<gv_stashpv>.
530 Perl_gv_stashsv(pTHX_ SV *sv, I32 create)
535 return gv_stashpvn(ptr, len, create);
540 Perl_gv_fetchpv(pTHX_ const char *nambeg, I32 add, I32 sv_type)
542 register const char *name = nambeg;
546 register const char *namend;
549 if (*name == '*' && isALPHA(name[1])) /* accidental stringify on a GV? */
552 for (namend = name; *namend; namend++) {
553 if ((*namend == ':' && namend[1] == ':')
554 || (*namend == '\'' && namend[1]))
558 if (!stash || !SvREFCNT(stash)) /* symbol table under destruction */
566 if (len + 3 < sizeof smallbuf)
569 New(601, tmpbuf, len+3, char);
570 Copy(name, tmpbuf, len, char);
574 gvp = (GV**)hv_fetch(stash,tmpbuf,len,add);
575 gv = gvp ? *gvp : Nullgv;
576 if (gv && gv != (GV*)&PL_sv_undef) {
577 if (SvTYPE(gv) != SVt_PVGV)
578 gv_init(gv, stash, tmpbuf, len, (add & GV_ADDMULTI));
582 if (tmpbuf != smallbuf)
584 if (!gv || gv == (GV*)&PL_sv_undef)
587 if (!(stash = GvHV(gv)))
588 stash = GvHV(gv) = newHV();
591 HvNAME(stash) = savepvn(nambeg, namend - nambeg);
599 return gv ? gv : (GV*)*hv_fetch(PL_defstash, "main::", 6, TRUE);
606 /* No stash in name, so see how we can default */
609 if (isIDFIRST_lazy(name)) {
612 if (isUPPER(*name)) {
613 if (*name == 'S' && (
614 strEQ(name, "SIG") ||
615 strEQ(name, "STDIN") ||
616 strEQ(name, "STDOUT") ||
617 strEQ(name, "STDERR")))
619 else if (*name == 'I' && strEQ(name, "INC"))
621 else if (*name == 'E' && strEQ(name, "ENV"))
623 else if (*name == 'A' && (
624 strEQ(name, "ARGV") ||
625 strEQ(name, "ARGVOUT")))
628 else if (*name == '_' && !name[1])
633 else if ((COP*)PL_curcop == &PL_compiling) {
635 if (add && (PL_hints & HINT_STRICT_VARS) &&
636 sv_type != SVt_PVCV &&
637 sv_type != SVt_PVGV &&
638 sv_type != SVt_PVFM &&
639 sv_type != SVt_PVIO &&
640 !(len == 1 && sv_type == SVt_PV && strchr("ab",*name)) )
642 gvp = (GV**)hv_fetch(stash,name,len,0);
644 *gvp == (GV*)&PL_sv_undef ||
645 SvTYPE(*gvp) != SVt_PVGV)
649 else if ((sv_type == SVt_PV && !GvIMPORTED_SV(*gvp)) ||
650 (sv_type == SVt_PVAV && !GvIMPORTED_AV(*gvp)) ||
651 (sv_type == SVt_PVHV && !GvIMPORTED_HV(*gvp)) )
653 Perl_warn(aTHX_ "Variable \"%c%s\" is not imported",
654 sv_type == SVt_PVAV ? '@' :
655 sv_type == SVt_PVHV ? '%' : '$',
658 Perl_warn(aTHX_ "\t(Did you mean &%s instead?)\n", name);
664 stash = CopSTASH(PL_curcop);
670 /* By this point we should have a stash and a name */
674 qerror(Perl_mess(aTHX_
675 "Global symbol \"%s%s\" requires explicit package name",
676 (sv_type == SVt_PV ? "$"
677 : sv_type == SVt_PVAV ? "@"
678 : sv_type == SVt_PVHV ? "%"
680 stash = PL_nullstash;
686 if (!SvREFCNT(stash)) /* symbol table under destruction */
689 gvp = (GV**)hv_fetch(stash,name,len,add);
690 if (!gvp || *gvp == (GV*)&PL_sv_undef)
693 if (SvTYPE(gv) == SVt_PVGV) {
696 gv_init_sv(gv, sv_type);
699 } else if (add & GV_NOINIT) {
703 /* Adding a new symbol */
705 if (add & GV_ADDWARN && ckWARN_d(WARN_INTERNAL))
706 Perl_warner(aTHX_ WARN_INTERNAL, "Had to create %s unexpectedly", nambeg);
707 gv_init(gv, stash, name, len, add & GV_ADDMULTI);
708 gv_init_sv(gv, sv_type);
710 if (isLEXWARN_on && isALPHA(name[0]) && ! ckWARN(WARN_ONCE))
713 /* set up magic where warranted */
716 if (strEQ(name, "ARGV")) {
717 IoFLAGS(GvIOn(gv)) |= IOf_ARGV|IOf_START;
721 if (strnEQ(name, "EXPORT", 6))
725 if (strEQ(name, "ISA")) {
728 sv_magic((SV*)av, (SV*)gv, 'I', Nullch, 0);
729 /* NOTE: No support for tied ISA */
730 if ((add & GV_ADDMULTI) && strEQ(nambeg,"AnyDBM_File::ISA")
731 && AvFILLp(av) == -1)
734 av_push(av, newSVpvn(pname = "NDBM_File",9));
735 gv_stashpvn(pname, 9, TRUE);
736 av_push(av, newSVpvn(pname = "DB_File",7));
737 gv_stashpvn(pname, 7, TRUE);
738 av_push(av, newSVpvn(pname = "GDBM_File",9));
739 gv_stashpvn(pname, 9, TRUE);
740 av_push(av, newSVpvn(pname = "SDBM_File",9));
741 gv_stashpvn(pname, 9, TRUE);
742 av_push(av, newSVpvn(pname = "ODBM_File",9));
743 gv_stashpvn(pname, 9, TRUE);
748 if (strEQ(name, "OVERLOAD")) {
751 hv_magic(hv, Nullgv, 'A');
755 if (strEQ(name, "SIG")) {
759 Newz(73, PL_psig_ptr, SIG_SIZE, SV*);
760 Newz(73, PL_psig_name, SIG_SIZE, SV*);
761 Newz(73, PL_psig_pend, SIG_SIZE, int);
765 hv_magic(hv, Nullgv, 'S');
766 for (i = 1; i < SIG_SIZE; i++) {
768 init = hv_fetch(hv, PL_sig_name[i], strlen(PL_sig_name[i]), 1);
770 sv_setsv(*init, &PL_sv_undef);
778 if (strEQ(name, "VERSION"))
785 PL_sawampersand = TRUE;
791 PL_sawampersand = TRUE;
797 PL_sawampersand = TRUE;
803 sv_setpv(GvSV(gv),PL_chopset);
809 #ifdef COMPLEX_STATUS
810 (void)SvUPGRADE(GvSV(gv), SVt_PVLV);
817 if (sv_type > SVt_PV && PL_curcop != &PL_compiling) {
818 HV* stash = gv_stashpvn("Errno",5,FALSE);
819 if (!stash || !(gv_fetchmethod(stash, "TIEHASH"))) {
822 require_pv("Errno.pm");
824 stash = gv_stashpvn("Errno",5,FALSE);
825 if (!stash || !(gv_fetchmethod(stash, "TIEHASH")))
826 Perl_croak(aTHX_ "Can't use %%! because Errno.pm is not available");
835 sv_magic((SV*)av, Nullsv, 'D', Nullch, 0);
841 if (ckWARN(WARN_DEPRECATED) && len == 1 && sv_type == SVt_PV)
842 Perl_warner(aTHX_ WARN_DEPRECATED, "Use of $%s is deprecated", name);
857 case '\001': /* $^A */
858 case '\003': /* $^C */
859 case '\004': /* $^D */
860 case '\005': /* $^E */
861 case '\006': /* $^F */
862 case '\010': /* $^H */
863 case '\011': /* $^I, NOT \t in EBCDIC */
864 case '\020': /* $^P */
865 case '\024': /* $^T */
872 sv_setiv(GvSV(gv), (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0);
874 case '\017': /* $^O & $^OPEN */
875 if (len > 1 && strNE(name, "\017PEN"))
878 case '\023': /* $^S */
882 case '\027': /* $^W & $^WARNING_BITS */
883 if (len > 1 && strNE(name, "\027ARNING_BITS")
884 && strNE(name, "\027IDE_SYSTEM_CALLS"))
893 sv_magic((SV*)av, (SV*)av, 'D', Nullch, 0);
907 SvREADONLY_on(GvSV(gv));
909 sv_magic(GvSV(gv), (SV*)gv, 0, name, len);
912 case '\014': /* $^L */
915 sv_setpv(GvSV(gv),"\f");
916 PL_formfeed = GvSV(gv);
921 sv_setpv(GvSV(gv),"\034");
926 (void)SvUPGRADE(sv, SVt_PVNV);
927 Perl_sv_setpvf(aTHX_ sv,
928 #if defined(PERL_SUBVERSION) && (PERL_SUBVERSION > 0)
934 SvNVX(PL_patchlevel));
935 SvNVX(sv) = SvNVX(PL_patchlevel);
940 case '\026': /* $^V */
943 GvSV(gv) = SvREFCNT_inc(PL_patchlevel);
952 Perl_gv_fullname4(pTHX_ SV *sv, GV *gv, const char *prefix, bool keepmain)
954 HV *hv = GvSTASH(gv);
959 sv_setpv(sv, prefix ? prefix : "");
960 if (keepmain || strNE(HvNAME(hv), "main")) {
961 sv_catpv(sv,HvNAME(hv));
962 sv_catpvn(sv,"::", 2);
964 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
968 Perl_gv_fullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
970 HV *hv = GvSTASH(gv);
975 sv_setpv(sv, prefix ? prefix : "");
976 sv_catpv(sv,HvNAME(hv));
977 sv_catpvn(sv,"::", 2);
978 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
982 Perl_gv_efullname4(pTHX_ SV *sv, GV *gv, const char *prefix, bool keepmain)
987 gv_fullname4(sv, egv, prefix, keepmain);
991 Perl_gv_efullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
996 gv_fullname3(sv, egv, prefix);
999 /* XXX compatibility with versions <= 5.003. */
1001 Perl_gv_fullname(pTHX_ SV *sv, GV *gv)
1003 gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : "");
1006 /* XXX compatibility with versions <= 5.003. */
1008 Perl_gv_efullname(pTHX_ SV *sv, GV *gv)
1010 gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : "");
1019 io = (IO*)NEWSV(0,0);
1020 sv_upgrade((SV *)io,SVt_PVIO);
1023 iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV);
1024 /* unless exists($main::{FileHandle}) and defined(%main::FileHandle::) */
1025 if (!(iogv && GvHV(iogv) && HvARRAY(GvHV(iogv))))
1026 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
1027 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
1032 Perl_gv_check(pTHX_ HV *stash)
1039 if (!HvARRAY(stash))
1041 for (i = 0; i <= (I32) HvMAX(stash); i++) {
1042 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
1043 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
1044 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)) && HvNAME(hv))
1046 if (hv != PL_defstash && hv != stash)
1047 gv_check(hv); /* nested package */
1049 else if (isALPHA(*HeKEY(entry))) {
1051 gv = (GV*)HeVAL(entry);
1052 if (SvTYPE(gv) != SVt_PVGV || GvMULTI(gv))
1055 /* performance hack: if filename is absolute and it's a standard
1056 * module, don't bother warning */
1058 && PERL_FILE_IS_ABSOLUTE(file)
1059 && (instr(file, "/lib/") || instr(file, ".pm")))
1063 CopLINE_set(PL_curcop, GvLINE(gv));
1065 CopFILE(PL_curcop) = file; /* set for warning */
1067 CopFILEGV(PL_curcop) = gv_fetchfile(file);
1069 Perl_warner(aTHX_ WARN_ONCE,
1070 "Name \"%s::%s\" used only once: possible typo",
1071 HvNAME(stash), GvNAME(gv));
1078 Perl_newGVgen(pTHX_ char *pack)
1080 return gv_fetchpv(Perl_form(aTHX_ "%s::_GEN_%ld", pack, (long)PL_gensym++),
1084 /* hopefully this is only called on local symbol table entries */
1087 Perl_gp_ref(pTHX_ GP *gp)
1094 /* multi-named GPs cannot be used for method cache */
1095 SvREFCNT_dec(gp->gp_cv);
1100 /* Adding a new name to a subroutine invalidates method cache */
1101 PL_sub_generation++;
1108 Perl_gp_free(pTHX_ GV *gv)
1112 if (!gv || !(gp = GvGP(gv)))
1114 if (gp->gp_refcnt == 0) {
1115 if (ckWARN_d(WARN_INTERNAL))
1116 Perl_warner(aTHX_ WARN_INTERNAL,
1117 "Attempt to free unreferenced glob pointers");
1121 /* Deleting the name of a subroutine invalidates method cache */
1122 PL_sub_generation++;
1124 if (--gp->gp_refcnt > 0) {
1125 if (gp->gp_egv == gv)
1130 SvREFCNT_dec(gp->gp_sv);
1131 SvREFCNT_dec(gp->gp_av);
1132 SvREFCNT_dec(gp->gp_hv);
1133 SvREFCNT_dec(gp->gp_io);
1134 SvREFCNT_dec(gp->gp_cv);
1135 SvREFCNT_dec(gp->gp_form);
1141 #if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
1145 #ifdef MICROPORT /* Microport 2.4 hack */
1149 if (GvGP(gv)->gp_av)
1150 return GvGP(gv)->gp_av;
1152 return GvGP(gv_AVadd(gv))->gp_av;
1158 if (GvGP(gv)->gp_hv)
1159 return GvGP(gv)->gp_hv;
1161 return GvGP(gv_HVadd(gv))->gp_hv;
1163 #endif /* Microport 2.4 hack */
1166 Perl_magic_freeovrld(pTHX_ SV *sv, MAGIC *mg)
1168 AMT *amtp = (AMT*)mg->mg_ptr;
1169 if (amtp && AMT_AMAGIC(amtp)) {
1171 for (i = 1; i < NofAMmeth; i++) {
1172 CV *cv = amtp->table[i];
1174 SvREFCNT_dec((SV *) cv);
1175 amtp->table[i] = Nullcv;
1182 /* Updates and caches the CV's */
1185 Perl_Gv_AMupdate(pTHX_ HV *stash)
1189 MAGIC* mg=mg_find((SV*)stash,'c');
1190 AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL;
1194 if (mg && amtp->was_ok_am == PL_amagic_generation
1195 && amtp->was_ok_sub == PL_sub_generation)
1196 return AMT_OVERLOADED(amtp);
1197 sv_unmagic((SV*)stash, 'c');
1199 DEBUG_o( Perl_deb(aTHX_ "Recalcing overload magic in package %s\n",HvNAME(stash)) );
1202 amt.was_ok_am = PL_amagic_generation;
1203 amt.was_ok_sub = PL_sub_generation;
1204 amt.fallback = AMGfallNO;
1208 int filled = 0, have_ovl = 0;
1212 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
1214 /* Try to find via inheritance. */
1215 gv = gv_fetchmeth(stash, PL_AMG_names[0], 2, -1);
1220 lim = DESTROY_amg; /* Skip overloading entries. */
1221 else if (SvTRUE(sv))
1222 amt.fallback=AMGfallYES;
1224 amt.fallback=AMGfallNEVER;
1226 for (i = 1; i < lim; i++)
1227 amt.table[i] = Nullcv;
1228 for (; i < NofAMmeth; i++) {
1229 char *cooky = (char*)PL_AMG_names[i];
1230 /* Human-readable form, for debugging: */
1231 char *cp = (i >= DESTROY_amg ? cooky : AMG_id2name(i));
1232 STRLEN l = strlen(cooky);
1234 DEBUG_o( Perl_deb(aTHX_ "Checking overloading of `%s' in package `%.256s'\n",
1235 cp, HvNAME(stash)) );
1236 /* don't fill the cache while looking up! */
1237 gv = gv_fetchmeth(stash, cooky, l, -1);
1239 if (gv && (cv = GvCV(gv))) {
1240 if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil")
1241 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) {
1242 /* GvSV contains the name of the method. */
1245 DEBUG_o( Perl_deb(aTHX_ "Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n",
1246 SvPV(GvSV(gv), n_a), cp, HvNAME(stash)) );
1247 if (!SvPOK(GvSV(gv))
1248 || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)),
1251 /* Can be an import stub (created by `can'). */
1253 Perl_croak(aTHX_ "Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'",
1254 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1257 Perl_croak(aTHX_ "Can't resolve method `%.256s' overloading `%s' in package `%.256s'",
1258 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1261 cv = GvCV(gv = ngv);
1263 DEBUG_o( Perl_deb(aTHX_ "Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n",
1264 cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))),
1265 GvNAME(CvGV(cv))) );
1267 if (i < DESTROY_amg)
1270 amt.table[i]=(CV*)SvREFCNT_inc(cv);
1273 AMT_AMAGIC_on(&amt);
1275 AMT_OVERLOADED_on(&amt);
1276 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMT));
1280 /* Here we have no table: */
1282 AMT_AMAGIC_off(&amt);
1283 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMTS));
1289 Perl_gv_handler(pTHX_ HV *stash, I32 id)
1297 mg = mg_find((SV*)stash,'c');
1301 mg = mg_find((SV*)stash,'c');
1303 amtp = (AMT*)mg->mg_ptr;
1304 if ( amtp->was_ok_am != PL_amagic_generation
1305 || amtp->was_ok_sub != PL_sub_generation )
1307 if (AMT_AMAGIC(amtp))
1308 return amtp->table[id];
1314 Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags)
1318 CV **cvp=NULL, **ocvp=NULL;
1320 int fl=0, off, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
1321 int postpr = 0, force_cpy = 0, assignshift = assign ? 1 : 0;
1323 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
1324 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),'c'))
1325 && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1326 ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table
1328 && ((cv = cvp[off=method+assignshift])
1329 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
1331 (fl = 1, cv = cvp[off=method])))) {
1332 lr = -1; /* Call method for left argument */
1334 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
1337 /* look for substituted methods */
1338 /* In all the covered cases we should be called with assign==0. */
1342 if ((cv = cvp[off=add_ass_amg])
1343 || ((cv = cvp[off = add_amg]) && (force_cpy = 0, postpr = 1))) {
1344 right = &PL_sv_yes; lr = -1; assign = 1;
1349 if ((cv = cvp[off = subtr_ass_amg])
1350 || ((cv = cvp[off = subtr_amg]) && (force_cpy = 0, postpr=1))) {
1351 right = &PL_sv_yes; lr = -1; assign = 1;
1355 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
1358 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1361 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1364 (void)((cv = cvp[off=bool__amg])
1365 || (cv = cvp[off=numer_amg])
1366 || (cv = cvp[off=string_amg]));
1372 * SV* ref causes confusion with the interpreter variable of
1375 SV* tmpRef=SvRV(left);
1376 if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) {
1378 * Just to be extra cautious. Maybe in some
1379 * additional cases sv_setsv is safe, too.
1381 SV* newref = newSVsv(tmpRef);
1382 SvOBJECT_on(newref);
1383 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(tmpRef));
1389 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
1390 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
1391 SV* nullsv=sv_2mortal(newSViv(0));
1393 SV* lessp = amagic_call(left,nullsv,
1394 lt_amg,AMGf_noright);
1395 logic = SvTRUE(lessp);
1397 SV* lessp = amagic_call(left,nullsv,
1398 ncmp_amg,AMGf_noright);
1399 logic = (SvNV(lessp) < 0);
1402 if (off==subtr_amg) {
1413 if ((cv = cvp[off=subtr_amg])) {
1415 left = sv_2mortal(newSViv(0));
1420 case iter_amg: /* XXXX Eventually should do to_gv. */
1422 return NULL; /* Delegate operation to standard mechanisms. */
1430 return left; /* Delegate operation to standard mechanisms. */
1435 if (!cv) goto not_found;
1436 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1437 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c'))
1438 && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1439 ? (amtp = (AMT*)mg->mg_ptr)->table
1441 && (cv = cvp[off=method])) { /* Method for right
1444 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
1445 && (cvp=ocvp) && (lr = -1))
1446 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1447 && !(flags & AMGf_unary)) {
1448 /* We look for substitution for
1449 * comparison operations and
1451 if (method==concat_amg || method==concat_ass_amg
1452 || method==repeat_amg || method==repeat_ass_amg) {
1453 return NULL; /* Delegate operation to string conversion */
1463 postpr = 1; off=ncmp_amg; break;
1470 postpr = 1; off=scmp_amg; break;
1472 if (off != -1) cv = cvp[off];
1477 not_found: /* No method found, either report or croak */
1485 return left; /* Delegate operation to standard mechanisms. */
1488 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1489 notfound = 1; lr = -1;
1490 } else if (cvp && (cv=cvp[nomethod_amg])) {
1491 notfound = 1; lr = 1;
1494 if (off==-1) off=method;
1495 msg = sv_2mortal(Perl_newSVpvf(aTHX_
1496 "Operation `%s': no method found,%sargument %s%s%s%s",
1497 AMG_id2name(method + assignshift),
1498 (flags & AMGf_unary ? " " : "\n\tleft "),
1500 "in overloaded package ":
1501 "has no overloaded magic",
1503 HvNAME(SvSTASH(SvRV(left))):
1506 ",\n\tright argument in overloaded package ":
1509 : ",\n\tright argument has no overloaded magic"),
1511 HvNAME(SvSTASH(SvRV(right))):
1513 if (amtp && amtp->fallback >= AMGfallYES) {
1514 DEBUG_o( Perl_deb(aTHX_ "%s", SvPVX(msg)) );
1516 Perl_croak(aTHX_ "%"SVf, msg);
1520 force_cpy = force_cpy || assign;
1524 DEBUG_o( Perl_deb(aTHX_
1525 "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n",
1527 method+assignshift==off? "" :
1529 method+assignshift==off? "" :
1530 AMG_id2name(method+assignshift),
1531 method+assignshift==off? "" : "')",
1532 flags & AMGf_unary? "" :
1533 lr==1 ? " for right argument": " for left argument",
1534 flags & AMGf_unary? " for argument" : "",
1536 fl? ",\n\tassignment variant used": "") );
1538 /* Since we use shallow copy during assignment, we need
1539 * to dublicate the contents, probably calling user-supplied
1540 * version of copy operator
1542 /* We need to copy in following cases:
1543 * a) Assignment form was called.
1544 * assignshift==1, assign==T, method + 1 == off
1545 * b) Increment or decrement, called directly.
1546 * assignshift==0, assign==0, method + 0 == off
1547 * c) Increment or decrement, translated to assignment add/subtr.
1548 * assignshift==0, assign==T,
1550 * d) Increment or decrement, translated to nomethod.
1551 * assignshift==0, assign==0,
1553 * e) Assignment form translated to nomethod.
1554 * assignshift==1, assign==T, method + 1 != off
1557 /* off is method, method+assignshift, or a result of opcode substitution.
1558 * In the latter case assignshift==0, so only notfound case is important.
1560 if (( (method + assignshift == off)
1561 && (assign || (method == inc_amg) || (method == dec_amg)))
1568 bool oldcatch = CATCH_GET;
1571 Zero(&myop, 1, BINOP);
1572 myop.op_last = (OP *) &myop;
1573 myop.op_next = Nullop;
1574 myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED;
1576 PUSHSTACKi(PERLSI_OVERLOAD);
1579 PL_op = (OP *) &myop;
1580 if (PERLDB_SUB && PL_curstash != PL_debstash)
1581 PL_op->op_private |= OPpENTERSUB_DB;
1585 EXTEND(SP, notfound + 5);
1586 PUSHs(lr>0? right: left);
1587 PUSHs(lr>0? left: right);
1588 PUSHs( lr > 0 ? &PL_sv_yes : ( assign ? &PL_sv_undef : &PL_sv_no ));
1590 PUSHs( sv_2mortal(newSVpv(AMG_id2name(method + assignshift),0)));
1595 if ((PL_op = Perl_pp_entersub(aTHX)))
1603 CATCH_SET(oldcatch);
1610 ans=SvIV(res)<=0; break;
1613 ans=SvIV(res)<0; break;
1616 ans=SvIV(res)>=0; break;
1619 ans=SvIV(res)>0; break;
1622 ans=SvIV(res)==0; break;
1625 ans=SvIV(res)!=0; break;
1628 SvSetSV(left,res); return left;
1630 ans=!SvTRUE(res); break;
1633 } else if (method==copy_amg) {
1635 Perl_croak(aTHX_ "Copy method did not return a reference");
1637 return SvREFCNT_inc(SvRV(res));
1645 =for apidoc is_gv_magical
1647 Returns C<TRUE> if given the name of a magical GV.
1649 Currently only useful internally when determining if a GV should be
1650 created even in rvalue contexts.
1652 C<flags> is not used at present but available for future extension to
1653 allow selecting particular classes of magical variable.
1658 Perl_is_gv_magical(pTHX_ char *name, STRLEN len, U32 flags)
1665 if (len == 3 && strEQ(name, "ISA"))
1669 if (len == 8 && strEQ(name, "OVERLOAD"))
1673 if (len == 3 && strEQ(name, "SIG"))
1676 case '\017': /* $^O & $^OPEN */
1678 || (len == 4 && strEQ(name, "\027PEN")))
1683 case '\027': /* $^W & $^WARNING_BITS */
1685 || (len == 12 && strEQ(name, "\027ARNING_BITS"))
1686 || (len == 17 && strEQ(name, "\027IDE_SYSTEM_CALLS")))
1718 case '\001': /* $^A */
1719 case '\003': /* $^C */
1720 case '\004': /* $^D */
1721 case '\005': /* $^E */
1722 case '\006': /* $^F */
1723 case '\010': /* $^H */
1724 case '\011': /* $^I, NOT \t in EBCDIC */
1725 case '\014': /* $^L */
1726 case '\020': /* $^P */
1727 case '\023': /* $^S */
1728 case '\024': /* $^T */
1729 case '\026': /* $^V */
1743 char *end = name + len;
1744 while (--end > name) {