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_UNIQUE_CHECK
51 Perl_croak(aTHX_ "Bad symbol for filehandle (GV is unique)");
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;
131 #ifdef USE_5005THREADS
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_5005THREADS */
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);
442 #ifndef USE_5005THREADS
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);
466 #ifdef USE_5005THREADS
467 sv_lock((SV *)varstash);
470 gv_init(vargv, varstash, autoload, autolen, FALSE);
473 #ifdef USE_5005THREADS
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"))
821 sv_type == SVt_PVAV ||
822 sv_type == SVt_PVHV ||
823 sv_type == SVt_PVCV ||
824 sv_type == SVt_PVFM ||
827 PL_sawampersand = TRUE;
833 sv_setpv(GvSV(gv),PL_chopset);
839 #ifdef COMPLEX_STATUS
840 (void)SvUPGRADE(GvSV(gv), SVt_PVLV);
848 /* If %! has been used, automatically load Errno.pm.
849 The require will itself set errno, so in order to
850 preserve its value we have to set up the magic
851 now (rather than going to magicalize)
854 sv_magic(GvSV(gv), (SV*)gv, PERL_MAGIC_sv, name, len);
856 if (sv_type == SVt_PVHV)
865 sv_magic((SV*)av, Nullsv, PERL_MAGIC_regdata, Nullch, 0);
871 if (ckWARN(WARN_DEPRECATED) && len == 1 && sv_type == SVt_PV)
872 Perl_warner(aTHX_ WARN_DEPRECATED, "Use of $%s is deprecated", name);
887 case '\001': /* $^A */
888 case '\003': /* $^C */
889 case '\004': /* $^D */
890 case '\005': /* $^E */
891 case '\006': /* $^F */
892 case '\010': /* $^H */
893 case '\011': /* $^I, NOT \t in EBCDIC */
894 case '\016': /* $^N */
895 case '\020': /* $^P */
896 case '\024': /* $^T */
903 sv_setiv(GvSV(gv), (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0);
905 case '\017': /* $^O & $^OPEN */
906 if (len > 1 && strNE(name, "\017PEN"))
909 case '\023': /* $^S */
913 case '\027': /* $^W & $^WARNING_BITS */
914 if (len > 1 && strNE(name, "\027ARNING_BITS")
915 && strNE(name, "\027IDE_SYSTEM_CALLS"))
924 sv_magic((SV*)av, (SV*)av, PERL_MAGIC_regdata, Nullch, 0);
937 /* ensures variable is only digits */
938 /* ${"1foo"} fails this test (and is thus writeable) */
939 /* added by japhy, but borrowed from is_gv_magical */
942 const char *end = name + len;
943 while (--end > name) {
944 if (!isDIGIT(*end)) return gv;
949 SvREADONLY_on(GvSV(gv));
951 sv_magic(GvSV(gv), (SV*)gv, PERL_MAGIC_sv, name, len);
954 case '\014': /* $^L */
957 sv_setpv(GvSV(gv),"\f");
958 PL_formfeed = GvSV(gv);
963 sv_setpv(GvSV(gv),"\034");
968 (void)SvUPGRADE(sv, SVt_PVNV);
969 Perl_sv_setpvf(aTHX_ sv,
970 #if defined(PERL_SUBVERSION) && (PERL_SUBVERSION > 0)
976 SvNVX(PL_patchlevel));
977 SvNVX(sv) = SvNVX(PL_patchlevel);
982 case '\026': /* $^V */
985 GvSV(gv) = SvREFCNT_inc(PL_patchlevel);
994 Perl_gv_fullname4(pTHX_ SV *sv, GV *gv, const char *prefix, bool keepmain)
996 HV *hv = GvSTASH(gv);
1001 sv_setpv(sv, prefix ? prefix : "");
1002 if (keepmain || strNE(HvNAME(hv), "main")) {
1003 sv_catpv(sv,HvNAME(hv));
1004 sv_catpvn(sv,"::", 2);
1006 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
1010 Perl_gv_fullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
1012 HV *hv = GvSTASH(gv);
1017 sv_setpv(sv, prefix ? prefix : "");
1018 sv_catpv(sv,HvNAME(hv));
1019 sv_catpvn(sv,"::", 2);
1020 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
1024 Perl_gv_efullname4(pTHX_ SV *sv, GV *gv, const char *prefix, bool keepmain)
1026 GV *egv = GvEGV(gv);
1029 gv_fullname4(sv, egv, prefix, keepmain);
1033 Perl_gv_efullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
1035 GV *egv = GvEGV(gv);
1038 gv_fullname3(sv, egv, prefix);
1041 /* XXX compatibility with versions <= 5.003. */
1043 Perl_gv_fullname(pTHX_ SV *sv, GV *gv)
1045 gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : "");
1048 /* XXX compatibility with versions <= 5.003. */
1050 Perl_gv_efullname(pTHX_ SV *sv, GV *gv)
1052 gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : "");
1061 io = (IO*)NEWSV(0,0);
1062 sv_upgrade((SV *)io,SVt_PVIO);
1065 iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV);
1066 /* unless exists($main::{FileHandle}) and defined(%main::FileHandle::) */
1067 if (!(iogv && GvHV(iogv) && HvARRAY(GvHV(iogv))))
1068 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
1069 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
1074 Perl_gv_check(pTHX_ HV *stash)
1081 if (!HvARRAY(stash))
1083 for (i = 0; i <= (I32) HvMAX(stash); i++) {
1084 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
1085 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
1086 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)))
1088 if (hv != PL_defstash && hv != stash)
1089 gv_check(hv); /* nested package */
1091 else if (isALPHA(*HeKEY(entry))) {
1093 gv = (GV*)HeVAL(entry);
1094 if (SvTYPE(gv) != SVt_PVGV || GvMULTI(gv))
1097 /* performance hack: if filename is absolute and it's a standard
1098 * module, don't bother warning */
1100 && PERL_FILE_IS_ABSOLUTE(file)
1101 #ifdef MACOS_TRADITIONAL
1102 && (instr(file, ":lib:")
1104 && (instr(file, "/lib/")
1106 || instr(file, ".pm")))
1110 CopLINE_set(PL_curcop, GvLINE(gv));
1112 CopFILE(PL_curcop) = file; /* set for warning */
1114 CopFILEGV(PL_curcop) = gv_fetchfile(file);
1116 Perl_warner(aTHX_ WARN_ONCE,
1117 "Name \"%s::%s\" used only once: possible typo",
1118 HvNAME(stash), GvNAME(gv));
1125 Perl_newGVgen(pTHX_ char *pack)
1127 return gv_fetchpv(Perl_form(aTHX_ "%s::_GEN_%ld", pack, (long)PL_gensym++),
1131 /* hopefully this is only called on local symbol table entries */
1134 Perl_gp_ref(pTHX_ GP *gp)
1141 /* multi-named GPs cannot be used for method cache */
1142 SvREFCNT_dec(gp->gp_cv);
1147 /* Adding a new name to a subroutine invalidates method cache */
1148 PL_sub_generation++;
1155 Perl_gp_free(pTHX_ GV *gv)
1159 if (!gv || !(gp = GvGP(gv)))
1161 if (gp->gp_refcnt == 0) {
1162 if (ckWARN_d(WARN_INTERNAL))
1163 Perl_warner(aTHX_ WARN_INTERNAL,
1164 "Attempt to free unreferenced glob pointers");
1168 /* Deleting the name of a subroutine invalidates method cache */
1169 PL_sub_generation++;
1171 if (--gp->gp_refcnt > 0) {
1172 if (gp->gp_egv == gv)
1177 SvREFCNT_dec(gp->gp_sv);
1178 SvREFCNT_dec(gp->gp_av);
1179 SvREFCNT_dec(gp->gp_hv);
1180 SvREFCNT_dec(gp->gp_io);
1181 SvREFCNT_dec(gp->gp_cv);
1182 SvREFCNT_dec(gp->gp_form);
1188 #if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
1192 #ifdef MICROPORT /* Microport 2.4 hack */
1196 if (GvGP(gv)->gp_av)
1197 return GvGP(gv)->gp_av;
1199 return GvGP(gv_AVadd(gv))->gp_av;
1205 if (GvGP(gv)->gp_hv)
1206 return GvGP(gv)->gp_hv;
1208 return GvGP(gv_HVadd(gv))->gp_hv;
1210 #endif /* Microport 2.4 hack */
1213 Perl_magic_freeovrld(pTHX_ SV *sv, MAGIC *mg)
1215 AMT *amtp = (AMT*)mg->mg_ptr;
1216 if (amtp && AMT_AMAGIC(amtp)) {
1218 for (i = 1; i < NofAMmeth; i++) {
1219 CV *cv = amtp->table[i];
1221 SvREFCNT_dec((SV *) cv);
1222 amtp->table[i] = Nullcv;
1229 /* Updates and caches the CV's */
1232 Perl_Gv_AMupdate(pTHX_ HV *stash)
1236 MAGIC* mg=mg_find((SV*)stash, PERL_MAGIC_overload_table);
1237 AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL;
1240 if (mg && amtp->was_ok_am == PL_amagic_generation
1241 && amtp->was_ok_sub == PL_sub_generation)
1242 return AMT_OVERLOADED(amtp);
1243 sv_unmagic((SV*)stash, PERL_MAGIC_overload_table);
1245 DEBUG_o( Perl_deb(aTHX_ "Recalcing overload magic in package %s\n",HvNAME(stash)) );
1248 amt.was_ok_am = PL_amagic_generation;
1249 amt.was_ok_sub = PL_sub_generation;
1250 amt.fallback = AMGfallNO;
1254 int filled = 0, have_ovl = 0;
1258 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
1260 /* Try to find via inheritance. */
1261 gv = gv_fetchmeth(stash, PL_AMG_names[0], 2, -1);
1266 lim = DESTROY_amg; /* Skip overloading entries. */
1267 else if (SvTRUE(sv))
1268 amt.fallback=AMGfallYES;
1270 amt.fallback=AMGfallNEVER;
1272 for (i = 1; i < lim; i++)
1273 amt.table[i] = Nullcv;
1274 for (; i < NofAMmeth; i++) {
1275 char *cooky = (char*)PL_AMG_names[i];
1276 /* Human-readable form, for debugging: */
1277 char *cp = (i >= DESTROY_amg ? cooky : AMG_id2name(i));
1278 STRLEN l = strlen(cooky);
1280 DEBUG_o( Perl_deb(aTHX_ "Checking overloading of `%s' in package `%.256s'\n",
1281 cp, HvNAME(stash)) );
1282 /* don't fill the cache while looking up! */
1283 gv = gv_fetchmeth(stash, cooky, l, -1);
1285 if (gv && (cv = GvCV(gv))) {
1286 if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil")
1287 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) {
1288 /* GvSV contains the name of the method. */
1291 DEBUG_o( Perl_deb(aTHX_ "Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n",
1292 SvPV_nolen(GvSV(gv)), cp, HvNAME(stash)) );
1293 if (!SvPOK(GvSV(gv))
1294 || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)),
1297 /* Can be an import stub (created by `can'). */
1299 Perl_croak(aTHX_ "Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'",
1300 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1303 Perl_croak(aTHX_ "Can't resolve method `%.256s' overloading `%s' in package `%.256s'",
1304 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1307 cv = GvCV(gv = ngv);
1309 DEBUG_o( Perl_deb(aTHX_ "Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n",
1310 cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))),
1311 GvNAME(CvGV(cv))) );
1313 if (i < DESTROY_amg)
1316 amt.table[i]=(CV*)SvREFCNT_inc(cv);
1319 AMT_AMAGIC_on(&amt);
1321 AMT_OVERLOADED_on(&amt);
1322 sv_magic((SV*)stash, 0, PERL_MAGIC_overload_table,
1323 (char*)&amt, sizeof(AMT));
1327 /* Here we have no table: */
1329 AMT_AMAGIC_off(&amt);
1330 sv_magic((SV*)stash, 0, PERL_MAGIC_overload_table,
1331 (char*)&amt, sizeof(AMTS));
1337 Perl_gv_handler(pTHX_ HV *stash, I32 id)
1344 mg = mg_find((SV*)stash, PERL_MAGIC_overload_table);
1348 mg = mg_find((SV*)stash, PERL_MAGIC_overload_table);
1350 amtp = (AMT*)mg->mg_ptr;
1351 if ( amtp->was_ok_am != PL_amagic_generation
1352 || amtp->was_ok_sub != PL_sub_generation )
1354 if (AMT_AMAGIC(amtp))
1355 return amtp->table[id];
1361 Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags)
1365 CV **cvp=NULL, **ocvp=NULL;
1366 AMT *amtp=NULL, *oamtp=NULL;
1367 int off=0, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
1368 int postpr = 0, force_cpy = 0, assignshift = assign ? 1 : 0;
1373 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
1374 && (stash = SvSTASH(SvRV(left)))
1375 && (mg = mg_find((SV*)stash, PERL_MAGIC_overload_table))
1376 && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1377 ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table
1379 && ((cv = cvp[off=method+assignshift])
1380 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
1386 cv = cvp[off=method])))) {
1387 lr = -1; /* Call method for left argument */
1389 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
1392 /* look for substituted methods */
1393 /* In all the covered cases we should be called with assign==0. */
1397 if ((cv = cvp[off=add_ass_amg])
1398 || ((cv = cvp[off = add_amg]) && (force_cpy = 0, postpr = 1))) {
1399 right = &PL_sv_yes; lr = -1; assign = 1;
1404 if ((cv = cvp[off = subtr_ass_amg])
1405 || ((cv = cvp[off = subtr_amg]) && (force_cpy = 0, postpr=1))) {
1406 right = &PL_sv_yes; lr = -1; assign = 1;
1410 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
1413 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1416 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1419 (void)((cv = cvp[off=bool__amg])
1420 || (cv = cvp[off=numer_amg])
1421 || (cv = cvp[off=string_amg]));
1427 * SV* ref causes confusion with the interpreter variable of
1430 SV* tmpRef=SvRV(left);
1431 if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) {
1433 * Just to be extra cautious. Maybe in some
1434 * additional cases sv_setsv is safe, too.
1436 SV* newref = newSVsv(tmpRef);
1437 SvOBJECT_on(newref);
1438 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(tmpRef));
1444 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
1445 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
1446 SV* nullsv=sv_2mortal(newSViv(0));
1448 SV* lessp = amagic_call(left,nullsv,
1449 lt_amg,AMGf_noright);
1450 logic = SvTRUE(lessp);
1452 SV* lessp = amagic_call(left,nullsv,
1453 ncmp_amg,AMGf_noright);
1454 logic = (SvNV(lessp) < 0);
1457 if (off==subtr_amg) {
1468 if ((cv = cvp[off=subtr_amg])) {
1470 left = sv_2mortal(newSViv(0));
1475 case iter_amg: /* XXXX Eventually should do to_gv. */
1477 return NULL; /* Delegate operation to standard mechanisms. */
1485 return left; /* Delegate operation to standard mechanisms. */
1490 if (!cv) goto not_found;
1491 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1492 && (stash = SvSTASH(SvRV(right)))
1493 && (mg = mg_find((SV*)stash, PERL_MAGIC_overload_table))
1494 && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1495 ? (amtp = (AMT*)mg->mg_ptr)->table
1497 && (cv = cvp[off=method])) { /* Method for right
1500 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
1501 && (cvp=ocvp) && (lr = -1))
1502 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1503 && !(flags & AMGf_unary)) {
1504 /* We look for substitution for
1505 * comparison operations and
1507 if (method==concat_amg || method==concat_ass_amg
1508 || method==repeat_amg || method==repeat_ass_amg) {
1509 return NULL; /* Delegate operation to string conversion */
1519 postpr = 1; off=ncmp_amg; break;
1526 postpr = 1; off=scmp_amg; break;
1528 if (off != -1) cv = cvp[off];
1533 not_found: /* No method found, either report or croak */
1541 return left; /* Delegate operation to standard mechanisms. */
1544 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1545 notfound = 1; lr = -1;
1546 } else if (cvp && (cv=cvp[nomethod_amg])) {
1547 notfound = 1; lr = 1;
1550 if (off==-1) off=method;
1551 msg = sv_2mortal(Perl_newSVpvf(aTHX_
1552 "Operation `%s': no method found,%sargument %s%s%s%s",
1553 AMG_id2name(method + assignshift),
1554 (flags & AMGf_unary ? " " : "\n\tleft "),
1556 "in overloaded package ":
1557 "has no overloaded magic",
1559 HvNAME(SvSTASH(SvRV(left))):
1562 ",\n\tright argument in overloaded package ":
1565 : ",\n\tright argument has no overloaded magic"),
1567 HvNAME(SvSTASH(SvRV(right))):
1569 if (amtp && amtp->fallback >= AMGfallYES) {
1570 DEBUG_o( Perl_deb(aTHX_ "%s", SvPVX(msg)) );
1572 Perl_croak(aTHX_ "%"SVf, msg);
1576 force_cpy = force_cpy || assign;
1581 DEBUG_o(Perl_deb(aTHX_
1582 "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n",
1584 method+assignshift==off? "" :
1586 method+assignshift==off? "" :
1587 AMG_id2name(method+assignshift),
1588 method+assignshift==off? "" : "')",
1589 flags & AMGf_unary? "" :
1590 lr==1 ? " for right argument": " for left argument",
1591 flags & AMGf_unary? " for argument" : "",
1592 stash ? HvNAME(stash) : "null",
1593 fl? ",\n\tassignment variant used": "") );
1596 /* Since we use shallow copy during assignment, we need
1597 * to dublicate the contents, probably calling user-supplied
1598 * version of copy operator
1600 /* We need to copy in following cases:
1601 * a) Assignment form was called.
1602 * assignshift==1, assign==T, method + 1 == off
1603 * b) Increment or decrement, called directly.
1604 * assignshift==0, assign==0, method + 0 == off
1605 * c) Increment or decrement, translated to assignment add/subtr.
1606 * assignshift==0, assign==T,
1608 * d) Increment or decrement, translated to nomethod.
1609 * assignshift==0, assign==0,
1611 * e) Assignment form translated to nomethod.
1612 * assignshift==1, assign==T, method + 1 != off
1615 /* off is method, method+assignshift, or a result of opcode substitution.
1616 * In the latter case assignshift==0, so only notfound case is important.
1618 if (( (method + assignshift == off)
1619 && (assign || (method == inc_amg) || (method == dec_amg)))
1626 bool oldcatch = CATCH_GET;
1629 Zero(&myop, 1, BINOP);
1630 myop.op_last = (OP *) &myop;
1631 myop.op_next = Nullop;
1632 myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED;
1634 PUSHSTACKi(PERLSI_OVERLOAD);
1637 PL_op = (OP *) &myop;
1638 if (PERLDB_SUB && PL_curstash != PL_debstash)
1639 PL_op->op_private |= OPpENTERSUB_DB;
1643 EXTEND(SP, notfound + 5);
1644 PUSHs(lr>0? right: left);
1645 PUSHs(lr>0? left: right);
1646 PUSHs( lr > 0 ? &PL_sv_yes : ( assign ? &PL_sv_undef : &PL_sv_no ));
1648 PUSHs( sv_2mortal(newSVpv(AMG_id2name(method + assignshift),0)));
1653 if ((PL_op = Perl_pp_entersub(aTHX)))
1661 CATCH_SET(oldcatch);
1668 ans=SvIV(res)<=0; break;
1671 ans=SvIV(res)<0; break;
1674 ans=SvIV(res)>=0; break;
1677 ans=SvIV(res)>0; break;
1680 ans=SvIV(res)==0; break;
1683 ans=SvIV(res)!=0; break;
1686 SvSetSV(left,res); return left;
1688 ans=!SvTRUE(res); break;
1691 } else if (method==copy_amg) {
1693 Perl_croak(aTHX_ "Copy method did not return a reference");
1695 return SvREFCNT_inc(SvRV(res));
1703 =for apidoc is_gv_magical
1705 Returns C<TRUE> if given the name of a magical GV.
1707 Currently only useful internally when determining if a GV should be
1708 created even in rvalue contexts.
1710 C<flags> is not used at present but available for future extension to
1711 allow selecting particular classes of magical variable.
1716 Perl_is_gv_magical(pTHX_ char *name, STRLEN len, U32 flags)
1723 if (len == 3 && strEQ(name, "ISA"))
1727 if (len == 8 && strEQ(name, "OVERLOAD"))
1731 if (len == 3 && strEQ(name, "SIG"))
1734 case '\017': /* $^O & $^OPEN */
1736 || (len == 4 && strEQ(name, "\027PEN")))
1741 case '\027': /* $^W & $^WARNING_BITS */
1743 || (len == 12 && strEQ(name, "\027ARNING_BITS"))
1744 || (len == 17 && strEQ(name, "\027IDE_SYSTEM_CALLS")))
1776 case '\001': /* $^A */
1777 case '\003': /* $^C */
1778 case '\004': /* $^D */
1779 case '\005': /* $^E */
1780 case '\006': /* $^F */
1781 case '\010': /* $^H */
1782 case '\011': /* $^I, NOT \t in EBCDIC */
1783 case '\014': /* $^L */
1784 case '\016': /* $^N */
1785 case '\020': /* $^P */
1786 case '\023': /* $^S */
1787 case '\024': /* $^T */
1788 case '\026': /* $^V */
1802 char *end = name + len;
1803 while (--end > name) {