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, PERL_MAGIC_dbfile);
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, PERL_MAGIC_glob, 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;
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)
191 /* UNIVERSAL methods should be callable without a stash */
193 level = -1; /* probably appropriate */
194 if(!(stash = gv_stashpvn("UNIVERSAL", 9, FALSE)))
199 Perl_croak(aTHX_ "Can't use anonymous symbol table for method lookup");
200 if ((level > 100) || (level < -100))
201 Perl_croak(aTHX_ "Recursive inheritance detected while looking for method '%s' in package '%s'",
202 name, HvNAME(stash));
204 DEBUG_o( Perl_deb(aTHX_ "Looking for method %s in package %s\n",name,HvNAME(stash)) );
206 gvp = (GV**)hv_fetch(stash, name, len, (level >= 0));
211 if (SvTYPE(topgv) != SVt_PVGV)
212 gv_init(topgv, stash, name, len, TRUE);
213 if ((cv = GvCV(topgv))) {
214 /* If genuine method or valid cache entry, use it */
215 if (!GvCVGEN(topgv) || GvCVGEN(topgv) == PL_sub_generation)
217 /* Stale cached entry: junk it */
219 GvCV(topgv) = cv = Nullcv;
222 else if (GvCVGEN(topgv) == PL_sub_generation)
223 return 0; /* cache indicates sub doesn't exist */
226 gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE);
227 av = (gvp && (gv = *gvp) && gv != (GV*)&PL_sv_undef) ? GvAV(gv) : Nullav;
229 /* create and re-create @.*::SUPER::ISA on demand */
230 if (!av || !SvMAGIC(av)) {
231 char* packname = HvNAME(stash);
232 STRLEN packlen = strlen(packname);
234 if (packlen >= 7 && strEQ(packname + packlen - 7, "::SUPER")) {
238 basestash = gv_stashpvn(packname, packlen, TRUE);
239 gvp = (GV**)hv_fetch(basestash, "ISA", 3, FALSE);
240 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
241 gvp = (GV**)hv_fetch(stash, "ISA", 3, TRUE);
242 if (!gvp || !(gv = *gvp))
243 Perl_croak(aTHX_ "Cannot create %s::ISA", HvNAME(stash));
244 if (SvTYPE(gv) != SVt_PVGV)
245 gv_init(gv, stash, "ISA", 3, TRUE);
246 SvREFCNT_dec(GvAV(gv));
247 GvAV(gv) = (AV*)SvREFCNT_inc(av);
253 SV** svp = AvARRAY(av);
254 /* NOTE: No support for tied ISA */
255 I32 items = AvFILLp(av) + 1;
258 HV* basestash = gv_stashsv(sv, FALSE);
260 if (ckWARN(WARN_MISC))
261 Perl_warner(aTHX_ WARN_MISC, "Can't locate package %s for @%s::ISA",
262 SvPVX(sv), HvNAME(stash));
265 gv = gv_fetchmeth(basestash, name, len,
266 (level >= 0) ? level + 1 : level - 1);
272 /* if at top level, try UNIVERSAL */
274 if (level == 0 || level == -1) {
277 if ((lastchance = gv_stashpvn("UNIVERSAL", 9, FALSE))) {
278 if ((gv = gv_fetchmeth(lastchance, name, len,
279 (level >= 0) ? level + 1 : level - 1)))
283 * Cache method in topgv if:
284 * 1. topgv has no synonyms (else inheritance crosses wires)
285 * 2. method isn't a stub (else AUTOLOAD fails spectacularly)
288 GvREFCNT(topgv) == 1 &&
290 (CvROOT(cv) || CvXSUB(cv)))
292 if ((cv = GvCV(topgv)))
294 GvCV(topgv) = (CV*)SvREFCNT_inc(GvCV(gv));
295 GvCVGEN(topgv) = PL_sub_generation;
299 else if (topgv && GvREFCNT(topgv) == 1) {
300 /* cache the fact that the method is not defined */
301 GvCVGEN(topgv) = PL_sub_generation;
310 =for apidoc gv_fetchmethod
312 See L<gv_fetchmethod_autoload>.
318 Perl_gv_fetchmethod(pTHX_ HV *stash, const char *name)
320 return gv_fetchmethod_autoload(stash, name, TRUE);
324 =for apidoc gv_fetchmethod_autoload
326 Returns the glob which contains the subroutine to call to invoke the method
327 on the C<stash>. In fact in the presence of autoloading this may be the
328 glob for "AUTOLOAD". In this case the corresponding variable $AUTOLOAD is
331 The third parameter of C<gv_fetchmethod_autoload> determines whether
332 AUTOLOAD lookup is performed if the given method is not present: non-zero
333 means yes, look for AUTOLOAD; zero means no, don't look for AUTOLOAD.
334 Calling C<gv_fetchmethod> is equivalent to calling C<gv_fetchmethod_autoload>
335 with a non-zero C<autoload> parameter.
337 These functions grant C<"SUPER"> token as a prefix of the method name. Note
338 that if you want to keep the returned glob for a long time, you need to
339 check for it being "AUTOLOAD", since at the later time the call may load a
340 different subroutine due to $AUTOLOAD changing its value. Use the glob
341 created via a side effect to do this.
343 These functions have the same side-effects and as C<gv_fetchmeth> with
344 C<level==0>. C<name> should be writable if contains C<':'> or C<'
345 ''>. The warning against passing the GV returned by C<gv_fetchmeth> to
346 C<call_sv> apply equally to these functions.
352 Perl_gv_fetchmethod_autoload(pTHX_ HV *stash, const char *name, I32 autoload)
354 register const char *nend;
355 const char *nsplit = 0;
358 for (nend = name; *nend; nend++) {
361 else if (*nend == ':' && *(nend + 1) == ':')
365 const char *origname = name;
369 if ((nsplit - origname) == 5 && strnEQ(origname, "SUPER", 5)) {
370 /* ->SUPER::method should really be looked up in original stash */
371 SV *tmpstr = sv_2mortal(Perl_newSVpvf(aTHX_ "%s::SUPER",
372 CopSTASHPV(PL_curcop)));
373 /* __PACKAGE__::SUPER stash should be autovivified */
374 stash = gv_stashpvn(SvPVX(tmpstr), SvCUR(tmpstr), TRUE);
375 DEBUG_o( Perl_deb(aTHX_ "Treating %s as %s::%s\n",
376 origname, HvNAME(stash), name) );
379 /* don't autovifify if ->NoSuchStash::method */
380 stash = gv_stashpvn(origname, nsplit - origname, FALSE);
383 gv = gv_fetchmeth(stash, name, nend - name, 0);
385 if (strEQ(name,"import") || strEQ(name,"unimport"))
386 gv = (GV*)&PL_sv_yes;
388 gv = gv_autoload4(stash, name, nend - name, TRUE);
392 if (!CvROOT(cv) && !CvXSUB(cv)) {
400 if (GvCV(stubgv) != cv) /* orphaned import */
403 autogv = gv_autoload4(GvSTASH(stubgv),
404 GvNAME(stubgv), GvNAMELEN(stubgv), TRUE);
414 Perl_gv_autoload4(pTHX_ HV *stash, const char *name, STRLEN len, I32 method)
416 static char autoload[] = "AUTOLOAD";
417 static STRLEN autolen = 8;
425 return Nullgv; /* UNIVERSAL::AUTOLOAD could cause trouble */
426 if (len == autolen && strnEQ(name, autoload, autolen))
428 if (!(gv = gv_fetchmeth(stash, autoload, autolen, FALSE)))
432 if (!(CvROOT(cv) || CvXSUB(cv)))
436 * Inheriting AUTOLOAD for non-methods works ... for now.
438 if (ckWARN(WARN_DEPRECATED) && !method &&
439 (GvCVGEN(gv) || GvSTASH(gv) != stash))
440 Perl_warner(aTHX_ WARN_DEPRECATED,
441 "Use of inherited AUTOLOAD for non-method %s::%.*s() is deprecated",
442 HvNAME(stash), (int)len, name);
446 /* rather than lookup/init $AUTOLOAD here
447 * only to have the XSUB do another lookup for $AUTOLOAD
448 * and split that value on the last '::',
449 * pass along the same data via some unused fields in the CV
452 SvPVX(cv) = (char *)name; /* cast to loose constness warning */
459 * Given &FOO::AUTOLOAD, set $FOO::AUTOLOAD to desired function name.
460 * The subroutine's original name may not be "AUTOLOAD", so we don't
461 * use that, but for lack of anything better we will use the sub's
462 * original package to look up $AUTOLOAD.
464 varstash = GvSTASH(CvGV(cv));
465 vargv = *(GV**)hv_fetch(varstash, autoload, autolen, TRUE);
469 sv_lock((SV *)varstash);
472 gv_init(vargv, varstash, autoload, autolen, FALSE);
478 sv_setpv(varsv, HvNAME(stash));
479 sv_catpvn(varsv, "::", 2);
480 sv_catpvn(varsv, name, len);
481 SvTAINTED_off(varsv);
485 /* The "gv" parameter should be the glob known to Perl code as *!
486 * The scalar must already have been magicalized.
489 S_require_errno(pTHX_ GV *gv)
491 HV* stash = gv_stashpvn("Errno",5,FALSE);
493 if (!stash || !(gv_fetchmethod(stash, "TIEHASH"))) {
497 save_scalar(gv); /* keep the value of $! */
498 require_pv("Errno.pm");
501 stash = gv_stashpvn("Errno",5,FALSE);
502 if (!stash || !(gv_fetchmethod(stash, "TIEHASH")))
503 Perl_croak(aTHX_ "Can't use %%! because Errno.pm is not available");
508 =for apidoc gv_stashpv
510 Returns a pointer to the stash for a specified package. C<name> should
511 be a valid UTF-8 string. If C<create> is set then the package will be
512 created if it does not already exist. If C<create> is not set and the
513 package does not exist then NULL is returned.
519 Perl_gv_stashpv(pTHX_ const char *name, I32 create)
521 return gv_stashpvn(name, strlen(name), create);
525 Perl_gv_stashpvn(pTHX_ const char *name, U32 namelen, I32 create)
532 if (namelen + 3 < sizeof smallbuf)
535 New(606, tmpbuf, namelen + 3, char);
536 Copy(name,tmpbuf,namelen,char);
537 tmpbuf[namelen++] = ':';
538 tmpbuf[namelen++] = ':';
539 tmpbuf[namelen] = '\0';
540 tmpgv = gv_fetchpv(tmpbuf, create, SVt_PVHV);
541 if (tmpbuf != smallbuf)
546 GvHV(tmpgv) = newHV();
549 HvNAME(stash) = savepv(name);
554 =for apidoc gv_stashsv
556 Returns a pointer to the stash for a specified package, which must be a
557 valid UTF-8 string. See C<gv_stashpv>.
563 Perl_gv_stashsv(pTHX_ SV *sv, I32 create)
568 return gv_stashpvn(ptr, len, create);
573 Perl_gv_fetchpv(pTHX_ const char *nambeg, I32 add, I32 sv_type)
575 register const char *name = nambeg;
579 register const char *namend;
582 if (*name == '*' && isALPHA(name[1])) /* accidental stringify on a GV? */
585 for (namend = name; *namend; namend++) {
586 if ((*namend == ':' && namend[1] == ':')
587 || (*namend == '\'' && namend[1]))
591 if (!stash || !SvREFCNT(stash)) /* symbol table under destruction */
599 if (len + 3 < sizeof smallbuf)
602 New(601, tmpbuf, len+3, char);
603 Copy(name, tmpbuf, len, char);
607 gvp = (GV**)hv_fetch(stash,tmpbuf,len,add);
608 gv = gvp ? *gvp : Nullgv;
609 if (gv && gv != (GV*)&PL_sv_undef) {
610 if (SvTYPE(gv) != SVt_PVGV)
611 gv_init(gv, stash, tmpbuf, len, (add & GV_ADDMULTI));
615 if (tmpbuf != smallbuf)
617 if (!gv || gv == (GV*)&PL_sv_undef)
620 if (!(stash = GvHV(gv)))
621 stash = GvHV(gv) = newHV();
624 HvNAME(stash) = savepvn(nambeg, namend - nambeg);
632 return gv ? gv : (GV*)*hv_fetch(PL_defstash, "main::", 6, TRUE);
639 /* No stash in name, so see how we can default */
642 if (isIDFIRST_lazy(name)) {
645 if (isUPPER(*name)) {
646 if (*name == 'S' && (
647 strEQ(name, "SIG") ||
648 strEQ(name, "STDIN") ||
649 strEQ(name, "STDOUT") ||
650 strEQ(name, "STDERR")))
652 else if (*name == 'I' && strEQ(name, "INC"))
654 else if (*name == 'E' && strEQ(name, "ENV"))
656 else if (*name == 'A' && (
657 strEQ(name, "ARGV") ||
658 strEQ(name, "ARGVOUT")))
661 else if (*name == '_' && !name[1])
666 else if ((COP*)PL_curcop == &PL_compiling) {
668 if (add && (PL_hints & HINT_STRICT_VARS) &&
669 sv_type != SVt_PVCV &&
670 sv_type != SVt_PVGV &&
671 sv_type != SVt_PVFM &&
672 sv_type != SVt_PVIO &&
673 !(len == 1 && sv_type == SVt_PV && strchr("ab",*name)) )
675 gvp = (GV**)hv_fetch(stash,name,len,0);
677 *gvp == (GV*)&PL_sv_undef ||
678 SvTYPE(*gvp) != SVt_PVGV)
682 else if ((sv_type == SVt_PV && !GvIMPORTED_SV(*gvp)) ||
683 (sv_type == SVt_PVAV && !GvIMPORTED_AV(*gvp)) ||
684 (sv_type == SVt_PVHV && !GvIMPORTED_HV(*gvp)) )
686 Perl_warn(aTHX_ "Variable \"%c%s\" is not imported",
687 sv_type == SVt_PVAV ? '@' :
688 sv_type == SVt_PVHV ? '%' : '$',
691 Perl_warn(aTHX_ "\t(Did you mean &%s instead?)\n", name);
697 stash = CopSTASH(PL_curcop);
703 /* By this point we should have a stash and a name */
707 qerror(Perl_mess(aTHX_
708 "Global symbol \"%s%s\" requires explicit package name",
709 (sv_type == SVt_PV ? "$"
710 : sv_type == SVt_PVAV ? "@"
711 : sv_type == SVt_PVHV ? "%"
713 stash = PL_nullstash;
719 if (!SvREFCNT(stash)) /* symbol table under destruction */
722 gvp = (GV**)hv_fetch(stash,name,len,add);
723 if (!gvp || *gvp == (GV*)&PL_sv_undef)
726 if (SvTYPE(gv) == SVt_PVGV) {
729 gv_init_sv(gv, sv_type);
730 if (*name=='!' && sv_type == SVt_PVHV && len==1)
734 } else if (add & GV_NOINIT) {
738 /* Adding a new symbol */
740 if (add & GV_ADDWARN && ckWARN_d(WARN_INTERNAL))
741 Perl_warner(aTHX_ WARN_INTERNAL, "Had to create %s unexpectedly", nambeg);
742 gv_init(gv, stash, name, len, add & GV_ADDMULTI);
743 gv_init_sv(gv, sv_type);
745 if (isALPHA(name[0]) && ! (isLEXWARN_on ? ckWARN(WARN_ONCE)
746 : (PL_dowarn & G_WARN_ON ) ) )
749 /* set up magic where warranted */
752 if (strEQ(name, "ARGV")) {
753 IoFLAGS(GvIOn(gv)) |= IOf_ARGV|IOf_START;
757 if (strnEQ(name, "EXPORT", 6))
761 if (strEQ(name, "ISA")) {
764 sv_magic((SV*)av, (SV*)gv, PERL_MAGIC_isa, Nullch, 0);
765 /* NOTE: No support for tied ISA */
766 if ((add & GV_ADDMULTI) && strEQ(nambeg,"AnyDBM_File::ISA")
767 && AvFILLp(av) == -1)
770 av_push(av, newSVpvn(pname = "NDBM_File",9));
771 gv_stashpvn(pname, 9, TRUE);
772 av_push(av, newSVpvn(pname = "DB_File",7));
773 gv_stashpvn(pname, 7, TRUE);
774 av_push(av, newSVpvn(pname = "GDBM_File",9));
775 gv_stashpvn(pname, 9, TRUE);
776 av_push(av, newSVpvn(pname = "SDBM_File",9));
777 gv_stashpvn(pname, 9, TRUE);
778 av_push(av, newSVpvn(pname = "ODBM_File",9));
779 gv_stashpvn(pname, 9, TRUE);
784 if (strEQ(name, "OVERLOAD")) {
787 hv_magic(hv, Nullgv, PERL_MAGIC_overload);
791 if (strEQ(name, "SIG")) {
795 Newz(73, PL_psig_ptr, SIG_SIZE, SV*);
796 Newz(73, PL_psig_name, SIG_SIZE, SV*);
797 Newz(73, PL_psig_pend, SIG_SIZE, int);
801 hv_magic(hv, Nullgv, PERL_MAGIC_sig);
802 for (i = 1; i < SIG_SIZE; i++) {
804 init = hv_fetch(hv, PL_sig_name[i], strlen(PL_sig_name[i]), 1);
806 sv_setsv(*init, &PL_sv_undef);
814 if (strEQ(name, "VERSION"))
821 PL_sawampersand = TRUE;
827 PL_sawampersand = TRUE;
833 PL_sawampersand = TRUE;
839 sv_setpv(GvSV(gv),PL_chopset);
845 #ifdef COMPLEX_STATUS
846 (void)SvUPGRADE(GvSV(gv), SVt_PVLV);
854 /* If %! has been used, automatically load Errno.pm.
855 The require will itself set errno, so in order to
856 preserve its value we have to set up the magic
857 now (rather than going to magicalize)
860 sv_magic(GvSV(gv), (SV*)gv, PERL_MAGIC_sv, name, len);
862 if (sv_type == SVt_PVHV)
871 sv_magic((SV*)av, Nullsv, PERL_MAGIC_regdata, Nullch, 0);
877 if (ckWARN(WARN_DEPRECATED) && len == 1 && sv_type == SVt_PV)
878 Perl_warner(aTHX_ WARN_DEPRECATED, "Use of $%s is deprecated", name);
893 case '\001': /* $^A */
894 case '\003': /* $^C */
895 case '\004': /* $^D */
896 case '\005': /* $^E */
897 case '\006': /* $^F */
898 case '\010': /* $^H */
899 case '\011': /* $^I, NOT \t in EBCDIC */
900 case '\020': /* $^P */
901 case '\024': /* $^T */
908 sv_setiv(GvSV(gv), (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0);
910 case '\017': /* $^O & $^OPEN */
911 if (len > 1 && strNE(name, "\017PEN"))
914 case '\023': /* $^S */
918 case '\027': /* $^W & $^WARNING_BITS */
919 if (len > 1 && strNE(name, "\027ARNING_BITS")
920 && strNE(name, "\027IDE_SYSTEM_CALLS"))
929 sv_magic((SV*)av, (SV*)av, PERL_MAGIC_regdata, Nullch, 0);
943 SvREADONLY_on(GvSV(gv));
945 sv_magic(GvSV(gv), (SV*)gv, PERL_MAGIC_sv, name, len);
948 case '\014': /* $^L */
951 sv_setpv(GvSV(gv),"\f");
952 PL_formfeed = GvSV(gv);
957 sv_setpv(GvSV(gv),"\034");
962 (void)SvUPGRADE(sv, SVt_PVNV);
963 Perl_sv_setpvf(aTHX_ sv,
964 #if defined(PERL_SUBVERSION) && (PERL_SUBVERSION > 0)
970 SvNVX(PL_patchlevel));
971 SvNVX(sv) = SvNVX(PL_patchlevel);
976 case '\026': /* $^V */
979 GvSV(gv) = SvREFCNT_inc(PL_patchlevel);
988 Perl_gv_fullname4(pTHX_ SV *sv, GV *gv, const char *prefix, bool keepmain)
990 HV *hv = GvSTASH(gv);
995 sv_setpv(sv, prefix ? prefix : "");
996 if (keepmain || strNE(HvNAME(hv), "main")) {
997 sv_catpv(sv,HvNAME(hv));
998 sv_catpvn(sv,"::", 2);
1000 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
1004 Perl_gv_fullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
1006 HV *hv = GvSTASH(gv);
1011 sv_setpv(sv, prefix ? prefix : "");
1012 sv_catpv(sv,HvNAME(hv));
1013 sv_catpvn(sv,"::", 2);
1014 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
1018 Perl_gv_efullname4(pTHX_ SV *sv, GV *gv, const char *prefix, bool keepmain)
1020 GV *egv = GvEGV(gv);
1023 gv_fullname4(sv, egv, prefix, keepmain);
1027 Perl_gv_efullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
1029 GV *egv = GvEGV(gv);
1032 gv_fullname3(sv, egv, prefix);
1035 /* XXX compatibility with versions <= 5.003. */
1037 Perl_gv_fullname(pTHX_ SV *sv, GV *gv)
1039 gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : "");
1042 /* XXX compatibility with versions <= 5.003. */
1044 Perl_gv_efullname(pTHX_ SV *sv, GV *gv)
1046 gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : "");
1055 io = (IO*)NEWSV(0,0);
1056 sv_upgrade((SV *)io,SVt_PVIO);
1059 iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV);
1060 /* unless exists($main::{FileHandle}) and defined(%main::FileHandle::) */
1061 if (!(iogv && GvHV(iogv) && HvARRAY(GvHV(iogv))))
1062 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
1063 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
1068 Perl_gv_check(pTHX_ HV *stash)
1075 if (!HvARRAY(stash))
1077 for (i = 0; i <= (I32) HvMAX(stash); i++) {
1078 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
1079 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
1080 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)) && HvNAME(hv))
1082 if (hv != PL_defstash && hv != stash)
1083 gv_check(hv); /* nested package */
1085 else if (isALPHA(*HeKEY(entry))) {
1087 gv = (GV*)HeVAL(entry);
1088 if (SvTYPE(gv) != SVt_PVGV || GvMULTI(gv))
1091 /* performance hack: if filename is absolute and it's a standard
1092 * module, don't bother warning */
1094 && PERL_FILE_IS_ABSOLUTE(file)
1095 #ifdef MACOS_TRADITIONAL
1096 && (instr(file, ":lib:")
1098 && (instr(file, "/lib/")
1100 || instr(file, ".pm")))
1104 CopLINE_set(PL_curcop, GvLINE(gv));
1106 CopFILE(PL_curcop) = file; /* set for warning */
1108 CopFILEGV(PL_curcop) = gv_fetchfile(file);
1110 Perl_warner(aTHX_ WARN_ONCE,
1111 "Name \"%s::%s\" used only once: possible typo",
1112 HvNAME(stash), GvNAME(gv));
1119 Perl_newGVgen(pTHX_ char *pack)
1121 return gv_fetchpv(Perl_form(aTHX_ "%s::_GEN_%ld", pack, (long)PL_gensym++),
1125 /* hopefully this is only called on local symbol table entries */
1128 Perl_gp_ref(pTHX_ GP *gp)
1135 /* multi-named GPs cannot be used for method cache */
1136 SvREFCNT_dec(gp->gp_cv);
1141 /* Adding a new name to a subroutine invalidates method cache */
1142 PL_sub_generation++;
1149 Perl_gp_free(pTHX_ GV *gv)
1153 if (!gv || !(gp = GvGP(gv)))
1155 if (gp->gp_refcnt == 0) {
1156 if (ckWARN_d(WARN_INTERNAL))
1157 Perl_warner(aTHX_ WARN_INTERNAL,
1158 "Attempt to free unreferenced glob pointers");
1162 /* Deleting the name of a subroutine invalidates method cache */
1163 PL_sub_generation++;
1165 if (--gp->gp_refcnt > 0) {
1166 if (gp->gp_egv == gv)
1171 SvREFCNT_dec(gp->gp_sv);
1172 SvREFCNT_dec(gp->gp_av);
1173 SvREFCNT_dec(gp->gp_hv);
1174 SvREFCNT_dec(gp->gp_io);
1175 SvREFCNT_dec(gp->gp_cv);
1176 SvREFCNT_dec(gp->gp_form);
1182 #if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
1186 #ifdef MICROPORT /* Microport 2.4 hack */
1190 if (GvGP(gv)->gp_av)
1191 return GvGP(gv)->gp_av;
1193 return GvGP(gv_AVadd(gv))->gp_av;
1199 if (GvGP(gv)->gp_hv)
1200 return GvGP(gv)->gp_hv;
1202 return GvGP(gv_HVadd(gv))->gp_hv;
1204 #endif /* Microport 2.4 hack */
1207 Perl_magic_freeovrld(pTHX_ SV *sv, MAGIC *mg)
1209 AMT *amtp = (AMT*)mg->mg_ptr;
1210 if (amtp && AMT_AMAGIC(amtp)) {
1212 for (i = 1; i < NofAMmeth; i++) {
1213 CV *cv = amtp->table[i];
1215 SvREFCNT_dec((SV *) cv);
1216 amtp->table[i] = Nullcv;
1223 /* Updates and caches the CV's */
1226 Perl_Gv_AMupdate(pTHX_ HV *stash)
1230 MAGIC* mg=mg_find((SV*)stash, PERL_MAGIC_overload_table);
1231 AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL;
1234 if (mg && amtp->was_ok_am == PL_amagic_generation
1235 && amtp->was_ok_sub == PL_sub_generation)
1236 return AMT_OVERLOADED(amtp);
1237 sv_unmagic((SV*)stash, PERL_MAGIC_overload_table);
1239 DEBUG_o( Perl_deb(aTHX_ "Recalcing overload magic in package %s\n",HvNAME(stash)) );
1242 amt.was_ok_am = PL_amagic_generation;
1243 amt.was_ok_sub = PL_sub_generation;
1244 amt.fallback = AMGfallNO;
1248 int filled = 0, have_ovl = 0;
1252 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
1254 /* Try to find via inheritance. */
1255 gv = gv_fetchmeth(stash, PL_AMG_names[0], 2, -1);
1260 lim = DESTROY_amg; /* Skip overloading entries. */
1261 else if (SvTRUE(sv))
1262 amt.fallback=AMGfallYES;
1264 amt.fallback=AMGfallNEVER;
1266 for (i = 1; i < lim; i++)
1267 amt.table[i] = Nullcv;
1268 for (; i < NofAMmeth; i++) {
1269 char *cooky = (char*)PL_AMG_names[i];
1270 /* Human-readable form, for debugging: */
1271 char *cp = (i >= DESTROY_amg ? cooky : AMG_id2name(i));
1272 STRLEN l = strlen(cooky);
1274 DEBUG_o( Perl_deb(aTHX_ "Checking overloading of `%s' in package `%.256s'\n",
1275 cp, HvNAME(stash)) );
1276 /* don't fill the cache while looking up! */
1277 gv = gv_fetchmeth(stash, cooky, l, -1);
1279 if (gv && (cv = GvCV(gv))) {
1280 if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil")
1281 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) {
1282 /* GvSV contains the name of the method. */
1285 DEBUG_o( Perl_deb(aTHX_ "Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n",
1286 SvPV_nolen(GvSV(gv)), cp, HvNAME(stash)) );
1287 if (!SvPOK(GvSV(gv))
1288 || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)),
1291 /* Can be an import stub (created by `can'). */
1293 Perl_croak(aTHX_ "Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'",
1294 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1297 Perl_croak(aTHX_ "Can't resolve method `%.256s' overloading `%s' in package `%.256s'",
1298 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1301 cv = GvCV(gv = ngv);
1303 DEBUG_o( Perl_deb(aTHX_ "Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n",
1304 cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))),
1305 GvNAME(CvGV(cv))) );
1307 if (i < DESTROY_amg)
1310 amt.table[i]=(CV*)SvREFCNT_inc(cv);
1313 AMT_AMAGIC_on(&amt);
1315 AMT_OVERLOADED_on(&amt);
1316 sv_magic((SV*)stash, 0, PERL_MAGIC_overload_table,
1317 (char*)&amt, sizeof(AMT));
1321 /* Here we have no table: */
1323 AMT_AMAGIC_off(&amt);
1324 sv_magic((SV*)stash, 0, PERL_MAGIC_overload_table,
1325 (char*)&amt, sizeof(AMTS));
1331 Perl_gv_handler(pTHX_ HV *stash, I32 id)
1338 mg = mg_find((SV*)stash, PERL_MAGIC_overload_table);
1342 mg = mg_find((SV*)stash, PERL_MAGIC_overload_table);
1344 amtp = (AMT*)mg->mg_ptr;
1345 if ( amtp->was_ok_am != PL_amagic_generation
1346 || amtp->was_ok_sub != PL_sub_generation )
1348 if (AMT_AMAGIC(amtp))
1349 return amtp->table[id];
1355 Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags)
1359 CV **cvp=NULL, **ocvp=NULL;
1360 AMT *amtp=NULL, *oamtp=NULL;
1361 int fl=0, off=0, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
1362 int postpr = 0, force_cpy = 0, assignshift = assign ? 1 : 0;
1364 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
1365 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),
1366 PERL_MAGIC_overload_table))
1367 && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1368 ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table
1370 && ((cv = cvp[off=method+assignshift])
1371 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
1373 (fl = 1, cv = cvp[off=method])))) {
1374 lr = -1; /* Call method for left argument */
1376 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
1379 /* look for substituted methods */
1380 /* In all the covered cases we should be called with assign==0. */
1384 if ((cv = cvp[off=add_ass_amg])
1385 || ((cv = cvp[off = add_amg]) && (force_cpy = 0, postpr = 1))) {
1386 right = &PL_sv_yes; lr = -1; assign = 1;
1391 if ((cv = cvp[off = subtr_ass_amg])
1392 || ((cv = cvp[off = subtr_amg]) && (force_cpy = 0, postpr=1))) {
1393 right = &PL_sv_yes; lr = -1; assign = 1;
1397 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
1400 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1403 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1406 (void)((cv = cvp[off=bool__amg])
1407 || (cv = cvp[off=numer_amg])
1408 || (cv = cvp[off=string_amg]));
1414 * SV* ref causes confusion with the interpreter variable of
1417 SV* tmpRef=SvRV(left);
1418 if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) {
1420 * Just to be extra cautious. Maybe in some
1421 * additional cases sv_setsv is safe, too.
1423 SV* newref = newSVsv(tmpRef);
1424 SvOBJECT_on(newref);
1425 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(tmpRef));
1431 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
1432 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
1433 SV* nullsv=sv_2mortal(newSViv(0));
1435 SV* lessp = amagic_call(left,nullsv,
1436 lt_amg,AMGf_noright);
1437 logic = SvTRUE(lessp);
1439 SV* lessp = amagic_call(left,nullsv,
1440 ncmp_amg,AMGf_noright);
1441 logic = (SvNV(lessp) < 0);
1444 if (off==subtr_amg) {
1455 if ((cv = cvp[off=subtr_amg])) {
1457 left = sv_2mortal(newSViv(0));
1462 case iter_amg: /* XXXX Eventually should do to_gv. */
1464 return NULL; /* Delegate operation to standard mechanisms. */
1472 return left; /* Delegate operation to standard mechanisms. */
1477 if (!cv) goto not_found;
1478 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1479 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),
1480 PERL_MAGIC_overload_table))
1481 && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1482 ? (amtp = (AMT*)mg->mg_ptr)->table
1484 && (cv = cvp[off=method])) { /* Method for right
1487 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
1488 && (cvp=ocvp) && (lr = -1))
1489 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1490 && !(flags & AMGf_unary)) {
1491 /* We look for substitution for
1492 * comparison operations and
1494 if (method==concat_amg || method==concat_ass_amg
1495 || method==repeat_amg || method==repeat_ass_amg) {
1496 return NULL; /* Delegate operation to string conversion */
1506 postpr = 1; off=ncmp_amg; break;
1513 postpr = 1; off=scmp_amg; break;
1515 if (off != -1) cv = cvp[off];
1520 not_found: /* No method found, either report or croak */
1528 return left; /* Delegate operation to standard mechanisms. */
1531 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1532 notfound = 1; lr = -1;
1533 } else if (cvp && (cv=cvp[nomethod_amg])) {
1534 notfound = 1; lr = 1;
1537 if (off==-1) off=method;
1538 msg = sv_2mortal(Perl_newSVpvf(aTHX_
1539 "Operation `%s': no method found,%sargument %s%s%s%s",
1540 AMG_id2name(method + assignshift),
1541 (flags & AMGf_unary ? " " : "\n\tleft "),
1543 "in overloaded package ":
1544 "has no overloaded magic",
1546 HvNAME(SvSTASH(SvRV(left))):
1549 ",\n\tright argument in overloaded package ":
1552 : ",\n\tright argument has no overloaded magic"),
1554 HvNAME(SvSTASH(SvRV(right))):
1556 if (amtp && amtp->fallback >= AMGfallYES) {
1557 DEBUG_o( Perl_deb(aTHX_ "%s", SvPVX(msg)) );
1559 Perl_croak(aTHX_ "%"SVf, msg);
1563 force_cpy = force_cpy || assign;
1567 DEBUG_o( Perl_deb(aTHX_
1568 "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n",
1570 method+assignshift==off? "" :
1572 method+assignshift==off? "" :
1573 AMG_id2name(method+assignshift),
1574 method+assignshift==off? "" : "')",
1575 flags & AMGf_unary? "" :
1576 lr==1 ? " for right argument": " for left argument",
1577 flags & AMGf_unary? " for argument" : "",
1579 fl? ",\n\tassignment variant used": "") );
1581 /* Since we use shallow copy during assignment, we need
1582 * to dublicate the contents, probably calling user-supplied
1583 * version of copy operator
1585 /* We need to copy in following cases:
1586 * a) Assignment form was called.
1587 * assignshift==1, assign==T, method + 1 == off
1588 * b) Increment or decrement, called directly.
1589 * assignshift==0, assign==0, method + 0 == off
1590 * c) Increment or decrement, translated to assignment add/subtr.
1591 * assignshift==0, assign==T,
1593 * d) Increment or decrement, translated to nomethod.
1594 * assignshift==0, assign==0,
1596 * e) Assignment form translated to nomethod.
1597 * assignshift==1, assign==T, method + 1 != off
1600 /* off is method, method+assignshift, or a result of opcode substitution.
1601 * In the latter case assignshift==0, so only notfound case is important.
1603 if (( (method + assignshift == off)
1604 && (assign || (method == inc_amg) || (method == dec_amg)))
1611 bool oldcatch = CATCH_GET;
1614 Zero(&myop, 1, BINOP);
1615 myop.op_last = (OP *) &myop;
1616 myop.op_next = Nullop;
1617 myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED;
1619 PUSHSTACKi(PERLSI_OVERLOAD);
1622 PL_op = (OP *) &myop;
1623 if (PERLDB_SUB && PL_curstash != PL_debstash)
1624 PL_op->op_private |= OPpENTERSUB_DB;
1628 EXTEND(SP, notfound + 5);
1629 PUSHs(lr>0? right: left);
1630 PUSHs(lr>0? left: right);
1631 PUSHs( lr > 0 ? &PL_sv_yes : ( assign ? &PL_sv_undef : &PL_sv_no ));
1633 PUSHs( sv_2mortal(newSVpv(AMG_id2name(method + assignshift),0)));
1638 if ((PL_op = Perl_pp_entersub(aTHX)))
1646 CATCH_SET(oldcatch);
1653 ans=SvIV(res)<=0; break;
1656 ans=SvIV(res)<0; break;
1659 ans=SvIV(res)>=0; break;
1662 ans=SvIV(res)>0; break;
1665 ans=SvIV(res)==0; break;
1668 ans=SvIV(res)!=0; break;
1671 SvSetSV(left,res); return left;
1673 ans=!SvTRUE(res); break;
1676 } else if (method==copy_amg) {
1678 Perl_croak(aTHX_ "Copy method did not return a reference");
1680 return SvREFCNT_inc(SvRV(res));
1688 =for apidoc is_gv_magical
1690 Returns C<TRUE> if given the name of a magical GV.
1692 Currently only useful internally when determining if a GV should be
1693 created even in rvalue contexts.
1695 C<flags> is not used at present but available for future extension to
1696 allow selecting particular classes of magical variable.
1701 Perl_is_gv_magical(pTHX_ char *name, STRLEN len, U32 flags)
1708 if (len == 3 && strEQ(name, "ISA"))
1712 if (len == 8 && strEQ(name, "OVERLOAD"))
1716 if (len == 3 && strEQ(name, "SIG"))
1719 case '\017': /* $^O & $^OPEN */
1721 || (len == 4 && strEQ(name, "\027PEN")))
1726 case '\027': /* $^W & $^WARNING_BITS */
1728 || (len == 12 && strEQ(name, "\027ARNING_BITS"))
1729 || (len == 17 && strEQ(name, "\027IDE_SYSTEM_CALLS")))
1761 case '\001': /* $^A */
1762 case '\003': /* $^C */
1763 case '\004': /* $^D */
1764 case '\005': /* $^E */
1765 case '\006': /* $^F */
1766 case '\010': /* $^H */
1767 case '\011': /* $^I, NOT \t in EBCDIC */
1768 case '\014': /* $^L */
1769 case '\020': /* $^P */
1770 case '\023': /* $^S */
1771 case '\024': /* $^T */
1772 case '\026': /* $^V */
1786 char *end = name + len;
1787 while (--end > name) {