3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * 'Mercy!' cried Gandalf. 'If the giving of information is to be the cure
13 * of your inquisitiveness, I shall spend all the rest of my days in answering
14 * you. What more do you want to know?'
15 * 'The names of all the stars, and of all living things, and the whole
16 * history of Middle-earth and Over-heaven and of the Sundering Seas,'
19 * [p.599 of _The Lord of the Rings_, III/xi: "The PalantÃr"]
25 A GV is a structure which corresponds to to a Perl typeglob, ie *foo.
26 It is a structure that holds a pointer to a scalar, an array, a hash etc,
27 corresponding to $foo, @foo, %foo.
29 GVs are usually found as values in stashes (symbol table hashes) where
30 Perl stores its global variables.
41 static const char S_autoload[] = "AUTOLOAD";
42 static const STRLEN S_autolen = sizeof(S_autoload)-1;
45 Perl_gv_add_by_type(pTHX_ GV *gv, svtype type)
52 SvTYPE((const SV *)gv) != SVt_PVGV
53 && SvTYPE((const SV *)gv) != SVt_PVLV
57 if (type == SVt_PVIO) {
59 * if it walks like a dirhandle, then let's assume that
60 * this is a dirhandle.
62 what = OP_IS_DIRHOP(PL_op->op_type) ?
63 "dirhandle" : "filehandle";
64 /* diag_listed_as: Bad symbol for filehandle */
65 } else if (type == SVt_PVHV) {
68 what = type == SVt_PVAV ? "array" : "scalar";
70 Perl_croak(aTHX_ "Bad symbol for %s", what);
73 if (type == SVt_PVHV) {
74 where = (SV **)&GvHV(gv);
75 } else if (type == SVt_PVAV) {
76 where = (SV **)&GvAV(gv);
77 } else if (type == SVt_PVIO) {
78 where = (SV **)&GvIOp(gv);
84 *where = newSV_type(type);
89 Perl_gv_fetchfile(pTHX_ const char *name)
91 PERL_ARGS_ASSERT_GV_FETCHFILE;
92 return gv_fetchfile_flags(name, strlen(name), 0);
96 Perl_gv_fetchfile_flags(pTHX_ const char *const name, const STRLEN namelen,
102 const STRLEN tmplen = namelen + 2;
105 PERL_ARGS_ASSERT_GV_FETCHFILE_FLAGS;
106 PERL_UNUSED_ARG(flags);
111 if (tmplen <= sizeof smallbuf)
114 Newx(tmpbuf, tmplen, char);
115 /* This is where the debugger's %{"::_<$filename"} hash is created */
118 memcpy(tmpbuf + 2, name, namelen);
119 gv = *(GV**)hv_fetch(PL_defstash, tmpbuf, tmplen, TRUE);
121 gv_init(gv, PL_defstash, tmpbuf, tmplen, FALSE);
122 #ifdef PERL_DONT_CREATE_GVSV
123 GvSV(gv) = newSVpvn(name, namelen);
125 sv_setpvn(GvSV(gv), name, namelen);
128 if ((PERLDB_LINE || PERLDB_SAVESRC) && !GvAV(gv))
129 hv_magic(GvHVn(gv_AVadd(gv)), NULL, PERL_MAGIC_dbfile);
130 if (tmpbuf != smallbuf)
136 =for apidoc gv_const_sv
138 If C<gv> is a typeglob whose subroutine entry is a constant sub eligible for
139 inlining, or C<gv> is a placeholder reference that would be promoted to such
140 a typeglob, then returns the value returned by the sub. Otherwise, returns
147 Perl_gv_const_sv(pTHX_ GV *gv)
149 PERL_ARGS_ASSERT_GV_CONST_SV;
151 if (SvTYPE(gv) == SVt_PVGV)
152 return cv_const_sv(GvCVu(gv));
153 return SvROK(gv) ? SvRV(gv) : NULL;
157 Perl_newGP(pTHX_ GV *const gv)
162 const char *const file
163 = (PL_curcop && CopFILE(PL_curcop)) ? CopFILE(PL_curcop) : "";
164 const STRLEN len = strlen(file);
166 SV *const temp_sv = CopFILESV(PL_curcop);
170 PERL_ARGS_ASSERT_NEWGP;
173 file = SvPVX(temp_sv);
174 len = SvCUR(temp_sv);
181 PERL_HASH(hash, file, len);
185 #ifndef PERL_DONT_CREATE_GVSV
186 gp->gp_sv = newSV(0);
189 gp->gp_line = PL_curcop ? CopLINE(PL_curcop) : 0;
190 /* XXX Ideally this cast would be replaced with a change to const char*
192 gp->gp_file_hek = share_hek(file, len, hash);
199 /* Assign CvGV(cv) = gv, handling weak references.
200 * See also S_anonymise_cv_maybe */
203 Perl_cvgv_set(pTHX_ CV* cv, GV* gv)
205 GV * const oldgv = CvGV(cv);
206 PERL_ARGS_ASSERT_CVGV_SET;
217 sv_del_backref(MUTABLE_SV(oldgv), MUTABLE_SV(cv));
221 SvANY(cv)->xcv_gv = gv;
222 assert(!CvCVGV_RC(cv));
227 if (isGV_with_GP(gv) && GvGP(gv) && (GvCV(gv) == cv || GvFORM(gv) == cv))
228 Perl_sv_add_backref(aTHX_ MUTABLE_SV(gv), MUTABLE_SV(cv));
231 SvREFCNT_inc_simple_void_NN(gv);
235 /* Assign CvSTASH(cv) = st, handling weak references. */
238 Perl_cvstash_set(pTHX_ CV *cv, HV *st)
240 HV *oldst = CvSTASH(cv);
241 PERL_ARGS_ASSERT_CVSTASH_SET;
245 sv_del_backref(MUTABLE_SV(oldst), MUTABLE_SV(cv));
246 SvANY(cv)->xcv_stash = st;
248 Perl_sv_add_backref(aTHX_ MUTABLE_SV(st), MUTABLE_SV(cv));
252 =for apidoc gv_init_pvn
254 Converts a scalar into a typeglob. This is an incoercible typeglob;
255 assigning a reference to it will assign to one of its slots, instead of
256 overwriting it as happens with typeglobs created by SvSetSV. Converting
257 any scalar that is SvOK() may produce unpredictable results and is reserved
258 for perl's internal use.
260 C<gv> is the scalar to be converted.
262 C<stash> is the parent stash/package, if any.
264 C<name> and C<len> give the name. The name must be unqualified;
265 that is, it must not include the package name. If C<gv> is a
266 stash element, it is the caller's responsibility to ensure that the name
267 passed to this function matches the name of the element. If it does not
268 match, perl's internal bookkeeping will get out of sync.
270 C<flags> can be set to SVf_UTF8 if C<name> is a UTF8 string, or
271 the return value of SvUTF8(sv). It can also take the
272 GV_ADDMULTI flag, which means to pretend that the GV has been
273 seen before (i.e., suppress "Used once" warnings).
277 The old form of gv_init_pvn(). It does not work with UTF8 strings, as it
278 has no flags parameter. If the C<multi> parameter is set, the
279 GV_ADDMULTI flag will be passed to gv_init_pvn().
281 =for apidoc gv_init_pv
283 Same as gv_init_pvn(), but takes a nul-terminated string for the name
284 instead of separate char * and length parameters.
286 =for apidoc gv_init_sv
288 Same as gv_init_pvn(), but takes an SV * for the name instead of separate
289 char * and length parameters. C<flags> is currently unused.
295 Perl_gv_init_sv(pTHX_ GV *gv, HV *stash, SV* namesv, U32 flags)
299 PERL_ARGS_ASSERT_GV_INIT_SV;
300 namepv = SvPV(namesv, namelen);
303 gv_init_pvn(gv, stash, namepv, namelen, flags);
307 Perl_gv_init_pv(pTHX_ GV *gv, HV *stash, const char *name, U32 flags)
309 PERL_ARGS_ASSERT_GV_INIT_PV;
310 gv_init_pvn(gv, stash, name, strlen(name), flags);
314 Perl_gv_init_pvn(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len, U32 flags)
317 const U32 old_type = SvTYPE(gv);
318 const bool doproto = old_type > SVt_NULL;
319 char * const proto = (doproto && SvPOK(gv)) ? SvPVX(gv) : NULL;
320 const STRLEN protolen = proto ? SvCUR(gv) : 0;
321 SV *const has_constant = doproto && SvROK(gv) ? SvRV(gv) : NULL;
322 const U32 exported_constant = has_constant ? SvPCS_IMPORTED(gv) : 0;
324 PERL_ARGS_ASSERT_GV_INIT_PVN;
325 assert (!(proto && has_constant));
328 /* The constant has to be a simple scalar type. */
329 switch (SvTYPE(has_constant)) {
335 Perl_croak(aTHX_ "Cannot convert a reference to %s to typeglob",
336 sv_reftype(has_constant, 0));
344 if (old_type < SVt_PVGV) {
345 if (old_type >= SVt_PV)
347 sv_upgrade(MUTABLE_SV(gv), SVt_PVGV);
355 Safefree(SvPVX_mutable(gv));
360 GvGP_set(gv, Perl_newGP(aTHX_ gv));
363 Perl_sv_add_backref(aTHX_ MUTABLE_SV(stash), MUTABLE_SV(gv));
364 gv_name_set(gv, name, len, GV_ADD | ( flags & SVf_UTF8 ? SVf_UTF8 : 0 ));
365 if (flags & GV_ADDMULTI || doproto) /* doproto means it */
366 GvMULTI_on(gv); /* _was_ mentioned */
367 if (doproto) { /* Replicate part of newSUB here. */
373 /* newCONSTSUB doesn't take a len arg, so make sure we
374 * give it a \0-terminated string */
375 name0 = savepvn(name,len);
377 /* newCONSTSUB takes ownership of the reference from us. */
378 cv = newCONSTSUB_flags(stash, (name0 ? name0 : name), flags, has_constant);
379 /* In case op.c:S_process_special_blocks stole it: */
381 GvCV_set(gv, (CV *)SvREFCNT_inc_simple_NN(cv));
382 assert(GvCV(gv) == cv); /* newCONSTSUB should have set this */
385 /* If this reference was a copy of another, then the subroutine
386 must have been "imported", by a Perl space assignment to a GV
387 from a reference to CV. */
388 if (exported_constant)
389 GvIMPORTED_CV_on(gv);
391 (void) start_subparse(0,0); /* Create empty CV in compcv. */
397 mro_method_changed_in(GvSTASH(gv)); /* sub Foo::bar($) { (shift) } sub ASDF::baz($); *ASDF::baz = \&Foo::bar */
399 CvFILE_set_from_cop(cv, PL_curcop);
400 CvSTASH_set(cv, PL_curstash);
402 sv_usepvn_flags(MUTABLE_SV(cv), proto, protolen,
403 SV_HAS_TRAILING_NUL);
409 S_gv_init_svtype(pTHX_ GV *gv, const svtype sv_type)
411 PERL_ARGS_ASSERT_GV_INIT_SVTYPE;
423 #ifdef PERL_DONT_CREATE_GVSV
431 /* Work round what appears to be a bug in Sun C++ 5.8 2005/10/13
432 If we just cast GvSVn(gv) to void, it ignores evaluating it for
439 static void core_xsub(pTHX_ CV* cv);
442 S_maybe_add_coresub(pTHX_ HV * const stash, GV *gv,
443 const char * const name, const STRLEN len,
444 const char * const fullname, STRLEN const fullen)
446 const int code = keyword(name, len, 1);
447 static const char file[] = __FILE__;
451 bool ampable = TRUE; /* &{}-able */
453 yy_parser *oldparser;
458 assert(stash || fullname);
460 if (!fullname && !HvENAME(stash)) return NULL; /* pathological case
462 inlining newATTRSUB */
463 if (code >= 0) return NULL; /* not overridable */
465 /* no support for \&CORE::infix;
466 no support for funcs that take labels, as their parsing is
468 case KEY_and: case KEY_cmp: case KEY_CORE: case KEY_dump:
469 case KEY_eq: case KEY_ge:
470 case KEY_gt: case KEY_le: case KEY_lt: case KEY_ne:
471 case KEY_or: case KEY_x: case KEY_xor:
474 case KEY_chomp: case KEY_chop:
475 case KEY_each: case KEY_eof: case KEY_exec:
484 case KEY_truncate: case KEY_unlink:
491 gv_init(gv, stash, name, len, TRUE);
495 oldcurcop = PL_curcop;
496 oldparser = PL_parser;
497 lex_start(NULL, NULL, 0);
498 oldcompcv = PL_compcv;
499 PL_compcv = NULL; /* Prevent start_subparse from setting
501 oldsavestack_ix = start_subparse(FALSE,0);
505 /* Avoid calling newXS, as it calls us, and things start to
507 cv = MUTABLE_CV(newSV_type(SVt_PVCV));
510 mro_method_changed_in(GvSTASH(gv));
512 CvXSUB(cv) = core_xsub;
514 CvGV_set(cv, gv); /* This stops new ATTRSUB from setting CvFILE
516 (void)gv_fetchfile(file);
517 CvFILE(cv) = (char *)file;
518 /* XXX This is inefficient, as doing things this order causes
519 a prototype check in newATTRSUB. But we have to do
520 it this order as we need an op number before calling
522 (void)core_prototype((SV *)cv, name, code, &opnum);
523 if (stash && (fullname || !fullen))
524 (void)hv_store(stash,name,len,(SV *)gv,0);
529 tmpstr = newSVhek(HvENAME_HEK(stash));
530 sv_catpvs(tmpstr, "::");
531 sv_catpvn(tmpstr,name,len);
533 else tmpstr = newSVpvn_share(fullname,fullen,0);
534 newATTRSUB(oldsavestack_ix,
535 newSVOP(OP_CONST, 0, tmpstr),
540 : newSVpvn(name,len),
544 assert(GvCV(gv) == cv);
545 if (opnum != OP_VEC && opnum != OP_SUBSTR)
546 CvLVALUE_off(cv); /* Now *that* was a neat trick. */
548 PL_parser = oldparser;
549 PL_curcop = oldcurcop;
550 PL_compcv = oldcompcv;
552 opnumsv = opnum ? newSVuv((UV)opnum) : (SV *)NULL;
554 cv, Perl_ck_entersub_args_core, opnumsv ? opnumsv : (SV *)cv
556 SvREFCNT_dec(opnumsv);
561 =for apidoc gv_fetchmeth
563 Like L</gv_fetchmeth_pvn>, but lacks a flags parameter.
565 =for apidoc gv_fetchmeth_sv
567 Exactly like L</gv_fetchmeth_pvn>, but takes the name string in the form
568 of an SV instead of a string/length pair.
574 Perl_gv_fetchmeth_sv(pTHX_ HV *stash, SV *namesv, I32 level, U32 flags)
578 PERL_ARGS_ASSERT_GV_FETCHMETH_SV;
579 namepv = SvPV(namesv, namelen);
582 return gv_fetchmeth_pvn(stash, namepv, namelen, level, flags);
586 =for apidoc gv_fetchmeth_pv
588 Exactly like L</gv_fetchmeth_pvn>, but takes a nul-terminated string
589 instead of a string/length pair.
595 Perl_gv_fetchmeth_pv(pTHX_ HV *stash, const char *name, I32 level, U32 flags)
597 PERL_ARGS_ASSERT_GV_FETCHMETH_PV;
598 return gv_fetchmeth_pvn(stash, name, strlen(name), level, flags);
602 =for apidoc gv_fetchmeth_pvn
604 Returns the glob with the given C<name> and a defined subroutine or
605 C<NULL>. The glob lives in the given C<stash>, or in the stashes
606 accessible via @ISA and UNIVERSAL::.
608 The argument C<level> should be either 0 or -1. If C<level==0>, as a
609 side-effect creates a glob with the given C<name> in the given C<stash>
610 which in the case of success contains an alias for the subroutine, and sets
611 up caching info for this glob.
613 Currently, the only significant value for C<flags> is SVf_UTF8.
615 This function grants C<"SUPER"> token as a postfix of the stash name. The
616 GV returned from C<gv_fetchmeth> may be a method cache entry, which is not
617 visible to Perl code. So when calling C<call_sv>, you should not use
618 the GV directly; instead, you should use the method's CV, which can be
619 obtained from the GV with the C<GvCV> macro.
624 /* NOTE: No support for tied ISA */
627 Perl_gv_fetchmeth_pvn(pTHX_ HV *stash, const char *name, STRLEN len, I32 level, U32 flags)
635 GV* candidate = NULL;
639 I32 create = (level >= 0) ? 1 : 0;
643 U32 is_utf8 = flags & SVf_UTF8;
645 PERL_ARGS_ASSERT_GV_FETCHMETH_PVN;
647 /* UNIVERSAL methods should be callable without a stash */
649 create = 0; /* probably appropriate */
650 if(!(stash = gv_stashpvs("UNIVERSAL", 0)))
656 hvname = HvNAME_get(stash);
658 Perl_croak(aTHX_ "Can't use anonymous symbol table for method lookup");
663 DEBUG_o( Perl_deb(aTHX_ "Looking for method %s in package %s\n",name,hvname) );
665 topgen_cmp = HvMROMETA(stash)->cache_gen + PL_sub_generation;
667 /* check locally for a real method or a cache entry */
668 gvp = (GV**)hv_fetch(stash, name, is_utf8 ? -len : len, create);
673 if (SvTYPE(topgv) != SVt_PVGV)
674 gv_init_pvn(topgv, stash, name, len, GV_ADDMULTI|is_utf8);
675 if ((cand_cv = GvCV(topgv))) {
676 /* If genuine method or valid cache entry, use it */
677 if (!GvCVGEN(topgv) || GvCVGEN(topgv) == topgen_cmp) {
681 /* stale cache entry, junk it and move on */
682 SvREFCNT_dec(cand_cv);
683 GvCV_set(topgv, NULL);
688 else if (GvCVGEN(topgv) == topgen_cmp) {
689 /* cache indicates no such method definitively */
692 else if (len > 1 /* shortest is uc */ && HvNAMELEN_get(stash) == 4
693 && strnEQ(hvname, "CORE", 4)
694 && S_maybe_add_coresub(aTHX_ stash,topgv,name,len,0,1))
698 packlen = HvNAMELEN_get(stash);
699 if (packlen >= 7 && strEQ(hvname + packlen - 7, "::SUPER")) {
702 basestash = gv_stashpvn(hvname, packlen,
703 GV_ADD | (HvNAMEUTF8(stash) ? SVf_UTF8 : 0));
704 linear_av = mro_get_linear_isa(basestash);
707 linear_av = mro_get_linear_isa(stash); /* has ourselves at the top of the list */
710 linear_svp = AvARRAY(linear_av) + 1; /* skip over self */
711 items = AvFILLp(linear_av); /* no +1, to skip over self */
713 linear_sv = *linear_svp++;
715 cstash = gv_stashsv(linear_sv, 0);
718 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Can't locate package %"SVf" for @%s::ISA",
719 SVfARG(linear_sv), hvname);
725 gvp = (GV**)hv_fetch(cstash, name, is_utf8 ? -len : len, 0);
727 if (len > 1 && HvNAMELEN_get(cstash) == 4) {
728 const char *hvname = HvNAME(cstash); assert(hvname);
729 if (strnEQ(hvname, "CORE", 4)
731 S_maybe_add_coresub(aTHX_ cstash,NULL,name,len,0,0)
737 else candidate = *gvp;
740 if (SvTYPE(candidate) != SVt_PVGV)
741 gv_init_pvn(candidate, cstash, name, len, GV_ADDMULTI|is_utf8);
742 if (SvTYPE(candidate) == SVt_PVGV && (cand_cv = GvCV(candidate)) && !GvCVGEN(candidate)) {
744 * Found real method, cache method in topgv if:
745 * 1. topgv has no synonyms (else inheritance crosses wires)
746 * 2. method isn't a stub (else AUTOLOAD fails spectacularly)
748 if (topgv && (GvREFCNT(topgv) == 1) && (CvROOT(cand_cv) || CvXSUB(cand_cv))) {
749 CV *old_cv = GvCV(topgv);
750 SvREFCNT_dec(old_cv);
751 SvREFCNT_inc_simple_void_NN(cand_cv);
752 GvCV_set(topgv, cand_cv);
753 GvCVGEN(topgv) = topgen_cmp;
759 /* Check UNIVERSAL without caching */
760 if(level == 0 || level == -1) {
761 candidate = gv_fetchmeth_pvn(NULL, name, len, 1, flags);
763 cand_cv = GvCV(candidate);
764 if (topgv && (GvREFCNT(topgv) == 1) && (CvROOT(cand_cv) || CvXSUB(cand_cv))) {
765 CV *old_cv = GvCV(topgv);
766 SvREFCNT_dec(old_cv);
767 SvREFCNT_inc_simple_void_NN(cand_cv);
768 GvCV_set(topgv, cand_cv);
769 GvCVGEN(topgv) = topgen_cmp;
775 if (topgv && GvREFCNT(topgv) == 1) {
776 /* cache the fact that the method is not defined */
777 GvCVGEN(topgv) = topgen_cmp;
784 =for apidoc gv_fetchmeth_autoload
786 This is the old form of L</gv_fetchmeth_pvn_autoload>, which has no flags
789 =for apidoc gv_fetchmeth_sv_autoload
791 Exactly like L</gv_fetchmeth_pvn_autoload>, but takes the name string in the form
792 of an SV instead of a string/length pair.
798 Perl_gv_fetchmeth_sv_autoload(pTHX_ HV *stash, SV *namesv, I32 level, U32 flags)
802 PERL_ARGS_ASSERT_GV_FETCHMETH_SV_AUTOLOAD;
803 namepv = SvPV(namesv, namelen);
806 return gv_fetchmeth_pvn_autoload(stash, namepv, namelen, level, flags);
810 =for apidoc gv_fetchmeth_pv_autoload
812 Exactly like L</gv_fetchmeth_pvn_autoload>, but takes a nul-terminated string
813 instead of a string/length pair.
819 Perl_gv_fetchmeth_pv_autoload(pTHX_ HV *stash, const char *name, I32 level, U32 flags)
821 PERL_ARGS_ASSERT_GV_FETCHMETH_PV_AUTOLOAD;
822 return gv_fetchmeth_pvn_autoload(stash, name, strlen(name), level, flags);
826 =for apidoc gv_fetchmeth_pvn_autoload
828 Same as gv_fetchmeth_pvn(), but looks for autoloaded subroutines too.
829 Returns a glob for the subroutine.
831 For an autoloaded subroutine without a GV, will create a GV even
832 if C<level < 0>. For an autoloaded subroutine without a stub, GvCV()
833 of the result may be zero.
835 Currently, the only significant value for C<flags> is SVf_UTF8.
841 Perl_gv_fetchmeth_pvn_autoload(pTHX_ HV *stash, const char *name, STRLEN len, I32 level, U32 flags)
843 GV *gv = gv_fetchmeth_pvn(stash, name, len, level, 0);
845 PERL_ARGS_ASSERT_GV_FETCHMETH_PVN_AUTOLOAD;
852 return NULL; /* UNIVERSAL::AUTOLOAD could cause trouble */
853 if (len == S_autolen && memEQ(name, S_autoload, S_autolen))
855 if (!(gv = gv_fetchmeth_pvn(stash, S_autoload, S_autolen, FALSE, flags)))
858 if (!(CvROOT(cv) || CvXSUB(cv)))
860 /* Have an autoload */
861 if (level < 0) /* Cannot do without a stub */
862 gv_fetchmeth_pvn(stash, name, len, 0, flags);
863 gvp = (GV**)hv_fetch(stash, name, len, (level >= 0));
872 =for apidoc gv_fetchmethod_autoload
874 Returns the glob which contains the subroutine to call to invoke the method
875 on the C<stash>. In fact in the presence of autoloading this may be the
876 glob for "AUTOLOAD". In this case the corresponding variable $AUTOLOAD is
879 The third parameter of C<gv_fetchmethod_autoload> determines whether
880 AUTOLOAD lookup is performed if the given method is not present: non-zero
881 means yes, look for AUTOLOAD; zero means no, don't look for AUTOLOAD.
882 Calling C<gv_fetchmethod> is equivalent to calling C<gv_fetchmethod_autoload>
883 with a non-zero C<autoload> parameter.
885 These functions grant C<"SUPER"> token as a prefix of the method name. Note
886 that if you want to keep the returned glob for a long time, you need to
887 check for it being "AUTOLOAD", since at the later time the call may load a
888 different subroutine due to $AUTOLOAD changing its value. Use the glob
889 created via a side effect to do this.
891 These functions have the same side-effects and as C<gv_fetchmeth> with
892 C<level==0>. C<name> should be writable if contains C<':'> or C<'
893 ''>. The warning against passing the GV returned by C<gv_fetchmeth> to
894 C<call_sv> apply equally to these functions.
900 S_gv_get_super_pkg(pTHX_ const char* name, I32 namelen, U32 flags)
907 PERL_ARGS_ASSERT_GV_GET_SUPER_PKG;
909 stash = gv_stashpvn(name, namelen, 0);
910 if(stash) return stash;
912 /* If we must create it, give it an @ISA array containing
913 the real package this SUPER is for, so that it's tied
914 into the cache invalidation code correctly */
915 stash = gv_stashpvn(name, namelen, GV_ADD);
916 gvp = (GV**)hv_fetchs(stash, "ISA", TRUE);
918 gv_init_pvn(gv, stash, "ISA", 3, GV_ADDMULTI|(flags & SVf_UTF8));
919 superisa = GvAVn(gv);
921 sv_magic(MUTABLE_SV(superisa), MUTABLE_SV(gv), PERL_MAGIC_isa, NULL, 0);
923 av_push(superisa, newSVpv(CopSTASHPV(PL_curcop), 0));
925 av_push(superisa, newSVhek(CopSTASH(PL_curcop)
926 ? HvNAME_HEK(CopSTASH(PL_curcop)) : NULL));
933 Perl_gv_fetchmethod_autoload(pTHX_ HV *stash, const char *name, I32 autoload)
935 PERL_ARGS_ASSERT_GV_FETCHMETHOD_AUTOLOAD;
937 return gv_fetchmethod_flags(stash, name, autoload ? GV_AUTOLOAD : 0);
941 Perl_gv_fetchmethod_sv_flags(pTHX_ HV *stash, SV *namesv, U32 flags)
945 PERL_ARGS_ASSERT_GV_FETCHMETHOD_SV_FLAGS;
946 namepv = SvPV(namesv, namelen);
949 return gv_fetchmethod_pvn_flags(stash, namepv, namelen, flags);
953 Perl_gv_fetchmethod_pv_flags(pTHX_ HV *stash, const char *name, U32 flags)
955 PERL_ARGS_ASSERT_GV_FETCHMETHOD_PV_FLAGS;
956 return gv_fetchmethod_pvn_flags(stash, name, strlen(name), flags);
959 /* Don't merge this yet, as it's likely to get a len parameter, and possibly
962 Perl_gv_fetchmethod_pvn_flags(pTHX_ HV *stash, const char *name, const STRLEN len, U32 flags)
965 register const char *nend;
966 const char *nsplit = NULL;
969 const char * const origname = name;
970 SV *const error_report = MUTABLE_SV(stash);
971 const U32 autoload = flags & GV_AUTOLOAD;
972 const U32 do_croak = flags & GV_CROAK;
974 PERL_ARGS_ASSERT_GV_FETCHMETHOD_PVN_FLAGS;
976 if (SvTYPE(stash) < SVt_PVHV)
979 /* The only way stash can become NULL later on is if nsplit is set,
980 which in turn means that there is no need for a SVt_PVHV case
981 the error reporting code. */
984 for (nend = name; *nend || nend != (origname + len); nend++) {
989 else if (*nend == ':' && *(nend + 1) == ':') {
995 if ((nsplit - origname) == 5 && memEQ(origname, "SUPER", 5)) {
996 /* ->SUPER::method should really be looked up in original stash */
997 SV * const tmpstr = sv_2mortal(Perl_newSVpvf(aTHX_ "%s::SUPER",
998 CopSTASHPV(PL_curcop)));
999 /* __PACKAGE__::SUPER stash should be autovivified */
1000 stash = gv_get_super_pkg(SvPVX_const(tmpstr), SvCUR(tmpstr), SvUTF8(tmpstr));
1001 DEBUG_o( Perl_deb(aTHX_ "Treating %s as %s::%s\n",
1002 origname, HvNAME_get(stash), name) );
1005 /* don't autovifify if ->NoSuchStash::method */
1006 stash = gv_stashpvn(origname, nsplit - origname, 0);
1008 /* however, explicit calls to Pkg::SUPER::method may
1009 happen, and may require autovivification to work */
1010 if (!stash && (nsplit - origname) >= 7 &&
1011 strnEQ(nsplit - 7, "::SUPER", 7) &&
1012 gv_stashpvn(origname, nsplit - origname - 7, 0))
1013 stash = gv_get_super_pkg(origname, nsplit - origname, flags);
1018 gv = gv_fetchmeth_pvn(stash, name, nend - name, 0, 0);
1020 if (strEQ(name,"import") || strEQ(name,"unimport"))
1021 gv = MUTABLE_GV(&PL_sv_yes);
1023 gv = gv_autoload_pvn(
1024 ostash, name, nend - name, GV_AUTOLOAD_ISMETHOD|flags
1026 if (!gv && do_croak) {
1027 /* Right now this is exclusively for the benefit of S_method_common
1030 /* If we can't find an IO::File method, it might be a call on
1031 * a filehandle. If IO:File has not been loaded, try to
1032 * require it first instead of croaking */
1033 const char *stash_name = HvNAME_get(stash);
1034 if (stash_name && memEQs(stash_name, HvNAMELEN_get(stash), "IO::File")
1035 && !Perl_hv_common(aTHX_ GvHVn(PL_incgv), NULL,
1036 STR_WITH_LEN("IO/File.pm"), 0,
1037 HV_FETCH_ISEXISTS, NULL, 0)
1039 require_pv("IO/File.pm");
1040 gv = gv_fetchmeth_pvn(stash, name, nend - name, 0, 0);
1045 "Can't locate object method \"%s\" via package \"%.*s\"",
1046 name, (int)HvNAMELEN_get(stash), HvNAME_get(stash));
1050 const char *packname;
1053 packlen = nsplit - origname;
1054 packname = origname;
1056 packname = SvPV_const(error_report, packlen);
1060 "Can't locate object method \"%s\" via package \"%.*s\""
1061 " (perhaps you forgot to load \"%.*s\"?)",
1062 name, (int)packlen, packname, (int)packlen, packname);
1066 else if (autoload) {
1067 CV* const cv = GvCV(gv);
1068 if (!CvROOT(cv) && !CvXSUB(cv)) {
1076 if (GvCV(stubgv) != cv) /* orphaned import */
1079 autogv = gv_autoload_pvn(GvSTASH(stubgv),
1080 GvNAME(stubgv), GvNAMELEN(stubgv),
1081 GV_AUTOLOAD_ISMETHOD
1082 | (GvNAMEUTF8(stubgv) ? SVf_UTF8 : 0));
1092 Perl_gv_autoload_sv(pTHX_ HV *stash, SV* namesv, U32 flags)
1096 PERL_ARGS_ASSERT_GV_AUTOLOAD_SV;
1097 namepv = SvPV(namesv, namelen);
1100 return gv_autoload_pvn(stash, namepv, namelen, flags);
1104 Perl_gv_autoload_pv(pTHX_ HV *stash, const char *namepv, U32 flags)
1106 PERL_ARGS_ASSERT_GV_AUTOLOAD_PV;
1107 return gv_autoload_pvn(stash, namepv, strlen(namepv), flags);
1111 Perl_gv_autoload_pvn(pTHX_ HV *stash, const char *name, STRLEN len, U32 flags)
1119 SV *packname = NULL;
1120 U32 is_utf8 = flags & SVf_UTF8 ? SVf_UTF8 : 0;
1122 PERL_ARGS_ASSERT_GV_AUTOLOAD_PVN;
1124 if (len == S_autolen && memEQ(name, S_autoload, S_autolen))
1127 if (SvTYPE(stash) < SVt_PVHV) {
1128 STRLEN packname_len = 0;
1129 const char * const packname_ptr = SvPV_const(MUTABLE_SV(stash), packname_len);
1130 packname = newSVpvn_flags(packname_ptr, packname_len,
1131 SVs_TEMP | SvUTF8(stash));
1135 packname = sv_2mortal(newSVhek(HvNAME_HEK(stash)));
1137 if (!(gv = gv_fetchmeth_pvn(stash, S_autoload, S_autolen, FALSE, is_utf8)))
1141 if (!(CvROOT(cv) || CvXSUB(cv)))
1145 * Inheriting AUTOLOAD for non-methods works ... for now.
1148 !(flags & GV_AUTOLOAD_ISMETHOD)
1149 && (GvCVGEN(gv) || GvSTASH(gv) != stash)
1151 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
1152 "Use of inherited AUTOLOAD for non-method %s::%.*s() is deprecated",
1153 SvPV_nolen(packname), (int)len, name);
1156 /* rather than lookup/init $AUTOLOAD here
1157 * only to have the XSUB do another lookup for $AUTOLOAD
1158 * and split that value on the last '::',
1159 * pass along the same data via some unused fields in the CV
1161 CvSTASH_set(cv, stash);
1162 SvPV_set(cv, (char *)name); /* cast to lose constness warning */
1170 * Given &FOO::AUTOLOAD, set $FOO::AUTOLOAD to desired function name.
1171 * The subroutine's original name may not be "AUTOLOAD", so we don't
1172 * use that, but for lack of anything better we will use the sub's
1173 * original package to look up $AUTOLOAD.
1175 varstash = GvSTASH(CvGV(cv));
1176 vargv = *(GV**)hv_fetch(varstash, S_autoload, S_autolen, TRUE);
1180 gv_init_pvn(vargv, varstash, S_autoload, S_autolen, 0);
1181 #ifdef PERL_DONT_CREATE_GVSV
1182 GvSV(vargv) = newSV(0);
1186 varsv = GvSVn(vargv);
1187 sv_setsv(varsv, packname);
1188 sv_catpvs(varsv, "::");
1189 /* Ensure SvSETMAGIC() is called if necessary. In particular, to clear
1190 tainting if $FOO::AUTOLOAD was previously tainted, but is not now. */
1191 sv_catpvn_mg(varsv, name, len);
1198 /* require_tie_mod() internal routine for requiring a module
1199 * that implements the logic of automatic ties like %! and %-
1201 * The "gv" parameter should be the glob.
1202 * "varpv" holds the name of the var, used for error messages.
1203 * "namesv" holds the module name. Its refcount will be decremented.
1204 * "methpv" holds the method name to test for to check that things
1205 * are working reasonably close to as expected.
1206 * "flags": if flag & 1 then save the scalar before loading.
1207 * For the protection of $! to work (it is set by this routine)
1208 * the sv slot must already be magicalized.
1211 S_require_tie_mod(pTHX_ GV *gv, const char *varpv, SV* namesv, const char *methpv,const U32 flags)
1214 HV* stash = gv_stashsv(namesv, 0);
1216 PERL_ARGS_ASSERT_REQUIRE_TIE_MOD;
1218 if (!stash || !(gv_fetchmethod(stash, methpv))) {
1219 SV *module = newSVsv(namesv);
1220 char varname = *varpv; /* varpv might be clobbered by load_module,
1221 so save it. For the moment it's always
1227 PUSHSTACKi(PERLSI_MAGIC);
1228 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, module, NULL);
1232 stash = gv_stashsv(namesv, 0);
1234 Perl_croak(aTHX_ "panic: Can't use %%%c because %"SVf" is not available",
1235 varname, SVfARG(namesv));
1236 else if (!gv_fetchmethod(stash, methpv))
1237 Perl_croak(aTHX_ "panic: Can't use %%%c because %"SVf" does not support method %s",
1238 varname, SVfARG(namesv), methpv);
1240 SvREFCNT_dec(namesv);
1245 =for apidoc gv_stashpv
1247 Returns a pointer to the stash for a specified package. Uses C<strlen> to
1248 determine the length of C<name>, then calls C<gv_stashpvn()>.
1254 Perl_gv_stashpv(pTHX_ const char *name, I32 create)
1256 PERL_ARGS_ASSERT_GV_STASHPV;
1257 return gv_stashpvn(name, strlen(name), create);
1261 =for apidoc gv_stashpvn
1263 Returns a pointer to the stash for a specified package. The C<namelen>
1264 parameter indicates the length of the C<name>, in bytes. C<flags> is passed
1265 to C<gv_fetchpvn_flags()>, so if set to C<GV_ADD> then the package will be
1266 created if it does not already exist. If the package does not exist and
1267 C<flags> is 0 (or any other setting that does not create packages) then NULL
1275 Perl_gv_stashpvn(pTHX_ const char *name, U32 namelen, I32 flags)
1281 U32 tmplen = namelen + 2;
1283 PERL_ARGS_ASSERT_GV_STASHPVN;
1285 if (tmplen <= sizeof smallbuf)
1288 Newx(tmpbuf, tmplen, char);
1289 Copy(name, tmpbuf, namelen, char);
1290 tmpbuf[namelen] = ':';
1291 tmpbuf[namelen+1] = ':';
1292 tmpgv = gv_fetchpvn_flags(tmpbuf, tmplen, flags, SVt_PVHV);
1293 if (tmpbuf != smallbuf)
1297 stash = GvHV(tmpgv);
1298 if (!(flags & ~GV_NOADD_MASK) && !stash) return NULL;
1300 if (!HvNAME_get(stash)) {
1301 hv_name_set(stash, name, namelen, flags & SVf_UTF8 ? SVf_UTF8 : 0 );
1303 /* FIXME: This is a repeat of logic in gv_fetchpvn_flags */
1304 /* If the containing stash has multiple effective
1305 names, see that this one gets them, too. */
1306 if (HvAUX(GvSTASH(tmpgv))->xhv_name_count)
1307 mro_package_moved(stash, NULL, tmpgv, 1);
1313 =for apidoc gv_stashsv
1315 Returns a pointer to the stash for a specified package. See C<gv_stashpvn>.
1321 Perl_gv_stashsv(pTHX_ SV *sv, I32 flags)
1324 const char * const ptr = SvPV_const(sv,len);
1326 PERL_ARGS_ASSERT_GV_STASHSV;
1328 return gv_stashpvn(ptr, len, flags | SvUTF8(sv));
1333 Perl_gv_fetchpv(pTHX_ const char *nambeg, I32 add, const svtype sv_type) {
1334 PERL_ARGS_ASSERT_GV_FETCHPV;
1335 return gv_fetchpvn_flags(nambeg, strlen(nambeg), add, sv_type);
1339 Perl_gv_fetchsv(pTHX_ SV *name, I32 flags, const svtype sv_type) {
1341 const char * const nambeg =
1342 SvPV_flags_const(name, len, flags & GV_NO_SVGMAGIC ? 0 : SV_GMAGIC);
1343 PERL_ARGS_ASSERT_GV_FETCHSV;
1344 return gv_fetchpvn_flags(nambeg, len, flags | SvUTF8(name), sv_type);
1348 S_gv_magicalize_isa(pTHX_ GV *gv)
1352 PERL_ARGS_ASSERT_GV_MAGICALIZE_ISA;
1356 sv_magic(MUTABLE_SV(av), MUTABLE_SV(gv), PERL_MAGIC_isa,
1361 S_gv_magicalize_overload(pTHX_ GV *gv)
1365 PERL_ARGS_ASSERT_GV_MAGICALIZE_OVERLOAD;
1369 hv_magic(hv, NULL, PERL_MAGIC_overload);
1373 Perl_gv_fetchpvn_flags(pTHX_ const char *nambeg, STRLEN full_len, I32 flags,
1374 const svtype sv_type)
1377 register const char *name = nambeg;
1378 register GV *gv = NULL;
1381 register const char *name_cursor;
1383 const I32 no_init = flags & (GV_NOADD_NOINIT | GV_NOINIT);
1384 const I32 no_expand = flags & GV_NOEXPAND;
1385 const I32 add = flags & ~GV_NOADD_MASK;
1386 const U32 is_utf8 = flags & SVf_UTF8;
1387 bool addmg = !!(flags & GV_ADDMG);
1388 const char *const name_end = nambeg + full_len;
1389 const char *const name_em1 = name_end - 1;
1392 PERL_ARGS_ASSERT_GV_FETCHPVN_FLAGS;
1394 if (flags & GV_NOTQUAL) {
1395 /* Caller promised that there is no stash, so we can skip the check. */
1400 if (full_len > 2 && *name == '*' && isALPHA(name[1])) {
1401 /* accidental stringify on a GV? */
1405 for (name_cursor = name; name_cursor < name_end; name_cursor++) {
1406 if (name_cursor < name_em1 &&
1407 ((*name_cursor == ':'
1408 && name_cursor[1] == ':')
1409 || *name_cursor == '\''))
1412 stash = PL_defstash;
1413 if (!stash || !SvREFCNT(stash)) /* symbol table under destruction */
1416 len = name_cursor - name;
1417 if (name_cursor > nambeg) { /* Skip for initial :: or ' */
1419 if (*name_cursor == ':') {
1424 Newx(tmpbuf, len+2, char);
1425 Copy(name, tmpbuf, len, char);
1426 tmpbuf[len++] = ':';
1427 tmpbuf[len++] = ':';
1430 gvp = (GV**)hv_fetch(stash, key, is_utf8 ? -len : len, add);
1431 gv = gvp ? *gvp : NULL;
1432 if (gv && gv != (const GV *)&PL_sv_undef) {
1433 if (SvTYPE(gv) != SVt_PVGV)
1434 gv_init_pvn(gv, stash, key, len, (add & GV_ADDMULTI)|is_utf8);
1440 if (!gv || gv == (const GV *)&PL_sv_undef)
1443 if (!(stash = GvHV(gv)))
1445 stash = GvHV(gv) = newHV();
1446 if (!HvNAME_get(stash)) {
1447 if (GvSTASH(gv) == PL_defstash && len == 6
1448 && strnEQ(name, "CORE", 4))
1449 hv_name_set(stash, "CORE", 4, 0);
1452 stash, nambeg, name_cursor-nambeg, is_utf8
1454 /* If the containing stash has multiple effective
1455 names, see that this one gets them, too. */
1456 if (HvAUX(GvSTASH(gv))->xhv_name_count)
1457 mro_package_moved(stash, NULL, gv, 1);
1460 else if (!HvNAME_get(stash))
1461 hv_name_set(stash, nambeg, name_cursor - nambeg, is_utf8);
1464 if (*name_cursor == ':')
1466 name = name_cursor+1;
1467 if (name == name_end)
1469 ? gv : MUTABLE_GV(*hv_fetchs(PL_defstash, "main::", TRUE));
1472 len = name_cursor - name;
1474 /* No stash in name, so see how we can default */
1478 if (len && isIDFIRST_lazy(name)) {
1479 bool global = FALSE;
1487 if ((name[0] == 'I' && name[1] == 'N' && name[2] == 'C')
1488 || (name[0] == 'E' && name[1] == 'N' && name[2] == 'V')
1489 || (name[0] == 'S' && name[1] == 'I' && name[2] == 'G'))
1493 if (name[0] == 'A' && name[1] == 'R' && name[2] == 'G'
1498 if (name[0] == 'S' && name[1] == 'T' && name[2] == 'D'
1499 && name[3] == 'I' && name[4] == 'N')
1503 if ((name[0] == 'S' && name[1] == 'T' && name[2] == 'D')
1504 &&((name[3] == 'O' && name[4] == 'U' && name[5] == 'T')
1505 ||(name[3] == 'E' && name[4] == 'R' && name[5] == 'R')))
1509 if (name[0] == 'A' && name[1] == 'R' && name[2] == 'G'
1510 && name[3] == 'V' && name[4] == 'O' && name[5] == 'U'
1517 stash = PL_defstash;
1518 else if (IN_PERL_COMPILETIME) {
1519 stash = PL_curstash;
1520 if (add && (PL_hints & HINT_STRICT_VARS) &&
1521 sv_type != SVt_PVCV &&
1522 sv_type != SVt_PVGV &&
1523 sv_type != SVt_PVFM &&
1524 sv_type != SVt_PVIO &&
1525 !(len == 1 && sv_type == SVt_PV &&
1526 (*name == 'a' || *name == 'b')) )
1528 gvp = (GV**)hv_fetch(stash,name,is_utf8 ? -len : len,0);
1530 *gvp == (const GV *)&PL_sv_undef ||
1531 SvTYPE(*gvp) != SVt_PVGV)
1535 else if ((sv_type == SVt_PV && !GvIMPORTED_SV(*gvp)) ||
1536 (sv_type == SVt_PVAV && !GvIMPORTED_AV(*gvp)) ||
1537 (sv_type == SVt_PVHV && !GvIMPORTED_HV(*gvp)) )
1539 /* diag_listed_as: Variable "%s" is not imported%s */
1541 aTHX_ packWARN(WARN_MISC),
1542 "Variable \"%c%s\" is not imported",
1543 sv_type == SVt_PVAV ? '@' :
1544 sv_type == SVt_PVHV ? '%' : '$',
1548 aTHX_ packWARN(WARN_MISC),
1549 "\t(Did you mean &%s instead?)\n", name
1556 stash = CopSTASH(PL_curcop);
1559 stash = PL_defstash;
1562 /* By this point we should have a stash and a name */
1566 SV * const err = Perl_mess(aTHX_
1567 "Global symbol \"%s%s\" requires explicit package name",
1568 (sv_type == SVt_PV ? "$"
1569 : sv_type == SVt_PVAV ? "@"
1570 : sv_type == SVt_PVHV ? "%"
1573 if (USE_UTF8_IN_NAMES)
1576 gv = gv_fetchpvs("<none>::", GV_ADDMULTI, SVt_PVHV);
1578 /* symbol table under destruction */
1587 if (!SvREFCNT(stash)) /* symbol table under destruction */
1590 gvp = (GV**)hv_fetch(stash,name,is_utf8 ? -len : len,add);
1591 if (!gvp || *gvp == (const GV *)&PL_sv_undef) {
1592 if (addmg) gv = (GV *)newSV(0);
1595 else gv = *gvp, addmg = 0;
1596 /* From this point on, addmg means gv has not been inserted in the
1599 if (SvTYPE(gv) == SVt_PVGV) {
1602 gv_init_svtype(gv, sv_type);
1603 if (len == 1 && stash == PL_defstash
1604 && (sv_type == SVt_PVHV || sv_type == SVt_PVGV)) {
1606 require_tie_mod(gv, "!", newSVpvs("Errno"), "TIEHASH", 1);
1607 else if (*name == '-' || *name == '+')
1608 require_tie_mod(gv, name, newSVpvs("Tie::Hash::NamedCapture"), "TIEHASH", 0);
1610 else if (len == 3 && sv_type == SVt_PVAV
1611 && strnEQ(name, "ISA", 3)
1612 && (!GvAV(gv) || !SvSMAGICAL(GvAV(gv))))
1613 gv_magicalize_isa(gv);
1616 } else if (no_init) {
1619 } else if (no_expand && SvROK(gv)) {
1624 /* Adding a new symbol.
1625 Unless of course there was already something non-GV here, in which case
1626 we want to behave as if there was always a GV here, containing some sort
1628 Otherwise we run the risk of creating things like GvIO, which can cause
1629 subtle bugs. eg the one that tripped up SQL::Translator */
1631 faking_it = SvOK(gv);
1633 if (add & GV_ADDWARN)
1634 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "Had to create %s unexpectedly", nambeg);
1635 gv_init_pvn(gv, stash, name, len, (add & GV_ADDMULTI)|is_utf8);
1637 if (isALPHA(name[0]) && ! (isLEXWARN_on ? ckWARN(WARN_ONCE)
1638 : (PL_dowarn & G_WARN_ON ) ) )
1641 /* set up magic where warranted */
1642 if (stash != PL_defstash) { /* not the main stash */
1643 /* We only have to check for four names here: EXPORT, ISA, OVERLOAD
1644 and VERSION. All the others apply only to the main stash or to
1645 CORE (which is checked right after this). */
1647 const char * const name2 = name + 1;
1650 if (strnEQ(name2, "XPORT", 5))
1654 if (strEQ(name2, "SA"))
1655 gv_magicalize_isa(gv);
1658 if (strEQ(name2, "VERLOAD"))
1659 gv_magicalize_overload(gv);
1662 if (strEQ(name2, "ERSION"))
1668 goto add_magical_gv;
1671 if (len > 1 /* shortest is uc */ && HvNAMELEN_get(stash) == 4) {
1672 /* Avoid null warning: */
1673 const char * const stashname = HvNAME(stash); assert(stashname);
1674 if (strnEQ(stashname, "CORE", 4)
1675 && S_maybe_add_coresub(aTHX_
1676 addmg ? stash : 0, gv, name, len, nambeg, full_len
1685 /* Nothing else to do.
1686 The compiler will probably turn the switch statement into a
1687 branch table. Make sure we avoid even that small overhead for
1688 the common case of lower case variable names. */
1692 const char * const name2 = name + 1;
1695 if (strEQ(name2, "RGV")) {
1696 IoFLAGS(GvIOn(gv)) |= IOf_ARGV|IOf_START;
1698 else if (strEQ(name2, "RGVOUT")) {
1703 if (strnEQ(name2, "XPORT", 5))
1707 if (strEQ(name2, "SA")) {
1708 gv_magicalize_isa(gv);
1712 if (strEQ(name2, "VERLOAD")) {
1713 gv_magicalize_overload(gv);
1717 if (strEQ(name2, "IG")) {
1720 if (!PL_psig_name) {
1721 Newxz(PL_psig_name, 2 * SIG_SIZE, SV*);
1722 Newxz(PL_psig_pend, SIG_SIZE, int);
1723 PL_psig_ptr = PL_psig_name + SIG_SIZE;
1725 /* I think that the only way to get here is to re-use an
1726 embedded perl interpreter, where the previous
1727 use didn't clean up fully because
1728 PL_perl_destruct_level was 0. I'm not sure that we
1729 "support" that, in that I suspect in that scenario
1730 there are sufficient other garbage values left in the
1731 interpreter structure that something else will crash
1732 before we get here. I suspect that this is one of
1733 those "doctor, it hurts when I do this" bugs. */
1734 Zero(PL_psig_name, 2 * SIG_SIZE, SV*);
1735 Zero(PL_psig_pend, SIG_SIZE, int);
1739 hv_magic(hv, NULL, PERL_MAGIC_sig);
1740 for (i = 1; i < SIG_SIZE; i++) {
1741 SV * const * const init = hv_fetch(hv, PL_sig_name[i], strlen(PL_sig_name[i]), 1);
1743 sv_setsv(*init, &PL_sv_undef);
1748 if (strEQ(name2, "ERSION"))
1751 case '\003': /* $^CHILD_ERROR_NATIVE */
1752 if (strEQ(name2, "HILD_ERROR_NATIVE"))
1755 case '\005': /* $^ENCODING */
1756 if (strEQ(name2, "NCODING"))
1759 case '\007': /* $^GLOBAL_PHASE */
1760 if (strEQ(name2, "LOBAL_PHASE"))
1763 case '\015': /* $^MATCH */
1764 if (strEQ(name2, "ATCH"))
1766 case '\017': /* $^OPEN */
1767 if (strEQ(name2, "PEN"))
1770 case '\020': /* $^PREMATCH $^POSTMATCH */
1771 if (strEQ(name2, "REMATCH") || strEQ(name2, "OSTMATCH"))
1774 case '\024': /* ${^TAINT} */
1775 if (strEQ(name2, "AINT"))
1778 case '\025': /* ${^UNICODE}, ${^UTF8LOCALE} */
1779 if (strEQ(name2, "NICODE"))
1781 if (strEQ(name2, "TF8LOCALE"))
1783 if (strEQ(name2, "TF8CACHE"))
1786 case '\027': /* $^WARNING_BITS */
1787 if (strEQ(name2, "ARNING_BITS"))
1800 /* Ensures that we have an all-digit variable, ${"1foo"} fails
1802 /* This snippet is taken from is_gv_magical */
1803 const char *end = name + len;
1804 while (--end > name) {
1805 if (!isDIGIT(*end)) goto add_magical_gv;
1812 /* Names of length 1. (Or 0. But name is NUL terminated, so that will
1813 be case '\0' in this switch statement (ie a default case) */
1819 sv_type == SVt_PVAV ||
1820 sv_type == SVt_PVHV ||
1821 sv_type == SVt_PVCV ||
1822 sv_type == SVt_PVFM ||
1825 PL_sawampersand = TRUE;
1829 sv_setpv(GvSVn(gv),PL_chopset);
1833 #ifdef COMPLEX_STATUS
1834 SvUPGRADE(GvSVn(gv), SVt_PVLV);
1840 /* If %! has been used, automatically load Errno.pm. */
1842 sv_magic(GvSVn(gv), MUTABLE_SV(gv), PERL_MAGIC_sv, name, len);
1844 /* magicalization must be done before require_tie_mod is called */
1845 if (sv_type == SVt_PVHV || sv_type == SVt_PVGV)
1846 require_tie_mod(gv, "!", newSVpvs("Errno"), "TIEHASH", 1);
1851 GvMULTI_on(gv); /* no used once warnings here */
1853 AV* const av = GvAVn(gv);
1854 SV* const avc = (*name == '+') ? MUTABLE_SV(av) : NULL;
1856 sv_magic(MUTABLE_SV(av), avc, PERL_MAGIC_regdata, NULL, 0);
1857 sv_magic(GvSVn(gv), MUTABLE_SV(gv), PERL_MAGIC_sv, name, len);
1859 SvREADONLY_on(GvSVn(gv));
1862 if (sv_type == SVt_PVHV || sv_type == SVt_PVGV)
1863 require_tie_mod(gv, name, newSVpvs("Tie::Hash::NamedCapture"), "TIEHASH", 0);
1869 if (sv_type == SVt_PV)
1870 Perl_ck_warner_d(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
1871 "$%c is no longer supported", *name);
1874 sv_setiv(GvSVn(gv), (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0);
1877 case '\010': /* $^H */
1879 HV *const hv = GvHVn(gv);
1880 hv_magic(hv, NULL, PERL_MAGIC_hints);
1883 case '\023': /* $^S */
1885 SvREADONLY_on(GvSVn(gv));
1910 case '\001': /* $^A */
1911 case '\003': /* $^C */
1912 case '\004': /* $^D */
1913 case '\005': /* $^E */
1914 case '\006': /* $^F */
1915 case '\011': /* $^I, NOT \t in EBCDIC */
1916 case '\016': /* $^N */
1917 case '\017': /* $^O */
1918 case '\020': /* $^P */
1919 case '\024': /* $^T */
1920 case '\027': /* $^W */
1922 sv_magic(GvSVn(gv), MUTABLE_SV(gv), PERL_MAGIC_sv, name, len);
1925 case '\014': /* $^L */
1926 sv_setpvs(GvSVn(gv),"\f");
1927 PL_formfeed = GvSVn(gv);
1930 sv_setpvs(GvSVn(gv),"\034");
1934 SV * const sv = GvSV(gv);
1935 if (!sv_derived_from(PL_patchlevel, "version"))
1936 upg_version(PL_patchlevel, TRUE);
1937 GvSV(gv) = vnumify(PL_patchlevel);
1938 SvREADONLY_on(GvSV(gv));
1942 case '\026': /* $^V */
1944 SV * const sv = GvSV(gv);
1945 GvSV(gv) = new_version(PL_patchlevel);
1946 SvREADONLY_on(GvSV(gv));
1954 if (GvAV(gv) || GvHV(gv) || GvIO(gv) || GvCV(gv) || (
1955 GvSV(gv) && (SvOK(GvSV(gv)) || SvMAGICAL(GvSV(gv)))
1957 (void)hv_store(stash,name,len,(SV *)gv,0);
1958 else SvREFCNT_dec(gv), gv = NULL;
1960 if (gv) gv_init_svtype(gv, faking_it ? SVt_PVCV : sv_type);
1965 Perl_gv_fullname4(pTHX_ SV *sv, const GV *gv, const char *prefix, bool keepmain)
1968 const HV * const hv = GvSTASH(gv);
1970 PERL_ARGS_ASSERT_GV_FULLNAME4;
1976 sv_setpv(sv, prefix ? prefix : "");
1978 name = HvNAME_get(hv)
1979 ? sv_2mortal(newSVhek(HvNAME_HEK(hv)))
1980 : newSVpvn_flags( "__ANON__", 8, SVs_TEMP );
1982 if (keepmain || strnNE(SvPV_nolen(name), "main", SvCUR(name))) {
1986 sv_catsv(sv,sv_2mortal(newSVhek(GvNAME_HEK(gv))));
1990 Perl_gv_efullname4(pTHX_ SV *sv, const GV *gv, const char *prefix, bool keepmain)
1992 const GV * const egv = GvEGVx(gv);
1994 PERL_ARGS_ASSERT_GV_EFULLNAME4;
1996 gv_fullname4(sv, egv ? egv : gv, prefix, keepmain);
2000 Perl_gv_check(pTHX_ const HV *stash)
2005 PERL_ARGS_ASSERT_GV_CHECK;
2007 if (!HvARRAY(stash))
2009 for (i = 0; i <= (I32) HvMAX(stash); i++) {
2011 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
2014 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
2015 (gv = MUTABLE_GV(HeVAL(entry))) && isGV(gv) && (hv = GvHV(gv)))
2017 if (hv != PL_defstash && hv != stash)
2018 gv_check(hv); /* nested package */
2020 else if (isALPHA(*HeKEY(entry))) {
2022 gv = MUTABLE_GV(HeVAL(entry));
2023 if (SvTYPE(gv) != SVt_PVGV || GvMULTI(gv))
2026 CopLINE_set(PL_curcop, GvLINE(gv));
2028 CopFILE(PL_curcop) = (char *)file; /* set for warning */
2030 CopFILEGV(PL_curcop)
2031 = gv_fetchfile_flags(file, HEK_LEN(GvFILE_HEK(gv)), 0);
2033 Perl_warner(aTHX_ packWARN(WARN_ONCE),
2034 "Name \"%s::%s\" used only once: possible typo",
2035 HvNAME_get(stash), GvNAME(gv));
2042 Perl_newGVgen_flags(pTHX_ const char *pack, U32 flags)
2045 PERL_ARGS_ASSERT_NEWGVGEN_FLAGS;
2047 return gv_fetchpv(Perl_form(aTHX_ "%"SVf"::_GEN_%ld",
2048 SVfARG(newSVpvn_flags(pack, strlen(pack),
2054 /* hopefully this is only called on local symbol table entries */
2057 Perl_gp_ref(pTHX_ GP *gp)
2065 /* If the GP they asked for a reference to contains
2066 a method cache entry, clear it first, so that we
2067 don't infect them with our cached entry */
2068 SvREFCNT_dec(gp->gp_cv);
2077 Perl_gp_free(pTHX_ GV *gv)
2083 if (!gv || !isGV_with_GP(gv) || !(gp = GvGP(gv)))
2085 if (gp->gp_refcnt == 0) {
2086 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
2087 "Attempt to free unreferenced glob pointers"
2088 pTHX__FORMAT pTHX__VALUE);
2091 if (--gp->gp_refcnt > 0) {
2092 if (gp->gp_egv == gv)
2099 /* Copy and null out all the glob slots, so destructors do not see
2101 HEK * const file_hek = gp->gp_file_hek;
2102 SV * const sv = gp->gp_sv;
2103 AV * const av = gp->gp_av;
2104 HV * const hv = gp->gp_hv;
2105 IO * const io = gp->gp_io;
2106 CV * const cv = gp->gp_cv;
2107 CV * const form = gp->gp_form;
2109 gp->gp_file_hek = NULL;
2118 unshare_hek(file_hek);
2122 /* FIXME - another reference loop GV -> symtab -> GV ?
2123 Somehow gp->gp_hv can end up pointing at freed garbage. */
2124 if (hv && SvTYPE(hv) == SVt_PVHV) {
2125 const HEK *hvname_hek = HvNAME_HEK(hv);
2126 if (PL_stashcache && hvname_hek)
2127 (void)hv_delete(PL_stashcache, HEK_KEY(hvname_hek),
2128 (HEK_UTF8(hvname_hek) ? -HEK_LEN(hvname_hek) : HEK_LEN(hvname_hek)),
2136 if (!gp->gp_file_hek
2142 && !gp->gp_form) break;
2144 if (--attempts == 0) {
2146 "panic: gp_free failed to free glob pointer - "
2147 "something is repeatedly re-creating entries"
2157 Perl_magic_freeovrld(pTHX_ SV *sv, MAGIC *mg)
2159 AMT * const amtp = (AMT*)mg->mg_ptr;
2160 PERL_UNUSED_ARG(sv);
2162 PERL_ARGS_ASSERT_MAGIC_FREEOVRLD;
2164 if (amtp && AMT_AMAGIC(amtp)) {
2166 for (i = 1; i < NofAMmeth; i++) {
2167 CV * const cv = amtp->table[i];
2169 SvREFCNT_dec(MUTABLE_SV(cv));
2170 amtp->table[i] = NULL;
2177 /* Updates and caches the CV's */
2179 * 1 on success and there is some overload
2180 * 0 if there is no overload
2181 * -1 if some error occurred and it couldn't croak
2185 Perl_Gv_AMupdate(pTHX_ HV *stash, bool destructing)
2188 MAGIC* const mg = mg_find((const SV *)stash, PERL_MAGIC_overload_table);
2190 const struct mro_meta* stash_meta = HvMROMETA(stash);
2193 PERL_ARGS_ASSERT_GV_AMUPDATE;
2195 newgen = PL_sub_generation + stash_meta->pkg_gen + stash_meta->cache_gen;
2197 const AMT * const amtp = (AMT*)mg->mg_ptr;
2198 if (amtp->was_ok_am == PL_amagic_generation
2199 && amtp->was_ok_sub == newgen) {
2200 return AMT_OVERLOADED(amtp) ? 1 : 0;
2202 sv_unmagic(MUTABLE_SV(stash), PERL_MAGIC_overload_table);
2205 DEBUG_o( Perl_deb(aTHX_ "Recalcing overload magic in package %s\n",HvNAME_get(stash)) );
2208 amt.was_ok_am = PL_amagic_generation;
2209 amt.was_ok_sub = newgen;
2210 amt.fallback = AMGfallNO;
2214 int filled = 0, have_ovl = 0;
2217 /* Work with "fallback" key, which we assume to be first in PL_AMG_names */
2219 /* Try to find via inheritance. */
2220 GV *gv = gv_fetchmeth_pvn(stash, PL_AMG_names[0], 2, -1, 0);
2221 SV * const sv = gv ? GvSV(gv) : NULL;
2225 lim = DESTROY_amg; /* Skip overloading entries. */
2226 #ifdef PERL_DONT_CREATE_GVSV
2228 NOOP; /* Equivalent to !SvTRUE and !SvOK */
2231 else if (SvTRUE(sv))
2232 amt.fallback=AMGfallYES;
2234 amt.fallback=AMGfallNEVER;
2236 for (i = 1; i < lim; i++)
2237 amt.table[i] = NULL;
2238 for (; i < NofAMmeth; i++) {
2239 const char * const cooky = PL_AMG_names[i];
2240 /* Human-readable form, for debugging: */
2241 const char * const cp = (i >= DESTROY_amg ? cooky : AMG_id2name(i));
2242 const STRLEN l = PL_AMG_namelens[i];
2244 DEBUG_o( Perl_deb(aTHX_ "Checking overloading of \"%s\" in package \"%.256s\"\n",
2245 cp, HvNAME_get(stash)) );
2246 /* don't fill the cache while looking up!
2247 Creation of inheritance stubs in intermediate packages may
2248 conflict with the logic of runtime method substitution.
2249 Indeed, for inheritance A -> B -> C, if C overloads "+0",
2250 then we could have created stubs for "(+0" in A and C too.
2251 But if B overloads "bool", we may want to use it for
2252 numifying instead of C's "+0". */
2253 if (i >= DESTROY_amg)
2254 gv = Perl_gv_fetchmeth_pvn_autoload(aTHX_ stash, cooky, l, 0, 0);
2255 else /* Autoload taken care of below */
2256 gv = Perl_gv_fetchmeth_pvn(aTHX_ stash, cooky, l, -1, 0);
2258 if (gv && (cv = GvCV(gv))) {
2259 if(GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil")){
2260 const char * const hvname = HvNAME_get(GvSTASH(CvGV(cv)));
2261 if (hvname && HEK_LEN(HvNAME_HEK(GvSTASH(CvGV(cv)))) == 8
2262 && strEQ(hvname, "overload")) {
2263 /* This is a hack to support autoloading..., while
2264 knowing *which* methods were declared as overloaded. */
2265 /* GvSV contains the name of the method. */
2267 SV *gvsv = GvSV(gv);
2269 DEBUG_o( Perl_deb(aTHX_ "Resolving method \"%"SVf256\
2270 "\" for overloaded \"%s\" in package \"%.256s\"\n",
2271 (void*)GvSV(gv), cp, HvNAME(stash)) );
2272 if (!gvsv || !SvPOK(gvsv)
2273 || !(ngv = gv_fetchmethod_sv_flags(stash, gvsv, 0)))
2275 /* Can be an import stub (created by "can"). */
2280 const char * const name = (gvsv && SvPOK(gvsv)) ? SvPVX_const(gvsv) : "???";
2281 Perl_croak(aTHX_ "%s method \"%.256s\" overloading \"%s\" "\
2282 "in package \"%.256s\"",
2283 (GvCVGEN(gv) ? "Stub found while resolving"
2285 name, cp, HvNAME(stash));
2288 cv = GvCV(gv = ngv);
2291 DEBUG_o( Perl_deb(aTHX_ "Overloading \"%s\" in package \"%.256s\" via \"%.256s::%.256s\"\n",
2292 cp, HvNAME_get(stash), HvNAME_get(GvSTASH(CvGV(cv))),
2293 GvNAME(CvGV(cv))) );
2295 if (i < DESTROY_amg)
2297 } else if (gv) { /* Autoloaded... */
2298 cv = MUTABLE_CV(gv);
2301 amt.table[i]=MUTABLE_CV(SvREFCNT_inc_simple(cv));
2304 AMT_AMAGIC_on(&amt);
2306 AMT_OVERLOADED_on(&amt);
2307 sv_magic(MUTABLE_SV(stash), 0, PERL_MAGIC_overload_table,
2308 (char*)&amt, sizeof(AMT));
2312 /* Here we have no table: */
2314 AMT_AMAGIC_off(&amt);
2315 sv_magic(MUTABLE_SV(stash), 0, PERL_MAGIC_overload_table,
2316 (char*)&amt, sizeof(AMTS));
2322 Perl_gv_handler(pTHX_ HV *stash, I32 id)
2328 struct mro_meta* stash_meta;
2330 if (!stash || !HvNAME_get(stash))
2333 stash_meta = HvMROMETA(stash);
2334 newgen = PL_sub_generation + stash_meta->pkg_gen + stash_meta->cache_gen;
2336 mg = mg_find((const SV *)stash, PERL_MAGIC_overload_table);
2339 /* If we're looking up a destructor to invoke, we must avoid
2340 * that Gv_AMupdate croaks, because we might be dying already */
2341 if (Gv_AMupdate(stash, cBOOL(id == DESTROY_amg)) == -1) {
2342 /* and if it didn't found a destructor, we fall back
2343 * to a simpler method that will only look for the
2344 * destructor instead of the whole magic */
2345 if (id == DESTROY_amg) {
2346 GV * const gv = gv_fetchmethod(stash, "DESTROY");
2352 mg = mg_find((const SV *)stash, PERL_MAGIC_overload_table);
2355 amtp = (AMT*)mg->mg_ptr;
2356 if ( amtp->was_ok_am != PL_amagic_generation
2357 || amtp->was_ok_sub != newgen )
2359 if (AMT_AMAGIC(amtp)) {
2360 CV * const ret = amtp->table[id];
2361 if (ret && isGV(ret)) { /* Autoloading stab */
2362 /* Passing it through may have resulted in a warning
2363 "Inherited AUTOLOAD for a non-method deprecated", since
2364 our caller is going through a function call, not a method call.
2365 So return the CV for AUTOLOAD, setting $AUTOLOAD. */
2366 GV * const gv = gv_fetchmethod(stash, PL_AMG_names[id]);
2378 /* Implement tryAMAGICun_MG macro.
2379 Do get magic, then see if the stack arg is overloaded and if so call it.
2381 AMGf_set return the arg using SETs rather than assigning to
2383 AMGf_numeric apply sv_2num to the stack arg.
2387 Perl_try_amagic_un(pTHX_ int method, int flags) {
2391 SV* const arg = TOPs;
2395 if (SvAMAGIC(arg) && (tmpsv = amagic_call(arg, &PL_sv_undef, method,
2396 AMGf_noright | AMGf_unary))) {
2397 if (flags & AMGf_set) {
2402 if (SvPADMY(TARG)) {
2403 sv_setsv(TARG, tmpsv);
2413 if ((flags & AMGf_numeric) && SvROK(arg))
2419 /* Implement tryAMAGICbin_MG macro.
2420 Do get magic, then see if the two stack args are overloaded and if so
2423 AMGf_set return the arg using SETs rather than assigning to
2425 AMGf_assign op may be called as mutator (eg +=)
2426 AMGf_numeric apply sv_2num to the stack arg.
2430 Perl_try_amagic_bin(pTHX_ int method, int flags) {
2433 SV* const left = TOPm1s;
2434 SV* const right = TOPs;
2440 if (SvAMAGIC(left) || SvAMAGIC(right)) {
2441 SV * const tmpsv = amagic_call(left, right, method,
2442 ((flags & AMGf_assign) && opASSIGN ? AMGf_assign: 0));
2444 if (flags & AMGf_set) {
2451 if (opASSIGN || SvPADMY(TARG)) {
2452 sv_setsv(TARG, tmpsv);
2462 if(left==right && SvGMAGICAL(left)) {
2463 SV * const left = sv_newmortal();
2465 /* Print the uninitialized warning now, so it includes the vari-
2468 if (ckWARN(WARN_UNINITIALIZED)) report_uninit(right);
2469 sv_setsv_flags(left, &PL_sv_no, 0);
2471 else sv_setsv_flags(left, right, 0);
2474 if (flags & AMGf_numeric) {
2476 *(sp-1) = sv_2num(TOPm1s);
2478 *sp = sv_2num(right);
2484 Perl_amagic_deref_call(pTHX_ SV *ref, int method) {
2487 PERL_ARGS_ASSERT_AMAGIC_DEREF_CALL;
2489 while (SvAMAGIC(ref) &&
2490 (tmpsv = amagic_call(ref, &PL_sv_undef, method,
2491 AMGf_noright | AMGf_unary))) {
2493 Perl_croak(aTHX_ "Overloaded dereference did not return a reference");
2494 if (tmpsv == ref || SvRV(tmpsv) == SvRV(ref)) {
2495 /* Bail out if it returns us the same reference. */
2500 return tmpsv ? tmpsv : ref;
2504 Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags)
2509 CV **cvp=NULL, **ocvp=NULL;
2510 AMT *amtp=NULL, *oamtp=NULL;
2511 int off = 0, off1, lr = 0, notfound = 0;
2512 int postpr = 0, force_cpy = 0;
2513 int assign = AMGf_assign & flags;
2514 const int assignshift = assign ? 1 : 0;
2515 int use_default_op = 0;
2521 PERL_ARGS_ASSERT_AMAGIC_CALL;
2523 if ( PL_curcop->cop_hints & HINT_NO_AMAGIC ) {
2524 SV *lex_mask = cop_hints_fetch_pvs(PL_curcop, "overloading", 0);
2526 if ( !lex_mask || !SvOK(lex_mask) )
2527 /* overloading lexically disabled */
2529 else if ( lex_mask && SvPOK(lex_mask) ) {
2530 /* we have an entry in the hints hash, check if method has been
2531 * masked by overloading.pm */
2533 const int offset = method / 8;
2534 const int bit = method % 8;
2535 char *pv = SvPV(lex_mask, len);
2537 /* Bit set, so this overloading operator is disabled */
2538 if ( (STRLEN)offset < len && pv[offset] & ( 1 << bit ) )
2543 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
2544 && (stash = SvSTASH(SvRV(left)))
2545 && (mg = mg_find((const SV *)stash, PERL_MAGIC_overload_table))
2546 && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
2547 ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table
2549 && ((cv = cvp[off=method+assignshift])
2550 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
2556 cv = cvp[off=method])))) {
2557 lr = -1; /* Call method for left argument */
2559 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
2562 /* look for substituted methods */
2563 /* In all the covered cases we should be called with assign==0. */
2567 if ((cv = cvp[off=add_ass_amg])
2568 || ((cv = cvp[off = add_amg]) && (force_cpy = 0, postpr = 1))) {
2569 right = &PL_sv_yes; lr = -1; assign = 1;
2574 if ((cv = cvp[off = subtr_ass_amg])
2575 || ((cv = cvp[off = subtr_amg]) && (force_cpy = 0, postpr=1))) {
2576 right = &PL_sv_yes; lr = -1; assign = 1;
2580 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
2583 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
2586 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
2589 (void)((cv = cvp[off=bool__amg])
2590 || (cv = cvp[off=numer_amg])
2591 || (cv = cvp[off=string_amg]));
2598 * SV* ref causes confusion with the interpreter variable of
2601 SV* const tmpRef=SvRV(left);
2602 if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) {
2604 * Just to be extra cautious. Maybe in some
2605 * additional cases sv_setsv is safe, too.
2607 SV* const newref = newSVsv(tmpRef);
2608 SvOBJECT_on(newref);
2609 /* As a bit of a source compatibility hack, SvAMAGIC() and
2610 friends dereference an RV, to behave the same was as when
2611 overloading was stored on the reference, not the referant.
2612 Hence we can't use SvAMAGIC_on()
2614 SvFLAGS(newref) |= SVf_AMAGIC;
2615 SvSTASH_set(newref, MUTABLE_HV(SvREFCNT_inc(SvSTASH(tmpRef))));
2621 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
2622 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
2623 SV* const nullsv=sv_2mortal(newSViv(0));
2625 SV* const lessp = amagic_call(left,nullsv,
2626 lt_amg,AMGf_noright);
2627 logic = SvTRUE(lessp);
2629 SV* const lessp = amagic_call(left,nullsv,
2630 ncmp_amg,AMGf_noright);
2631 logic = (SvNV(lessp) < 0);
2634 if (off==subtr_amg) {
2645 if ((cv = cvp[off=subtr_amg])) {
2647 left = sv_2mortal(newSViv(0));
2652 case iter_amg: /* XXXX Eventually should do to_gv. */
2653 case ftest_amg: /* XXXX Eventually should do to_gv. */
2656 return NULL; /* Delegate operation to standard mechanisms. */
2664 return left; /* Delegate operation to standard mechanisms. */
2669 if (!cv) goto not_found;
2670 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
2671 && (stash = SvSTASH(SvRV(right)))
2672 && (mg = mg_find((const SV *)stash, PERL_MAGIC_overload_table))
2673 && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr)
2674 ? (amtp = (AMT*)mg->mg_ptr)->table
2676 && (cv = cvp[off=method])) { /* Method for right
2679 } else if (((cvp && amtp->fallback > AMGfallNEVER)
2680 || (ocvp && oamtp->fallback > AMGfallNEVER))
2681 && !(flags & AMGf_unary)) {
2682 /* We look for substitution for
2683 * comparison operations and
2685 if (method==concat_amg || method==concat_ass_amg
2686 || method==repeat_amg || method==repeat_ass_amg) {
2687 return NULL; /* Delegate operation to string conversion */
2709 if (ocvp && (oamtp->fallback > AMGfallNEVER)) {
2713 if (!cv && (cvp && amtp->fallback > AMGfallNEVER)) {
2723 not_found: /* No method found, either report or croak */
2731 return left; /* Delegate operation to standard mechanisms. */
2734 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
2735 notfound = 1; lr = -1;
2736 } else if (cvp && (cv=cvp[nomethod_amg])) {
2737 notfound = 1; lr = 1;
2738 } else if ((use_default_op =
2739 (!ocvp || oamtp->fallback >= AMGfallYES)
2740 && (!cvp || amtp->fallback >= AMGfallYES))
2742 /* Skip generating the "no method found" message. */
2746 if (off==-1) off=method;
2747 msg = sv_2mortal(Perl_newSVpvf(aTHX_
2748 "Operation \"%s\": no method found,%sargument %s%s%s%s",
2749 AMG_id2name(method + assignshift),
2750 (flags & AMGf_unary ? " " : "\n\tleft "),
2752 "in overloaded package ":
2753 "has no overloaded magic",
2755 HvNAME_get(SvSTASH(SvRV(left))):
2758 ",\n\tright argument in overloaded package ":
2761 : ",\n\tright argument has no overloaded magic"),
2763 HvNAME_get(SvSTASH(SvRV(right))):
2765 if (use_default_op) {
2766 DEBUG_o( Perl_deb(aTHX_ "%s", SvPVX_const(msg)) );
2768 Perl_croak(aTHX_ "%"SVf, SVfARG(msg));
2772 force_cpy = force_cpy || assign;
2777 DEBUG_o(Perl_deb(aTHX_
2778 "Overloaded operator \"%s\"%s%s%s:\n\tmethod%s found%s in package %s%s\n",
2780 method+assignshift==off? "" :
2782 method+assignshift==off? "" :
2783 AMG_id2name(method+assignshift),
2784 method+assignshift==off? "" : "\")",
2785 flags & AMGf_unary? "" :
2786 lr==1 ? " for right argument": " for left argument",
2787 flags & AMGf_unary? " for argument" : "",
2788 stash ? HvNAME_get(stash) : "null",
2789 fl? ",\n\tassignment variant used": "") );
2792 /* Since we use shallow copy during assignment, we need
2793 * to dublicate the contents, probably calling user-supplied
2794 * version of copy operator
2796 /* We need to copy in following cases:
2797 * a) Assignment form was called.
2798 * assignshift==1, assign==T, method + 1 == off
2799 * b) Increment or decrement, called directly.
2800 * assignshift==0, assign==0, method + 0 == off
2801 * c) Increment or decrement, translated to assignment add/subtr.
2802 * assignshift==0, assign==T,
2804 * d) Increment or decrement, translated to nomethod.
2805 * assignshift==0, assign==0,
2807 * e) Assignment form translated to nomethod.
2808 * assignshift==1, assign==T, method + 1 != off
2811 /* off is method, method+assignshift, or a result of opcode substitution.
2812 * In the latter case assignshift==0, so only notfound case is important.
2814 if (( (method + assignshift == off)
2815 && (assign || (method == inc_amg) || (method == dec_amg)))
2818 /* newSVsv does not behave as advertised, so we copy missing
2819 * information by hand */
2820 SV *tmpRef = SvRV(left);
2822 if (SvREFCNT(tmpRef) > 1 && (rv_copy = AMG_CALLunary(left,copy_amg))) {
2823 SvRV_set(left, rv_copy);
2825 SvREFCNT_dec(tmpRef);
2833 const bool oldcatch = CATCH_GET;
2836 Zero(&myop, 1, BINOP);
2837 myop.op_last = (OP *) &myop;
2838 myop.op_next = NULL;
2839 myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED;
2841 PUSHSTACKi(PERLSI_OVERLOAD);
2844 PL_op = (OP *) &myop;
2845 if (PERLDB_SUB && PL_curstash != PL_debstash)
2846 PL_op->op_private |= OPpENTERSUB_DB;
2848 Perl_pp_pushmark(aTHX);
2850 EXTEND(SP, notfound + 5);
2851 PUSHs(lr>0? right: left);
2852 PUSHs(lr>0? left: right);
2853 PUSHs( lr > 0 ? &PL_sv_yes : ( assign ? &PL_sv_undef : &PL_sv_no ));
2855 PUSHs(newSVpvn_flags(AMG_id2name(method + assignshift),
2856 AMG_id2namelen(method + assignshift), SVs_TEMP));
2858 PUSHs(MUTABLE_SV(cv));
2861 if ((PL_op = PL_ppaddr[OP_ENTERSUB](aTHX)))
2869 CATCH_SET(oldcatch);
2876 ans=SvIV(res)<=0; break;
2879 ans=SvIV(res)<0; break;
2882 ans=SvIV(res)>=0; break;
2885 ans=SvIV(res)>0; break;
2888 ans=SvIV(res)==0; break;
2891 ans=SvIV(res)!=0; break;
2894 SvSetSV(left,res); return left;
2896 ans=!SvTRUE(res); break;
2901 } else if (method==copy_amg) {
2903 Perl_croak(aTHX_ "Copy method did not return a reference");
2905 return SvREFCNT_inc(SvRV(res));
2913 Perl_gv_name_set(pTHX_ GV *gv, const char *name, U32 len, U32 flags)
2918 PERL_ARGS_ASSERT_GV_NAME_SET;
2921 Perl_croak(aTHX_ "panic: gv name too long (%"UVuf")", (UV) len);
2923 if (!(flags & GV_ADD) && GvNAME_HEK(gv)) {
2924 unshare_hek(GvNAME_HEK(gv));
2927 PERL_HASH(hash, name, len);
2928 GvNAME_HEK(gv) = share_hek(name, (flags & SVf_UTF8 ? -len : len), hash);
2932 =for apidoc gv_try_downgrade
2934 If the typeglob C<gv> can be expressed more succinctly, by having
2935 something other than a real GV in its place in the stash, replace it
2936 with the optimised form. Basic requirements for this are that C<gv>
2937 is a real typeglob, is sufficiently ordinary, and is only referenced
2938 from its package. This function is meant to be used when a GV has been
2939 looked up in part to see what was there, causing upgrading, but based
2940 on what was found it turns out that the real GV isn't required after all.
2942 If C<gv> is a completely empty typeglob, it is deleted from the stash.
2944 If C<gv> is a typeglob containing only a sufficiently-ordinary constant
2945 sub, the typeglob is replaced with a scalar-reference placeholder that
2946 more compactly represents the same thing.
2952 Perl_gv_try_downgrade(pTHX_ GV *gv)
2958 PERL_ARGS_ASSERT_GV_TRY_DOWNGRADE;
2960 /* XXX Why and where does this leave dangling pointers during global
2962 if (PL_phase == PERL_PHASE_DESTRUCT) return;
2964 if (!(SvREFCNT(gv) == 1 && SvTYPE(gv) == SVt_PVGV && !SvFAKE(gv) &&
2965 !SvOBJECT(gv) && !SvREADONLY(gv) &&
2966 isGV_with_GP(gv) && GvGP(gv) &&
2967 !GvINTRO(gv) && GvREFCNT(gv) == 1 &&
2968 !GvSV(gv) && !GvAV(gv) && !GvHV(gv) && !GvIOp(gv) && !GvFORM(gv) &&
2969 GvEGVx(gv) == gv && (stash = GvSTASH(gv))))
2971 if (SvMAGICAL(gv)) {
2973 /* only backref magic is allowed */
2974 if (SvGMAGICAL(gv) || SvSMAGICAL(gv))
2976 for (mg = SvMAGIC(gv); mg; mg = mg->mg_moremagic) {
2977 if (mg->mg_type != PERL_MAGIC_backref)
2983 HEK *gvnhek = GvNAME_HEK(gv);
2984 (void)hv_delete(stash, HEK_KEY(gvnhek),
2985 HEK_UTF8(gvnhek) ? -HEK_LEN(gvnhek) : HEK_LEN(gvnhek), G_DISCARD);
2986 } else if (GvMULTI(gv) && cv &&
2987 !SvOBJECT(cv) && !SvMAGICAL(cv) && !SvREADONLY(cv) &&
2988 CvSTASH(cv) == stash && CvGV(cv) == gv &&
2989 CvCONST(cv) && !CvMETHOD(cv) && !CvLVALUE(cv) && !CvUNIQUE(cv) &&
2990 !CvNODEBUG(cv) && !CvCLONE(cv) && !CvCLONED(cv) && !CvANON(cv) &&
2991 (namehek = GvNAME_HEK(gv)) &&
2992 (gvp = hv_fetch(stash, HEK_KEY(namehek),
2993 HEK_LEN(namehek)*(HEK_UTF8(namehek) ? -1 : 1), 0)) &&
2995 SV *value = SvREFCNT_inc(CvXSUBANY(cv).any_ptr);
2999 SvFLAGS(gv) = SVt_IV|SVf_ROK;
3000 SvANY(gv) = (XPVGV*)((char*)&(gv->sv_u.svu_iv) -
3001 STRUCT_OFFSET(XPVIV, xiv_iv));
3002 SvRV_set(gv, value);
3009 core_xsub(pTHX_ CV* cv)
3012 "&CORE::%s cannot be called directly", GvNAME(CvGV(cv))
3018 * c-indentation-style: bsd
3020 * indent-tabs-mode: t
3023 * ex: set ts=8 sts=4 sw=4 noet: