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_PVGV ||
825 sv_type == SVt_PVFM ||
828 PL_sawampersand = TRUE;
834 sv_setpv(GvSV(gv),PL_chopset);
840 #ifdef COMPLEX_STATUS
841 (void)SvUPGRADE(GvSV(gv), SVt_PVLV);
849 /* If %! has been used, automatically load Errno.pm.
850 The require will itself set errno, so in order to
851 preserve its value we have to set up the magic
852 now (rather than going to magicalize)
855 sv_magic(GvSV(gv), (SV*)gv, PERL_MAGIC_sv, name, len);
857 if (sv_type == SVt_PVHV)
866 sv_magic((SV*)av, Nullsv, PERL_MAGIC_regdata, Nullch, 0);
872 if (ckWARN(WARN_DEPRECATED) && len == 1 && sv_type == SVt_PV)
873 Perl_warner(aTHX_ WARN_DEPRECATED, "Use of $%s is deprecated", name);
888 case '\001': /* $^A */
889 case '\003': /* $^C */
890 case '\004': /* $^D */
891 case '\005': /* $^E */
892 case '\006': /* $^F */
893 case '\010': /* $^H */
894 case '\011': /* $^I, NOT \t in EBCDIC */
895 case '\016': /* $^N */
896 case '\020': /* $^P */
897 case '\024': /* $^T */
904 sv_setiv(GvSV(gv), (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0);
906 case '\017': /* $^O & $^OPEN */
907 if (len > 1 && strNE(name, "\017PEN"))
910 case '\023': /* $^S */
914 case '\027': /* $^W & $^WARNING_BITS */
915 if (len > 1 && strNE(name, "\027ARNING_BITS")
916 && strNE(name, "\027IDE_SYSTEM_CALLS"))
925 sv_magic((SV*)av, (SV*)av, PERL_MAGIC_regdata, Nullch, 0);
938 /* ensures variable is only digits */
939 /* ${"1foo"} fails this test (and is thus writeable) */
940 /* added by japhy, but borrowed from is_gv_magical */
943 const char *end = name + len;
944 while (--end > name) {
945 if (!isDIGIT(*end)) return gv;
950 SvREADONLY_on(GvSV(gv));
952 sv_magic(GvSV(gv), (SV*)gv, PERL_MAGIC_sv, name, len);
955 case '\014': /* $^L */
958 sv_setpv(GvSV(gv),"\f");
959 PL_formfeed = GvSV(gv);
964 sv_setpv(GvSV(gv),"\034");
969 (void)SvUPGRADE(sv, SVt_PVNV);
970 Perl_sv_setpvf(aTHX_ sv,
971 #if defined(PERL_SUBVERSION) && (PERL_SUBVERSION > 0)
977 SvNVX(PL_patchlevel));
978 SvNVX(sv) = SvNVX(PL_patchlevel);
983 case '\026': /* $^V */
986 GvSV(gv) = SvREFCNT_inc(PL_patchlevel);
995 Perl_gv_fullname4(pTHX_ SV *sv, GV *gv, const char *prefix, bool keepmain)
997 HV *hv = GvSTASH(gv);
1002 sv_setpv(sv, prefix ? prefix : "");
1003 if (keepmain || strNE(HvNAME(hv), "main")) {
1004 sv_catpv(sv,HvNAME(hv));
1005 sv_catpvn(sv,"::", 2);
1007 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
1011 Perl_gv_fullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
1013 HV *hv = GvSTASH(gv);
1018 sv_setpv(sv, prefix ? prefix : "");
1019 sv_catpv(sv,HvNAME(hv));
1020 sv_catpvn(sv,"::", 2);
1021 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
1025 Perl_gv_efullname4(pTHX_ SV *sv, GV *gv, const char *prefix, bool keepmain)
1027 GV *egv = GvEGV(gv);
1030 gv_fullname4(sv, egv, prefix, keepmain);
1034 Perl_gv_efullname3(pTHX_ SV *sv, GV *gv, const char *prefix)
1036 GV *egv = GvEGV(gv);
1039 gv_fullname3(sv, egv, prefix);
1042 /* XXX compatibility with versions <= 5.003. */
1044 Perl_gv_fullname(pTHX_ SV *sv, GV *gv)
1046 gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : "");
1049 /* XXX compatibility with versions <= 5.003. */
1051 Perl_gv_efullname(pTHX_ SV *sv, GV *gv)
1053 gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : "");
1062 io = (IO*)NEWSV(0,0);
1063 sv_upgrade((SV *)io,SVt_PVIO);
1066 iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV);
1067 /* unless exists($main::{FileHandle}) and defined(%main::FileHandle::) */
1068 if (!(iogv && GvHV(iogv) && HvARRAY(GvHV(iogv))))
1069 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
1070 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
1075 Perl_gv_check(pTHX_ HV *stash)
1082 if (!HvARRAY(stash))
1084 for (i = 0; i <= (I32) HvMAX(stash); i++) {
1085 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
1086 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
1087 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)))
1089 if (hv != PL_defstash && hv != stash)
1090 gv_check(hv); /* nested package */
1092 else if (isALPHA(*HeKEY(entry))) {
1094 gv = (GV*)HeVAL(entry);
1095 if (SvTYPE(gv) != SVt_PVGV || GvMULTI(gv))
1098 /* performance hack: if filename is absolute and it's a standard
1099 * module, don't bother warning */
1101 && PERL_FILE_IS_ABSOLUTE(file)
1102 #ifdef MACOS_TRADITIONAL
1103 && (instr(file, ":lib:")
1105 && (instr(file, "/lib/")
1107 || instr(file, ".pm")))
1111 CopLINE_set(PL_curcop, GvLINE(gv));
1113 CopFILE(PL_curcop) = file; /* set for warning */
1115 CopFILEGV(PL_curcop) = gv_fetchfile(file);
1117 Perl_warner(aTHX_ WARN_ONCE,
1118 "Name \"%s::%s\" used only once: possible typo",
1119 HvNAME(stash), GvNAME(gv));
1126 Perl_newGVgen(pTHX_ char *pack)
1128 return gv_fetchpv(Perl_form(aTHX_ "%s::_GEN_%ld", pack, (long)PL_gensym++),
1132 /* hopefully this is only called on local symbol table entries */
1135 Perl_gp_ref(pTHX_ GP *gp)
1142 /* multi-named GPs cannot be used for method cache */
1143 SvREFCNT_dec(gp->gp_cv);
1148 /* Adding a new name to a subroutine invalidates method cache */
1149 PL_sub_generation++;
1156 Perl_gp_free(pTHX_ GV *gv)
1160 if (!gv || !(gp = GvGP(gv)))
1162 if (gp->gp_refcnt == 0) {
1163 if (ckWARN_d(WARN_INTERNAL))
1164 Perl_warner(aTHX_ WARN_INTERNAL,
1165 "Attempt to free unreferenced glob pointers");
1169 /* Deleting the name of a subroutine invalidates method cache */
1170 PL_sub_generation++;
1172 if (--gp->gp_refcnt > 0) {
1173 if (gp->gp_egv == gv)
1178 SvREFCNT_dec(gp->gp_sv);
1179 SvREFCNT_dec(gp->gp_av);
1180 SvREFCNT_dec(gp->gp_hv);
1181 SvREFCNT_dec(gp->gp_io);
1182 SvREFCNT_dec(gp->gp_cv);
1183 SvREFCNT_dec(gp->gp_form);
1189 #if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
1193 #ifdef MICROPORT /* Microport 2.4 hack */
1197 if (GvGP(gv)->gp_av)
1198 return GvGP(gv)->gp_av;
1200 return GvGP(gv_AVadd(gv))->gp_av;
1206 if (GvGP(gv)->gp_hv)
1207 return GvGP(gv)->gp_hv;
1209 return GvGP(gv_HVadd(gv))->gp_hv;
1211 #endif /* Microport 2.4 hack */
1214 Perl_magic_freeovrld(pTHX_ SV *sv, MAGIC *mg)
1216 AMT *amtp = (AMT*)mg->mg_ptr;
1217 if (amtp && AMT_AMAGIC(amtp)) {
1219 for (i = 1; i < NofAMmeth; i++) {
1220 CV *cv = amtp->table[i];
1222 SvREFCNT_dec((SV *) cv);
1223 amtp->table[i] = Nullcv;
1230 /* Updates and caches the CV's */
1233 Perl_Gv_AMupdate(pTHX_ HV *stash)
1237 MAGIC* mg=mg_find((SV*)stash, PERL_MAGIC_overload_table);
1238 AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL;
1241 if (mg && amtp->was_ok_am == PL_amagic_generation
1242 && amtp->was_ok_sub == PL_sub_generation)
1243 return AMT_OVERLOADED(amtp);
1244 sv_unmagic((SV*)stash, PERL_MAGIC_overload_table);
1246 DEBUG_o( Perl_deb(aTHX_ "Recalcing overload magic in package %s\n",HvNAME(stash)) );
1249 amt.was_ok_am = PL_amagic_generation;
1250 amt.was_ok_sub = PL_sub_generation;
1251 amt.fallback = AMGfallNO;
1255 int filled = 0, have_ovl = 0;
1259 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
1261 /* Try to find via inheritance. */
1262 gv = gv_fetchmeth(stash, PL_AMG_names[0], 2, -1);
1267 lim = DESTROY_amg; /* Skip overloading entries. */
1268 else if (SvTRUE(sv))
1269 amt.fallback=AMGfallYES;
1271 amt.fallback=AMGfallNEVER;
1273 for (i = 1; i < lim; i++)
1274 amt.table[i] = Nullcv;
1275 for (; i < NofAMmeth; i++) {
1276 char *cooky = (char*)PL_AMG_names[i];
1277 /* Human-readable form, for debugging: */
1278 char *cp = (i >= DESTROY_amg ? cooky : AMG_id2name(i));
1279 STRLEN l = strlen(cooky);
1281 DEBUG_o( Perl_deb(aTHX_ "Checking overloading of `%s' in package `%.256s'\n",
1282 cp, HvNAME(stash)) );
1283 /* don't fill the cache while looking up! */
1284 gv = gv_fetchmeth(stash, cooky, l, -1);
1286 if (gv && (cv = GvCV(gv))) {
1287 if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil")
1288 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) {
1289 /* GvSV contains the name of the method. */
1292 DEBUG_o( Perl_deb(aTHX_ "Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n",
1293 SvPV_nolen(GvSV(gv)), cp, HvNAME(stash)) );
1294 if (!SvPOK(GvSV(gv))
1295 || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)),
1298 /* Can be an import stub (created by `can'). */
1300 Perl_croak(aTHX_ "Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'",
1301 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1304 Perl_croak(aTHX_ "Can't resolve method `%.256s' overloading `%s' in package `%.256s'",
1305 (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ),
1308 cv = GvCV(gv = ngv);
1310 DEBUG_o( Perl_deb(aTHX_ "Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n",
1311 cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))),
1312 GvNAME(CvGV(cv))) );
1314 if (i < DESTROY_amg)
1317 amt.table[i]=(CV*)SvREFCNT_inc(cv);
1320 AMT_AMAGIC_on(&amt);
1322 AMT_OVERLOADED_on(&amt);
1323 sv_magic((SV*)stash, 0, PERL_MAGIC_overload_table,
1324 (char*)&amt, sizeof(AMT));
1328 /* Here we have no table: */
1330 AMT_AMAGIC_off(&amt);
1331 sv_magic((SV*)stash, 0, PERL_MAGIC_overload_table,
1332 (char*)&amt, sizeof(AMTS));
1338 Perl_gv_handler(pTHX_ HV *stash, I32 id)
1345 mg = mg_find((SV*)stash, PERL_MAGIC_overload_table);
1349 mg = mg_find((SV*)stash, PERL_MAGIC_overload_table);
1351 amtp = (AMT*)mg->mg_ptr;
1352 if ( amtp->was_ok_am != PL_amagic_generation
1353 || amtp->was_ok_sub != PL_sub_generation )
1355 if (AMT_AMAGIC(amtp))
1356 return amtp->table[id];
1362 Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags)
1366 CV **cvp=NULL, **ocvp=NULL;
1367 AMT *amtp=NULL, *oamtp=NULL;
1368 int off=0, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
1369 int postpr = 0, force_cpy = 0, assignshift = assign ? 1 : 0;
1374 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
1375 && (stash = SvSTASH(SvRV(left)))
1376 && (mg = mg_find((SV*)stash, PERL_MAGIC_overload_table))
1377 && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1378 ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table
1380 && ((cv = cvp[off=method+assignshift])
1381 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
1387 cv = cvp[off=method])))) {
1388 lr = -1; /* Call method for left argument */
1390 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
1393 /* look for substituted methods */
1394 /* In all the covered cases we should be called with assign==0. */
1398 if ((cv = cvp[off=add_ass_amg])
1399 || ((cv = cvp[off = add_amg]) && (force_cpy = 0, postpr = 1))) {
1400 right = &PL_sv_yes; lr = -1; assign = 1;
1405 if ((cv = cvp[off = subtr_ass_amg])
1406 || ((cv = cvp[off = subtr_amg]) && (force_cpy = 0, postpr=1))) {
1407 right = &PL_sv_yes; lr = -1; assign = 1;
1411 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
1414 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1417 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1420 (void)((cv = cvp[off=bool__amg])
1421 || (cv = cvp[off=numer_amg])
1422 || (cv = cvp[off=string_amg]));
1428 * SV* ref causes confusion with the interpreter variable of
1431 SV* tmpRef=SvRV(left);
1432 if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) {
1434 * Just to be extra cautious. Maybe in some
1435 * additional cases sv_setsv is safe, too.
1437 SV* newref = newSVsv(tmpRef);
1438 SvOBJECT_on(newref);
1439 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(tmpRef));
1445 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
1446 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
1447 SV* nullsv=sv_2mortal(newSViv(0));
1449 SV* lessp = amagic_call(left,nullsv,
1450 lt_amg,AMGf_noright);
1451 logic = SvTRUE(lessp);
1453 SV* lessp = amagic_call(left,nullsv,
1454 ncmp_amg,AMGf_noright);
1455 logic = (SvNV(lessp) < 0);
1458 if (off==subtr_amg) {
1469 if ((cv = cvp[off=subtr_amg])) {
1471 left = sv_2mortal(newSViv(0));
1476 case iter_amg: /* XXXX Eventually should do to_gv. */
1478 return NULL; /* Delegate operation to standard mechanisms. */
1486 return left; /* Delegate operation to standard mechanisms. */
1491 if (!cv) goto not_found;
1492 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1493 && (stash = SvSTASH(SvRV(right)))
1494 && (mg = mg_find((SV*)stash, PERL_MAGIC_overload_table))
1495 && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
1496 ? (amtp = (AMT*)mg->mg_ptr)->table
1498 && (cv = cvp[off=method])) { /* Method for right
1501 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
1502 && (cvp=ocvp) && (lr = -1))
1503 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1504 && !(flags & AMGf_unary)) {
1505 /* We look for substitution for
1506 * comparison operations and
1508 if (method==concat_amg || method==concat_ass_amg
1509 || method==repeat_amg || method==repeat_ass_amg) {
1510 return NULL; /* Delegate operation to string conversion */
1520 postpr = 1; off=ncmp_amg; break;
1527 postpr = 1; off=scmp_amg; break;
1529 if (off != -1) cv = cvp[off];
1534 not_found: /* No method found, either report or croak */
1542 return left; /* Delegate operation to standard mechanisms. */
1545 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1546 notfound = 1; lr = -1;
1547 } else if (cvp && (cv=cvp[nomethod_amg])) {
1548 notfound = 1; lr = 1;
1551 if (off==-1) off=method;
1552 msg = sv_2mortal(Perl_newSVpvf(aTHX_
1553 "Operation `%s': no method found,%sargument %s%s%s%s",
1554 AMG_id2name(method + assignshift),
1555 (flags & AMGf_unary ? " " : "\n\tleft "),
1557 "in overloaded package ":
1558 "has no overloaded magic",
1560 HvNAME(SvSTASH(SvRV(left))):
1563 ",\n\tright argument in overloaded package ":
1566 : ",\n\tright argument has no overloaded magic"),
1568 HvNAME(SvSTASH(SvRV(right))):
1570 if (amtp && amtp->fallback >= AMGfallYES) {
1571 DEBUG_o( Perl_deb(aTHX_ "%s", SvPVX(msg)) );
1573 Perl_croak(aTHX_ "%"SVf, msg);
1577 force_cpy = force_cpy || assign;
1582 DEBUG_o(Perl_deb(aTHX_
1583 "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n",
1585 method+assignshift==off? "" :
1587 method+assignshift==off? "" :
1588 AMG_id2name(method+assignshift),
1589 method+assignshift==off? "" : "')",
1590 flags & AMGf_unary? "" :
1591 lr==1 ? " for right argument": " for left argument",
1592 flags & AMGf_unary? " for argument" : "",
1593 stash ? HvNAME(stash) : "null",
1594 fl? ",\n\tassignment variant used": "") );
1597 /* Since we use shallow copy during assignment, we need
1598 * to dublicate the contents, probably calling user-supplied
1599 * version of copy operator
1601 /* We need to copy in following cases:
1602 * a) Assignment form was called.
1603 * assignshift==1, assign==T, method + 1 == off
1604 * b) Increment or decrement, called directly.
1605 * assignshift==0, assign==0, method + 0 == off
1606 * c) Increment or decrement, translated to assignment add/subtr.
1607 * assignshift==0, assign==T,
1609 * d) Increment or decrement, translated to nomethod.
1610 * assignshift==0, assign==0,
1612 * e) Assignment form translated to nomethod.
1613 * assignshift==1, assign==T, method + 1 != off
1616 /* off is method, method+assignshift, or a result of opcode substitution.
1617 * In the latter case assignshift==0, so only notfound case is important.
1619 if (( (method + assignshift == off)
1620 && (assign || (method == inc_amg) || (method == dec_amg)))
1627 bool oldcatch = CATCH_GET;
1630 Zero(&myop, 1, BINOP);
1631 myop.op_last = (OP *) &myop;
1632 myop.op_next = Nullop;
1633 myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED;
1635 PUSHSTACKi(PERLSI_OVERLOAD);
1638 PL_op = (OP *) &myop;
1639 if (PERLDB_SUB && PL_curstash != PL_debstash)
1640 PL_op->op_private |= OPpENTERSUB_DB;
1644 EXTEND(SP, notfound + 5);
1645 PUSHs(lr>0? right: left);
1646 PUSHs(lr>0? left: right);
1647 PUSHs( lr > 0 ? &PL_sv_yes : ( assign ? &PL_sv_undef : &PL_sv_no ));
1649 PUSHs( sv_2mortal(newSVpv(AMG_id2name(method + assignshift),0)));
1654 if ((PL_op = Perl_pp_entersub(aTHX)))
1662 CATCH_SET(oldcatch);
1669 ans=SvIV(res)<=0; break;
1672 ans=SvIV(res)<0; break;
1675 ans=SvIV(res)>=0; break;
1678 ans=SvIV(res)>0; break;
1681 ans=SvIV(res)==0; break;
1684 ans=SvIV(res)!=0; break;
1687 SvSetSV(left,res); return left;
1689 ans=!SvTRUE(res); break;
1692 } else if (method==copy_amg) {
1694 Perl_croak(aTHX_ "Copy method did not return a reference");
1696 return SvREFCNT_inc(SvRV(res));
1704 =for apidoc is_gv_magical
1706 Returns C<TRUE> if given the name of a magical GV.
1708 Currently only useful internally when determining if a GV should be
1709 created even in rvalue contexts.
1711 C<flags> is not used at present but available for future extension to
1712 allow selecting particular classes of magical variable.
1717 Perl_is_gv_magical(pTHX_ char *name, STRLEN len, U32 flags)
1724 if (len == 3 && strEQ(name, "ISA"))
1728 if (len == 8 && strEQ(name, "OVERLOAD"))
1732 if (len == 3 && strEQ(name, "SIG"))
1735 case '\017': /* $^O & $^OPEN */
1737 || (len == 4 && strEQ(name, "\027PEN")))
1742 case '\027': /* $^W & $^WARNING_BITS */
1744 || (len == 12 && strEQ(name, "\027ARNING_BITS"))
1745 || (len == 17 && strEQ(name, "\027IDE_SYSTEM_CALLS")))
1777 case '\001': /* $^A */
1778 case '\003': /* $^C */
1779 case '\004': /* $^D */
1780 case '\005': /* $^E */
1781 case '\006': /* $^F */
1782 case '\010': /* $^H */
1783 case '\011': /* $^I, NOT \t in EBCDIC */
1784 case '\014': /* $^L */
1785 case '\016': /* $^N */
1786 case '\020': /* $^P */
1787 case '\023': /* $^S */
1788 case '\024': /* $^T */
1789 case '\026': /* $^V */
1803 char *end = name + len;
1804 while (--end > name) {