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_set_from_cop(GvCV(gv), 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)))
198 if ((level > 100) || (level < -100))
199 Perl_croak(aTHX_ "Recursive inheritance detected while looking for method '%s' in package '%s'",
200 name, HvNAME(stash));
202 DEBUG_o( Perl_deb(aTHX_ "Looking for method %s in package %s\n",name,HvNAME(stash)) );
204 gvp = (GV**)hv_fetch(stash, name, len, (level >= 0));
209 if (SvTYPE(topgv) != SVt_PVGV)
210 gv_init(topgv, stash, name, len, TRUE);
211 if ((cv = GvCV(topgv))) {
212 /* If genuine method or valid cache entry, use it */
213 if (!GvCVGEN(topgv) || GvCVGEN(topgv) == PL_sub_generation)
215 /* Stale cached entry: junk it */
217 GvCV(topgv) = cv = Nullcv;
220 else if (GvCVGEN(topgv) == PL_sub_generation)
221 return 0; /* cache indicates sub doesn't exist */
224 gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE);
225 av = (gvp && (gv = *gvp) && gv != (GV*)&PL_sv_undef) ? GvAV(gv) : Nullav;
227 /* create and re-create @.*::SUPER::ISA on demand */
228 if (!av || !SvMAGIC(av)) {
229 char* packname = HvNAME(stash);
230 STRLEN packlen = strlen(packname);
232 if (packlen >= 7 && strEQ(packname + packlen - 7, "::SUPER")) {
236 basestash = gv_stashpvn(packname, packlen, TRUE);
237 gvp = (GV**)hv_fetch(basestash, "ISA", 3, FALSE);
238 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
239 gvp = (GV**)hv_fetch(stash, "ISA", 3, TRUE);
240 if (!gvp || !(gv = *gvp))
241 Perl_croak(aTHX_ "Cannot create %s::ISA", HvNAME(stash));
242 if (SvTYPE(gv) != SVt_PVGV)
243 gv_init(gv, stash, "ISA", 3, TRUE);
244 SvREFCNT_dec(GvAV(gv));
245 GvAV(gv) = (AV*)SvREFCNT_inc(av);
251 SV** svp = AvARRAY(av);
252 /* NOTE: No support for tied ISA */
253 I32 items = AvFILLp(av) + 1;
256 HV* basestash = gv_stashsv(sv, FALSE);
258 if (ckWARN(WARN_MISC))
259 Perl_warner(aTHX_ WARN_MISC, "Can't locate package %s for @%s::ISA",
260 SvPVX(sv), HvNAME(stash));
263 gv = gv_fetchmeth(basestash, name, len,
264 (level >= 0) ? level + 1 : level - 1);
270 /* if at top level, try UNIVERSAL */
272 if (level == 0 || level == -1) {
275 if ((lastchance = gv_stashpvn("UNIVERSAL", 9, FALSE))) {
276 if ((gv = gv_fetchmeth(lastchance, name, len,
277 (level >= 0) ? level + 1 : level - 1)))
281 * Cache method in topgv if:
282 * 1. topgv has no synonyms (else inheritance crosses wires)
283 * 2. method isn't a stub (else AUTOLOAD fails spectacularly)
286 GvREFCNT(topgv) == 1 &&
288 (CvROOT(cv) || CvXSUB(cv)))
290 if ((cv = GvCV(topgv)))
292 GvCV(topgv) = (CV*)SvREFCNT_inc(GvCV(gv));
293 GvCVGEN(topgv) = PL_sub_generation;
297 else if (topgv && GvREFCNT(topgv) == 1) {
298 /* cache the fact that the method is not defined */
299 GvCVGEN(topgv) = PL_sub_generation;
308 =for apidoc gv_fetchmethod
310 See L<gv_fetchmethod_autoload>.
316 Perl_gv_fetchmethod(pTHX_ HV *stash, const char *name)
318 return gv_fetchmethod_autoload(stash, name, TRUE);
322 =for apidoc gv_fetchmethod_autoload
324 Returns the glob which contains the subroutine to call to invoke the method
325 on the C<stash>. In fact in the presence of autoloading this may be the
326 glob for "AUTOLOAD". In this case the corresponding variable $AUTOLOAD is
329 The third parameter of C<gv_fetchmethod_autoload> determines whether
330 AUTOLOAD lookup is performed if the given method is not present: non-zero
331 means yes, look for AUTOLOAD; zero means no, don't look for AUTOLOAD.
332 Calling C<gv_fetchmethod> is equivalent to calling C<gv_fetchmethod_autoload>
333 with a non-zero C<autoload> parameter.
335 These functions grant C<"SUPER"> token as a prefix of the method name. Note
336 that if you want to keep the returned glob for a long time, you need to
337 check for it being "AUTOLOAD", since at the later time the call may load a
338 different subroutine due to $AUTOLOAD changing its value. Use the glob
339 created via a side effect to do this.
341 These functions have the same side-effects and as C<gv_fetchmeth> with
342 C<level==0>. C<name> should be writable if contains C<':'> or C<'
343 ''>. The warning against passing the GV returned by C<gv_fetchmeth> to
344 C<call_sv> apply equally to these functions.
350 Perl_gv_fetchmethod_autoload(pTHX_ HV *stash, const char *name, I32 autoload)
352 register const char *nend;
353 const char *nsplit = 0;
356 for (nend = name; *nend; nend++) {
359 else if (*nend == ':' && *(nend + 1) == ':')
363 const char *origname = name;
367 if ((nsplit - origname) == 5 && strnEQ(origname, "SUPER", 5)) {
368 /* ->SUPER::method should really be looked up in original stash */
369 SV *tmpstr = sv_2mortal(Perl_newSVpvf(aTHX_ "%s::SUPER",
370 CopSTASHPV(PL_curcop)));
371 /* __PACKAGE__::SUPER stash should be autovivified */
372 stash = gv_stashpvn(SvPVX(tmpstr), SvCUR(tmpstr), TRUE);
373 DEBUG_o( Perl_deb(aTHX_ "Treating %s as %s::%s\n",
374 origname, HvNAME(stash), name) );
377 /* don't autovifify if ->NoSuchStash::method */
378 stash = gv_stashpvn(origname, nsplit - origname, FALSE);
381 gv = gv_fetchmeth(stash, name, nend - name, 0);
383 if (strEQ(name,"import") || strEQ(name,"unimport"))
384 gv = (GV*)&PL_sv_yes;
386 gv = gv_autoload4(stash, name, nend - name, TRUE);
390 if (!CvROOT(cv) && !CvXSUB(cv)) {
398 if (GvCV(stubgv) != cv) /* orphaned import */
401 autogv = gv_autoload4(GvSTASH(stubgv),
402 GvNAME(stubgv), GvNAMELEN(stubgv), TRUE);
412 Perl_gv_autoload4(pTHX_ HV *stash, const char *name, STRLEN len, I32 method)
414 static char autoload[] = "AUTOLOAD";
415 static STRLEN autolen = 8;
423 return Nullgv; /* UNIVERSAL::AUTOLOAD could cause trouble */
424 if (len == autolen && strnEQ(name, autoload, autolen))
426 if (!(gv = gv_fetchmeth(stash, autoload, autolen, FALSE)))
430 if (!(CvROOT(cv) || CvXSUB(cv)))
434 * Inheriting AUTOLOAD for non-methods works ... for now.
436 if (ckWARN(WARN_DEPRECATED) && !method &&
437 (GvCVGEN(gv) || GvSTASH(gv) != stash))
438 Perl_warner(aTHX_ WARN_DEPRECATED,
439 "Use of inherited AUTOLOAD for non-method %s::%.*s() is deprecated",
440 HvNAME(stash), (int)len, name);
444 /* rather than lookup/init $AUTOLOAD here
445 * only to have the XSUB do another lookup for $AUTOLOAD
446 * and split that value on the last '::',
447 * pass along the same data via some unused fields in the CV
450 SvPVX(cv) = (char *)name; /* cast to lose constness warning */
457 * Given &FOO::AUTOLOAD, set $FOO::AUTOLOAD to desired function name.
458 * The subroutine's original name may not be "AUTOLOAD", so we don't
459 * use that, but for lack of anything better we will use the sub's
460 * original package to look up $AUTOLOAD.
462 varstash = GvSTASH(CvGV(cv));
463 vargv = *(GV**)hv_fetch(varstash, autoload, autolen, TRUE);
467 sv_lock((SV *)varstash);
470 gv_init(vargv, varstash, autoload, autolen, FALSE);
476 sv_setpv(varsv, HvNAME(stash));
477 sv_catpvn(varsv, "::", 2);
478 sv_catpvn(varsv, name, len);
479 SvTAINTED_off(varsv);
483 /* The "gv" parameter should be the glob known to Perl code as *!
484 * The scalar must already have been magicalized.
487 S_require_errno(pTHX_ GV *gv)
489 HV* stash = gv_stashpvn("Errno",5,FALSE);
491 if (!stash || !(gv_fetchmethod(stash, "TIEHASH"))) {
495 save_scalar(gv); /* keep the value of $! */
496 require_pv("Errno.pm");
499 stash = gv_stashpvn("Errno",5,FALSE);
500 if (!stash || !(gv_fetchmethod(stash, "TIEHASH")))
501 Perl_croak(aTHX_ "Can't use %%! because Errno.pm is not available");
506 =for apidoc gv_stashpv
508 Returns a pointer to the stash for a specified package. C<name> should
509 be a valid UTF-8 string. If C<create> is set then the package will be
510 created if it does not already exist. If C<create> is not set and the
511 package does not exist then NULL is returned.
517 Perl_gv_stashpv(pTHX_ const char *name, I32 create)
519 return gv_stashpvn(name, strlen(name), create);
523 Perl_gv_stashpvn(pTHX_ const char *name, U32 namelen, I32 create)
530 if (namelen + 3 < sizeof smallbuf)
533 New(606, tmpbuf, namelen + 3, char);
534 Copy(name,tmpbuf,namelen,char);
535 tmpbuf[namelen++] = ':';
536 tmpbuf[namelen++] = ':';
537 tmpbuf[namelen] = '\0';
538 tmpgv = gv_fetchpv(tmpbuf, create, SVt_PVHV);
539 if (tmpbuf != smallbuf)
544 GvHV(tmpgv) = newHV();
547 HvNAME(stash) = savepv(name);
552 =for apidoc gv_stashsv
554 Returns a pointer to the stash for a specified package, which must be a
555 valid UTF-8 string. See C<gv_stashpv>.
561 Perl_gv_stashsv(pTHX_ SV *sv, I32 create)
566 return gv_stashpvn(ptr, len, create);
571 Perl_gv_fetchpv(pTHX_ const char *nambeg, I32 add, I32 sv_type)
573 register const char *name = nambeg;
577 register const char *namend;
580 if (*name == '*' && isALPHA(name[1])) /* accidental stringify on a GV? */
583 for (namend = name; *namend; namend++) {
584 if ((*namend == ':' && namend[1] == ':')
585 || (*namend == '\'' && namend[1]))
589 if (!stash || !SvREFCNT(stash)) /* symbol table under destruction */
597 if (len + 3 < sizeof smallbuf)
600 New(601, tmpbuf, len+3, char);
601 Copy(name, tmpbuf, len, char);
605 gvp = (GV**)hv_fetch(stash,tmpbuf,len,add);
606 gv = gvp ? *gvp : Nullgv;
607 if (gv && gv != (GV*)&PL_sv_undef) {
608 if (SvTYPE(gv) != SVt_PVGV)
609 gv_init(gv, stash, tmpbuf, len, (add & GV_ADDMULTI));
613 if (tmpbuf != smallbuf)
615 if (!gv || gv == (GV*)&PL_sv_undef)
618 if (!(stash = GvHV(gv)))
619 stash = GvHV(gv) = newHV();
622 HvNAME(stash) = savepvn(nambeg, namend - nambeg);
630 return gv ? gv : (GV*)*hv_fetch(PL_defstash, "main::", 6, TRUE);
637 /* No stash in name, so see how we can default */
640 if (isIDFIRST_lazy(name)) {
643 if (isUPPER(*name)) {
644 if (*name == 'S' && (
645 strEQ(name, "SIG") ||
646 strEQ(name, "STDIN") ||
647 strEQ(name, "STDOUT") ||
648 strEQ(name, "STDERR")))
650 else if (*name == 'I' && strEQ(name, "INC"))
652 else if (*name == 'E' && strEQ(name, "ENV"))
654 else if (*name == 'A' && (
655 strEQ(name, "ARGV") ||
656 strEQ(name, "ARGVOUT")))
659 else if (*name == '_' && !name[1])
664 else if ((COP*)PL_curcop == &PL_compiling) {
666 if (add && (PL_hints & HINT_STRICT_VARS) &&
667 sv_type != SVt_PVCV &&
668 sv_type != SVt_PVGV &&
669 sv_type != SVt_PVFM &&
670 sv_type != SVt_PVIO &&
671 !(len == 1 && sv_type == SVt_PV && strchr("ab",*name)) )
673 gvp = (GV**)hv_fetch(stash,name,len,0);
675 *gvp == (GV*)&PL_sv_undef ||
676 SvTYPE(*gvp) != SVt_PVGV)
680 else if ((sv_type == SVt_PV && !GvIMPORTED_SV(*gvp)) ||
681 (sv_type == SVt_PVAV && !GvIMPORTED_AV(*gvp)) ||
682 (sv_type == SVt_PVHV && !GvIMPORTED_HV(*gvp)) )
684 Perl_warn(aTHX_ "Variable \"%c%s\" is not imported",
685 sv_type == SVt_PVAV ? '@' :
686 sv_type == SVt_PVHV ? '%' : '$',
689 Perl_warn(aTHX_ "\t(Did you mean &%s instead?)\n", name);
695 stash = CopSTASH(PL_curcop);
701 /* By this point we should have a stash and a name */
705 qerror(Perl_mess(aTHX_
706 "Global symbol \"%s%s\" requires explicit package name",
707 (sv_type == SVt_PV ? "$"
708 : sv_type == SVt_PVAV ? "@"
709 : sv_type == SVt_PVHV ? "%"
711 stash = PL_nullstash;
717 if (!SvREFCNT(stash)) /* symbol table under destruction */
720 gvp = (GV**)hv_fetch(stash,name,len,add);
721 if (!gvp || *gvp == (GV*)&PL_sv_undef)
724 if (SvTYPE(gv) == SVt_PVGV) {
727 gv_init_sv(gv, sv_type);
728 if (*name=='!' && sv_type == SVt_PVHV && len==1)
732 } else if (add & GV_NOINIT) {
736 /* Adding a new symbol */
738 if (add & GV_ADDWARN && ckWARN_d(WARN_INTERNAL))
739 Perl_warner(aTHX_ WARN_INTERNAL, "Had to create %s unexpectedly", nambeg);
740 gv_init(gv, stash, name, len, add & GV_ADDMULTI);
741 gv_init_sv(gv, sv_type);
743 if (isALPHA(name[0]) && ! (isLEXWARN_on ? ckWARN(WARN_ONCE)
744 : (PL_dowarn & G_WARN_ON ) ) )
747 /* set up magic where warranted */
750 if (strEQ(name, "ARGV")) {
751 IoFLAGS(GvIOn(gv)) |= IOf_ARGV|IOf_START;
755 if (strnEQ(name, "EXPORT", 6))
759 if (strEQ(name, "ISA")) {
762 sv_magic((SV*)av, (SV*)gv, PERL_MAGIC_isa, Nullch, 0);
763 /* NOTE: No support for tied ISA */
764 if ((add & GV_ADDMULTI) && strEQ(nambeg,"AnyDBM_File::ISA")
765 && AvFILLp(av) == -1)
768 av_push(av, newSVpvn(pname = "NDBM_File",9));
769 gv_stashpvn(pname, 9, TRUE);
770 av_push(av, newSVpvn(pname = "DB_File",7));
771 gv_stashpvn(pname, 7, TRUE);
772 av_push(av, newSVpvn(pname = "GDBM_File",9));
773 gv_stashpvn(pname, 9, TRUE);
774 av_push(av, newSVpvn(pname = "SDBM_File",9));
775 gv_stashpvn(pname, 9, TRUE);
776 av_push(av, newSVpvn(pname = "ODBM_File",9));
777 gv_stashpvn(pname, 9, TRUE);
782 if (strEQ(name, "OVERLOAD")) {
785 hv_magic(hv, Nullgv, PERL_MAGIC_overload);
789 if (strEQ(name, "SIG")) {
793 Newz(73, PL_psig_ptr, SIG_SIZE, SV*);
794 Newz(73, PL_psig_name, SIG_SIZE, SV*);
795 Newz(73, PL_psig_pend, SIG_SIZE, int);
799 hv_magic(hv, Nullgv, PERL_MAGIC_sig);
800 for (i = 1; i < SIG_SIZE; i++) {
802 init = hv_fetch(hv, PL_sig_name[i], strlen(PL_sig_name[i]), 1);
804 sv_setsv(*init, &PL_sv_undef);
812 if (strEQ(name, "VERSION"))
819 PL_sawampersand = TRUE;
825 PL_sawampersand = TRUE;
831 PL_sawampersand = TRUE;
837 sv_setpv(GvSV(gv),PL_chopset);
843 #ifdef COMPLEX_STATUS
844 (void)SvUPGRADE(GvSV(gv), SVt_PVLV);
852 /* If %! has been used, automatically load Errno.pm.
853 The require will itself set errno, so in order to
854 preserve its value we have to set up the magic
855 now (rather than going to magicalize)
858 sv_magic(GvSV(gv), (SV*)gv, PERL_MAGIC_sv, name, len);
860 if (sv_type == SVt_PVHV)
869 sv_magic((SV*)av, Nullsv, PERL_MAGIC_regdata, Nullch, 0);
875 if (ckWARN(WARN_DEPRECATED) && len == 1 && sv_type == SVt_PV)
876 Perl_warner(aTHX_ WARN_DEPRECATED, "Use of $%s is deprecated", name);
891 case '\001': /* $^A */
892 case '\003': /* $^C */
893 case '\004': /* $^D */
894 case '\005': /* $^E */
895 case '\006': /* $^F */
896 case '\010': /* $^H */
897 case '\011': /* $^I, NOT \t in EBCDIC */
898 case '\016': /* $^N */
899 case '\020': /* $^P */
900 case '\024': /* $^T */
907 sv_setiv(GvSV(gv), (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0);
909 case '\017': /* $^O & $^OPEN */
910 if (len > 1 && strNE(name, "\017PEN"))
913 case '\023': /* $^S */
917 case '\027': /* $^W & $^WARNING_BITS */
918 if (len > 1 && strNE(name, "\027ARNING_BITS")
919 && strNE(name, "\027IDE_SYSTEM_CALLS"))
928 sv_magic((SV*)av, (SV*)av, PERL_MAGIC_regdata, Nullch, 0);
942 SvREADONLY_on(GvSV(gv));
944 sv_magic(GvSV(gv), (SV*)gv, PERL_MAGIC_sv, name, len);
947 case '\014': /* $^L */
950 sv_setpv(GvSV(gv),"\f");
951 PL_formfeed = GvSV(gv);
956 sv_setpv(GvSV(gv),"\034");
961 (void)SvUPGRADE(sv, SVt_PVNV);
962 Perl_sv_setpvf(aTHX_ sv,
963 #if defined(PERL_SUBVERSION) && (PERL_SUBVERSION > 0)
969 SvNVX(PL_patchlevel));
970 SvNVX(sv) = SvNVX(PL_patchlevel);
975 case '\026': /* $^V */
978 GvSV(gv) = SvREFCNT_inc(PL_patchlevel);
987 Perl_gv_fullname4(pTHX_ SV *sv, GV *gv, const char *prefix, bool keepmain)
989 HV *hv = GvSTASH(gv);
994 sv_setpv(sv, prefix ? prefix : "");
995 if (keepmain || strNE(HvNAME(hv), "main")) {
996 sv_catpv(sv,HvNAME(hv));
997 sv_catpvn(sv,"::", 2);
999 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
1003 Perl_gv_fullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
1005 HV *hv = GvSTASH(gv);
1010 sv_setpv(sv, prefix ? prefix : "");
1011 sv_catpv(sv,HvNAME(hv));
1012 sv_catpvn(sv,"::", 2);
1013 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
1017 Perl_gv_efullname4(pTHX_ SV *sv, GV *gv, const char *prefix, bool keepmain)
1019 GV *egv = GvEGV(gv);
1022 gv_fullname4(sv, egv, prefix, keepmain);
1026 Perl_gv_efullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
1028 GV *egv = GvEGV(gv);
1031 gv_fullname3(sv, egv, prefix);
1034 /* XXX compatibility with versions <= 5.003. */
1036 Perl_gv_fullname(pTHX_ SV *sv, GV *gv)
1038 gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : "");
1041 /* XXX compatibility with versions <= 5.003. */
1043 Perl_gv_efullname(pTHX_ SV *sv, GV *gv)
1045 gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : "");
1054 io = (IO*)NEWSV(0,0);
1055 sv_upgrade((SV *)io,SVt_PVIO);
1058 iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV);
1059 /* unless exists($main::{FileHandle}) and defined(%main::FileHandle::) */
1060 if (!(iogv && GvHV(iogv) && HvARRAY(GvHV(iogv))))
1061 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
1062 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
1067 Perl_gv_check(pTHX_ HV *stash)
1074 if (!HvARRAY(stash))
1076 for (i = 0; i <= (I32) HvMAX(stash); i++) {
1077 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
1078 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
1079 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)))
1081 if (hv != PL_defstash && hv != stash)
1082 gv_check(hv); /* nested package */
1084 else if (isALPHA(*HeKEY(entry))) {
1086 gv = (GV*)HeVAL(entry);
1087 if (SvTYPE(gv) != SVt_PVGV || GvMULTI(gv))
1090 /* performance hack: if filename is absolute and it's a standard
1091 * module, don't bother warning */
1093 && PERL_FILE_IS_ABSOLUTE(file)
1094 #ifdef MACOS_TRADITIONAL
1095 && (instr(file, ":lib:")
1097 && (instr(file, "/lib/")
1099 || instr(file, ".pm")))
1103 CopLINE_set(PL_curcop, GvLINE(gv));
1105 CopFILE(PL_curcop) = file; /* set for warning */
1107 CopFILEGV(PL_curcop) = gv_fetchfile(file);
1109 Perl_warner(aTHX_ WARN_ONCE,
1110 "Name \"%s::%s\" used only once: possible typo",
1111 HvNAME(stash), GvNAME(gv));
1118 Perl_newGVgen(pTHX_ char *pack)
1120 return gv_fetchpv(Perl_form(aTHX_ "%s::_GEN_%ld", pack, (long)PL_gensym++),
1124 /* hopefully this is only called on local symbol table entries */
1127 Perl_gp_ref(pTHX_ GP *gp)
1134 /* multi-named GPs cannot be used for method cache */
1135 SvREFCNT_dec(gp->gp_cv);
1140 /* Adding a new name to a subroutine invalidates method cache */
1141 PL_sub_generation++;
1148 Perl_gp_free(pTHX_ GV *gv)
1152 if (!gv || !(gp = GvGP(gv)))
1154 if (gp->gp_refcnt == 0) {
1155 if (ckWARN_d(WARN_INTERNAL))
1156 Perl_warner(aTHX_ WARN_INTERNAL,
1157 "Attempt to free unreferenced glob pointers");
1161 /* Deleting the name of a subroutine invalidates method cache */
1162 PL_sub_generation++;
1164 if (--gp->gp_refcnt > 0) {
1165 if (gp->gp_egv == gv)
1170 SvREFCNT_dec(gp->gp_sv);
1171 SvREFCNT_dec(gp->gp_av);
1172 SvREFCNT_dec(gp->gp_hv);
1173 SvREFCNT_dec(gp->gp_io);
1174 SvREFCNT_dec(gp->gp_cv);
1175 SvREFCNT_dec(gp->gp_form);
1181 #if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
1185 #ifdef MICROPORT /* Microport 2.4 hack */
1189 if (GvGP(gv)->gp_av)
1190 return GvGP(gv)->gp_av;
1192 return GvGP(gv_AVadd(gv))->gp_av;
1198 if (GvGP(gv)->gp_hv)
1199 return GvGP(gv)->gp_hv;
1201 return GvGP(gv_HVadd(gv))->gp_hv;
1203 #endif /* Microport 2.4 hack */
1206 Perl_magic_freeovrld(pTHX_ SV *sv, MAGIC *mg)
1208 AMT *amtp = (AMT*)mg->mg_ptr;
1209 if (amtp && AMT_AMAGIC(amtp)) {
1211 for (i = 1; i < NofAMmeth; i++) {
1212 CV *cv = amtp->table[i];
1214 SvREFCNT_dec((SV *) cv);
1215 amtp->table[i] = Nullcv;
1222 /* Updates and caches the CV's */
1225 Perl_Gv_AMupdate(pTHX_ HV *stash)
1229 MAGIC* mg=mg_find((SV*)stash, PERL_MAGIC_overload_table);
1230 AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL;
1233 if (mg && amtp->was_ok_am == PL_amagic_generation
1234 && amtp->was_ok_sub == PL_sub_generation)
1235 return AMT_OVERLOADED(amtp);
1236 sv_unmagic((SV*)stash, PERL_MAGIC_overload_table);
1238 DEBUG_o( Perl_deb(aTHX_ "Recalcing overload magic in package %s\n",HvNAME(stash)) );
1241 amt.was_ok_am = PL_amagic_generation;
1242 amt.was_ok_sub = PL_sub_generation;
1243 amt.fallback = AMGfallNO;
1247 int filled = 0, have_ovl = 0;
1251 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
1253 /* Try to find via inheritance. */
1254 gv = gv_fetchmeth(stash, PL_AMG_names[0], 2, -1);
1259 lim = DESTROY_amg; /* Skip overloading entries. */
1260 else if (SvTRUE(sv))
1261 amt.fallback=AMGfallYES;
1263 amt.fallback=AMGfallNEVER;
1265 for (i = 1; i < lim; i++)
1266 amt.table[i] = Nullcv;
1267 for (; i < NofAMmeth; i++) {
1268 char *cooky = (char*)PL_AMG_names[i];
1269 /* Human-readable form, for debugging: */
1270 char *cp = (i >= DESTROY_amg ? cooky : AMG_id2name(i));
1271 STRLEN l = strlen(cooky);
1273 DEBUG_o( Perl_deb(aTHX_ "Checking overloading of `%s' in package `%.256s'\n",
1274 cp, HvNAME(stash)) );
1275 /* don't fill the cache while looking up! */
1276 gv = gv_fetchmeth(stash, cooky, l, -1);
1278 if (gv && (cv = GvCV(gv))) {
1279 if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil")
1280 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) {
1281 /* GvSV contains the name of the method. */
1284 DEBUG_o( Perl_deb(aTHX_ "Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n",
1285 SvPV_nolen(GvSV(gv)), cp, HvNAME(stash)) );
1286 if (!SvPOK(GvSV(gv))
1287 || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)),
1290 /* Can be an import stub (created by `can'). */
1292 Perl_croak(aTHX_ "Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'",
1293 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1296 Perl_croak(aTHX_ "Can't resolve method `%.256s' overloading `%s' in package `%.256s'",
1297 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1300 cv = GvCV(gv = ngv);
1302 DEBUG_o( Perl_deb(aTHX_ "Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n",
1303 cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))),
1304 GvNAME(CvGV(cv))) );
1306 if (i < DESTROY_amg)
1309 amt.table[i]=(CV*)SvREFCNT_inc(cv);
1312 AMT_AMAGIC_on(&amt);
1314 AMT_OVERLOADED_on(&amt);
1315 sv_magic((SV*)stash, 0, PERL_MAGIC_overload_table,
1316 (char*)&amt, sizeof(AMT));
1320 /* Here we have no table: */
1322 AMT_AMAGIC_off(&amt);
1323 sv_magic((SV*)stash, 0, PERL_MAGIC_overload_table,
1324 (char*)&amt, sizeof(AMTS));
1330 Perl_gv_handler(pTHX_ HV *stash, I32 id)
1337 mg = mg_find((SV*)stash, PERL_MAGIC_overload_table);
1341 mg = mg_find((SV*)stash, PERL_MAGIC_overload_table);
1343 amtp = (AMT*)mg->mg_ptr;
1344 if ( amtp->was_ok_am != PL_amagic_generation
1345 || amtp->was_ok_sub != PL_sub_generation )
1347 if (AMT_AMAGIC(amtp))
1348 return amtp->table[id];
1354 Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags)
1358 CV **cvp=NULL, **ocvp=NULL;
1359 AMT *amtp=NULL, *oamtp=NULL;
1360 int off=0, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
1361 int postpr = 0, force_cpy = 0, assignshift = assign ? 1 : 0;
1364 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
1365 && (mg = mg_find((SV*)(
1367 SvSTASH(SvRV(left))),
1368 PERL_MAGIC_overload_table))
1369 && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1370 ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table
1372 && ((cv = cvp[off=method+assignshift])
1373 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
1377 cv = cvp[off=method])))) {
1378 lr = -1; /* Call method for left argument */
1380 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
1383 /* look for substituted methods */
1384 /* In all the covered cases we should be called with assign==0. */
1388 if ((cv = cvp[off=add_ass_amg])
1389 || ((cv = cvp[off = add_amg]) && (force_cpy = 0, postpr = 1))) {
1390 right = &PL_sv_yes; lr = -1; assign = 1;
1395 if ((cv = cvp[off = subtr_ass_amg])
1396 || ((cv = cvp[off = subtr_amg]) && (force_cpy = 0, postpr=1))) {
1397 right = &PL_sv_yes; lr = -1; assign = 1;
1401 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
1404 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1407 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1410 (void)((cv = cvp[off=bool__amg])
1411 || (cv = cvp[off=numer_amg])
1412 || (cv = cvp[off=string_amg]));
1418 * SV* ref causes confusion with the interpreter variable of
1421 SV* tmpRef=SvRV(left);
1422 if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) {
1424 * Just to be extra cautious. Maybe in some
1425 * additional cases sv_setsv is safe, too.
1427 SV* newref = newSVsv(tmpRef);
1428 SvOBJECT_on(newref);
1429 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(tmpRef));
1435 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
1436 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
1437 SV* nullsv=sv_2mortal(newSViv(0));
1439 SV* lessp = amagic_call(left,nullsv,
1440 lt_amg,AMGf_noright);
1441 logic = SvTRUE(lessp);
1443 SV* lessp = amagic_call(left,nullsv,
1444 ncmp_amg,AMGf_noright);
1445 logic = (SvNV(lessp) < 0);
1448 if (off==subtr_amg) {
1459 if ((cv = cvp[off=subtr_amg])) {
1461 left = sv_2mortal(newSViv(0));
1466 case iter_amg: /* XXXX Eventually should do to_gv. */
1468 return NULL; /* Delegate operation to standard mechanisms. */
1476 return left; /* Delegate operation to standard mechanisms. */
1481 if (!cv) goto not_found;
1482 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1483 && (mg = mg_find((SV*)(
1485 SvSTASH(SvRV(right))),
1486 PERL_MAGIC_overload_table))
1487 && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1488 ? (amtp = (AMT*)mg->mg_ptr)->table
1490 && (cv = cvp[off=method])) { /* Method for right
1493 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
1494 && (cvp=ocvp) && (lr = -1))
1495 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1496 && !(flags & AMGf_unary)) {
1497 /* We look for substitution for
1498 * comparison operations and
1500 if (method==concat_amg || method==concat_ass_amg
1501 || method==repeat_amg || method==repeat_ass_amg) {
1502 return NULL; /* Delegate operation to string conversion */
1512 postpr = 1; off=ncmp_amg; break;
1519 postpr = 1; off=scmp_amg; break;
1521 if (off != -1) cv = cvp[off];
1526 not_found: /* No method found, either report or croak */
1534 return left; /* Delegate operation to standard mechanisms. */
1537 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1538 notfound = 1; lr = -1;
1539 } else if (cvp && (cv=cvp[nomethod_amg])) {
1540 notfound = 1; lr = 1;
1543 if (off==-1) off=method;
1544 msg = sv_2mortal(Perl_newSVpvf(aTHX_
1545 "Operation `%s': no method found,%sargument %s%s%s%s",
1546 AMG_id2name(method + assignshift),
1547 (flags & AMGf_unary ? " " : "\n\tleft "),
1549 "in overloaded package ":
1550 "has no overloaded magic",
1552 HvNAME(SvSTASH(SvRV(left))):
1555 ",\n\tright argument in overloaded package ":
1558 : ",\n\tright argument has no overloaded magic"),
1560 HvNAME(SvSTASH(SvRV(right))):
1562 if (amtp && amtp->fallback >= AMGfallYES) {
1563 DEBUG_o( Perl_deb(aTHX_ "%s", SvPVX(msg)) );
1565 Perl_croak(aTHX_ "%"SVf, msg);
1569 force_cpy = force_cpy || assign;
1574 DEBUG_o(Perl_deb(aTHX_
1575 "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n",
1577 method+assignshift==off? "" :
1579 method+assignshift==off? "" :
1580 AMG_id2name(method+assignshift),
1581 method+assignshift==off? "" : "')",
1582 flags & AMGf_unary? "" :
1583 lr==1 ? " for right argument": " for left argument",
1584 flags & AMGf_unary? " for argument" : "",
1586 fl? ",\n\tassignment variant used": "") );
1589 /* Since we use shallow copy during assignment, we need
1590 * to dublicate the contents, probably calling user-supplied
1591 * version of copy operator
1593 /* We need to copy in following cases:
1594 * a) Assignment form was called.
1595 * assignshift==1, assign==T, method + 1 == off
1596 * b) Increment or decrement, called directly.
1597 * assignshift==0, assign==0, method + 0 == off
1598 * c) Increment or decrement, translated to assignment add/subtr.
1599 * assignshift==0, assign==T,
1601 * d) Increment or decrement, translated to nomethod.
1602 * assignshift==0, assign==0,
1604 * e) Assignment form translated to nomethod.
1605 * assignshift==1, assign==T, method + 1 != off
1608 /* off is method, method+assignshift, or a result of opcode substitution.
1609 * In the latter case assignshift==0, so only notfound case is important.
1611 if (( (method + assignshift == off)
1612 && (assign || (method == inc_amg) || (method == dec_amg)))
1619 bool oldcatch = CATCH_GET;
1622 Zero(&myop, 1, BINOP);
1623 myop.op_last = (OP *) &myop;
1624 myop.op_next = Nullop;
1625 myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED;
1627 PUSHSTACKi(PERLSI_OVERLOAD);
1630 PL_op = (OP *) &myop;
1631 if (PERLDB_SUB && PL_curstash != PL_debstash)
1632 PL_op->op_private |= OPpENTERSUB_DB;
1636 EXTEND(SP, notfound + 5);
1637 PUSHs(lr>0? right: left);
1638 PUSHs(lr>0? left: right);
1639 PUSHs( lr > 0 ? &PL_sv_yes : ( assign ? &PL_sv_undef : &PL_sv_no ));
1641 PUSHs( sv_2mortal(newSVpv(AMG_id2name(method + assignshift),0)));
1646 if ((PL_op = Perl_pp_entersub(aTHX)))
1654 CATCH_SET(oldcatch);
1661 ans=SvIV(res)<=0; break;
1664 ans=SvIV(res)<0; break;
1667 ans=SvIV(res)>=0; break;
1670 ans=SvIV(res)>0; break;
1673 ans=SvIV(res)==0; break;
1676 ans=SvIV(res)!=0; break;
1679 SvSetSV(left,res); return left;
1681 ans=!SvTRUE(res); break;
1684 } else if (method==copy_amg) {
1686 Perl_croak(aTHX_ "Copy method did not return a reference");
1688 return SvREFCNT_inc(SvRV(res));
1696 =for apidoc is_gv_magical
1698 Returns C<TRUE> if given the name of a magical GV.
1700 Currently only useful internally when determining if a GV should be
1701 created even in rvalue contexts.
1703 C<flags> is not used at present but available for future extension to
1704 allow selecting particular classes of magical variable.
1709 Perl_is_gv_magical(pTHX_ char *name, STRLEN len, U32 flags)
1716 if (len == 3 && strEQ(name, "ISA"))
1720 if (len == 8 && strEQ(name, "OVERLOAD"))
1724 if (len == 3 && strEQ(name, "SIG"))
1727 case '\017': /* $^O & $^OPEN */
1729 || (len == 4 && strEQ(name, "\027PEN")))
1734 case '\027': /* $^W & $^WARNING_BITS */
1736 || (len == 12 && strEQ(name, "\027ARNING_BITS"))
1737 || (len == 17 && strEQ(name, "\027IDE_SYSTEM_CALLS")))
1769 case '\001': /* $^A */
1770 case '\003': /* $^C */
1771 case '\004': /* $^D */
1772 case '\005': /* $^E */
1773 case '\006': /* $^F */
1774 case '\010': /* $^H */
1775 case '\011': /* $^I, NOT \t in EBCDIC */
1776 case '\014': /* $^L */
1777 case '\016': /* $^N */
1778 case '\020': /* $^P */
1779 case '\023': /* $^S */
1780 case '\024': /* $^T */
1781 case '\026': /* $^V */
1795 char *end = name + len;
1796 while (--end > name) {