3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 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 * "Sam sat on the ground and put his head in his hands. 'I wish I had never
13 * come here, and I don't want to see no more magic,' he said, and fell silent."
17 =head1 Magical Functions
19 "Magic" is special data attached to SV structures in order to give them
20 "magical" properties. When any Perl code tries to read from, or assign to,
21 an SV marked as magical, it calls the 'get' or 'set' function associated
22 with that SV's magic. A get is called prior to reading an SV, in order to
23 give it a chance to update its internal value (get on $. writes the line
24 number of the last read filehandle into to the SV's IV slot), while
25 set is called after an SV has been written to, in order to allow it to make
26 use of its changed value (set on $/ copies the SV's new value to the
27 PL_rs global variable).
29 Magic is implemented as a linked list of MAGIC structures attached to the
30 SV. Each MAGIC struct holds the type of the magic, a pointer to an array
31 of functions that implement the get(), set(), length() etc functions,
32 plus space for some flags and pointers. For example, a tied variable has
33 a MAGIC structure that contains a pointer to the object associated with the
42 #if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS)
48 #if defined(HAS_SETGROUPS)
55 # include <sys/pstat.h>
58 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
59 Signal_t Perl_csighandler(int sig, ...);
61 Signal_t Perl_csighandler(int sig);
65 /* Missing protos on LynxOS */
66 void setruid(uid_t id);
67 void seteuid(uid_t id);
68 void setrgid(uid_t id);
69 void setegid(uid_t id);
73 * Use the "DESTRUCTOR" scope cleanup to reinstate magic.
81 /* MGS is typedef'ed to struct magic_state in perl.h */
84 S_save_magic(pTHX_ I32 mgs_ix, SV *sv)
88 assert(SvMAGICAL(sv));
89 /* Turning READONLY off for a copy-on-write scalar (including shared
90 hash keys) is a bad idea. */
92 sv_force_normal_flags(sv, 0);
94 SAVEDESTRUCTOR_X(S_restore_magic, INT2PTR(void*, (IV)mgs_ix));
96 mgs = SSPTR(mgs_ix, MGS*);
98 mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv);
99 mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */
103 if (!(SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK))) {
104 /* No public flags are set, so promote any private flags to public. */
105 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
110 =for apidoc mg_magical
112 Turns on the magical status of an SV. See C<sv_magic>.
118 Perl_mg_magical(pTHX_ SV *sv)
122 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
123 const MGVTBL* const vtbl = mg->mg_virtual;
125 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
129 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear)
138 Do magic after a value is retrieved from the SV. See C<sv_magic>.
144 Perl_mg_get(pTHX_ SV *sv)
147 const I32 mgs_ix = SSNEW(sizeof(MGS));
148 const bool was_temp = (bool)SvTEMP(sv);
150 MAGIC *newmg, *head, *cur, *mg;
151 /* guard against sv having being freed midway by holding a private
154 /* sv_2mortal has this side effect of turning on the TEMP flag, which can
155 cause the SV's buffer to get stolen (and maybe other stuff).
158 sv_2mortal(SvREFCNT_inc_simple_NN(sv));
163 save_magic(mgs_ix, sv);
165 /* We must call svt_get(sv, mg) for each valid entry in the linked
166 list of magic. svt_get() may delete the current entry, add new
167 magic to the head of the list, or upgrade the SV. AMS 20010810 */
169 newmg = cur = head = mg = SvMAGIC(sv);
171 const MGVTBL * const vtbl = mg->mg_virtual;
173 if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) {
174 CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg);
176 /* guard against magic having been deleted - eg FETCH calling
181 /* Don't restore the flags for this entry if it was deleted. */
182 if (mg->mg_flags & MGf_GSKIP)
183 (SSPTR(mgs_ix, MGS *))->mgs_flags = 0;
186 mg = mg->mg_moremagic;
189 /* Have we finished with the new entries we saw? Start again
190 where we left off (unless there are more new entries). */
198 /* Were any new entries added? */
199 if (!have_new && (newmg = SvMAGIC(sv)) != head) {
206 restore_magic(INT2PTR(void *, (IV)mgs_ix));
208 if (SvREFCNT(sv) == 1) {
209 /* We hold the last reference to this SV, which implies that the
210 SV was deleted as a side effect of the routines we called. */
219 Do magic after a value is assigned to the SV. See C<sv_magic>.
225 Perl_mg_set(pTHX_ SV *sv)
228 const I32 mgs_ix = SSNEW(sizeof(MGS));
232 save_magic(mgs_ix, sv);
234 for (mg = SvMAGIC(sv); mg; mg = nextmg) {
235 const MGVTBL* vtbl = mg->mg_virtual;
236 nextmg = mg->mg_moremagic; /* it may delete itself */
237 if (mg->mg_flags & MGf_GSKIP) {
238 mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */
239 (SSPTR(mgs_ix, MGS*))->mgs_flags = 0;
241 if (vtbl && vtbl->svt_set)
242 CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg);
245 restore_magic(INT2PTR(void*, (IV)mgs_ix));
250 =for apidoc mg_length
252 Report on the SV's length. See C<sv_magic>.
258 Perl_mg_length(pTHX_ SV *sv)
264 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
265 const MGVTBL * const vtbl = mg->mg_virtual;
266 if (vtbl && vtbl->svt_len) {
267 const I32 mgs_ix = SSNEW(sizeof(MGS));
268 save_magic(mgs_ix, sv);
269 /* omit MGf_GSKIP -- not changed here */
270 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
271 restore_magic(INT2PTR(void*, (IV)mgs_ix));
277 const U8 *s = (U8*)SvPV_const(sv, len);
278 len = utf8_length(s, s + len);
281 (void)SvPV_const(sv, len);
286 Perl_mg_size(pTHX_ SV *sv)
290 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
291 const MGVTBL* const vtbl = mg->mg_virtual;
292 if (vtbl && vtbl->svt_len) {
293 const I32 mgs_ix = SSNEW(sizeof(MGS));
295 save_magic(mgs_ix, sv);
296 /* omit MGf_GSKIP -- not changed here */
297 len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg);
298 restore_magic(INT2PTR(void*, (IV)mgs_ix));
305 return AvFILLp((AV *) sv); /* Fallback to non-tied array */
309 Perl_croak(aTHX_ "Size magic not implemented");
318 Clear something magical that the SV represents. See C<sv_magic>.
324 Perl_mg_clear(pTHX_ SV *sv)
326 const I32 mgs_ix = SSNEW(sizeof(MGS));
329 save_magic(mgs_ix, sv);
331 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
332 const MGVTBL* const vtbl = mg->mg_virtual;
333 /* omit GSKIP -- never set here */
335 if (vtbl && vtbl->svt_clear)
336 CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg);
339 restore_magic(INT2PTR(void*, (IV)mgs_ix));
346 Finds the magic pointer for type matching the SV. See C<sv_magic>.
352 Perl_mg_find(pTHX_ const SV *sv, int type)
357 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
358 if (mg->mg_type == type)
368 Copies the magic from one SV to another. See C<sv_magic>.
374 Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen)
378 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
379 const MGVTBL* const vtbl = mg->mg_virtual;
380 if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){
381 count += CALL_FPTR(vtbl->svt_copy)(aTHX_ sv, mg, nsv, key, klen);
384 const char type = mg->mg_type;
385 if (isUPPER(type) && type != PERL_MAGIC_uvar) {
387 (type == PERL_MAGIC_tied)
389 : (type == PERL_MAGIC_regdata && mg->mg_obj)
392 toLOWER(type), key, klen);
401 =for apidoc mg_localize
403 Copy some of the magic from an existing SV to new localized version of
404 that SV. Container magic (eg %ENV, $1, tie) gets copied, value magic
405 doesn't (eg taint, pos).
411 Perl_mg_localize(pTHX_ SV *sv, SV *nsv)
415 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
416 MGVTBL* const vtbl = mg->mg_virtual;
417 switch (mg->mg_type) {
418 /* value magic types: don't copy */
421 case PERL_MAGIC_regex_global:
422 case PERL_MAGIC_nkeys:
423 #ifdef USE_LOCALE_COLLATE
424 case PERL_MAGIC_collxfrm:
427 case PERL_MAGIC_taint:
429 case PERL_MAGIC_vstring:
430 case PERL_MAGIC_utf8:
431 case PERL_MAGIC_substr:
432 case PERL_MAGIC_defelem:
433 case PERL_MAGIC_arylen:
435 case PERL_MAGIC_backref:
436 case PERL_MAGIC_arylen_p:
437 case PERL_MAGIC_rhash:
438 case PERL_MAGIC_symtab:
442 if ((mg->mg_flags & MGf_LOCAL) && vtbl->svt_local)
443 (void)CALL_FPTR(vtbl->svt_local)(aTHX_ nsv, mg);
445 sv_magicext(nsv, mg->mg_obj, mg->mg_type, vtbl,
446 mg->mg_ptr, mg->mg_len);
448 /* container types should remain read-only across localization */
449 SvFLAGS(nsv) |= SvREADONLY(sv);
452 if (SvTYPE(nsv) >= SVt_PVMG && SvMAGIC(nsv)) {
453 SvFLAGS(nsv) |= SvMAGICAL(sv);
463 Free any magic storage used by the SV. See C<sv_magic>.
469 Perl_mg_free(pTHX_ SV *sv)
473 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
474 const MGVTBL* const vtbl = mg->mg_virtual;
475 moremagic = mg->mg_moremagic;
476 if (vtbl && vtbl->svt_free)
477 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
478 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
479 if (mg->mg_len > 0 || mg->mg_type == PERL_MAGIC_utf8)
480 Safefree(mg->mg_ptr);
481 else if (mg->mg_len == HEf_SVKEY)
482 SvREFCNT_dec((SV*)mg->mg_ptr);
484 if (mg->mg_flags & MGf_REFCOUNTED)
485 SvREFCNT_dec(mg->mg_obj);
488 SvMAGIC_set(sv, NULL);
495 Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg)
501 register const REGEXP * const rx = PM_GETRE(PL_curpm);
503 if (mg->mg_obj) { /* @+ */
504 /* return the number possible */
507 I32 paren = rx->lastparen;
509 /* return the last filled */
511 && (rx->startp[paren] == -1 || rx->endp[paren] == -1) )
522 Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg)
526 register const REGEXP * const rx = PM_GETRE(PL_curpm);
528 register const I32 paren = mg->mg_len;
533 if (paren <= (I32)rx->nparens &&
534 (s = rx->startp[paren]) != -1 &&
535 (t = rx->endp[paren]) != -1)
538 if (mg->mg_obj) /* @+ */
543 if (i > 0 && RX_MATCH_UTF8(rx)) {
544 const char * const b = rx->subbeg;
546 i = utf8_length((U8*)b, (U8*)(b+i));
557 Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg)
561 Perl_croak(aTHX_ PL_no_modify);
562 NORETURN_FUNCTION_END;
566 Perl_magic_len(pTHX_ SV *sv, MAGIC *mg)
571 register const REGEXP *rx;
574 switch (*mg->mg_ptr) {
575 case '1': case '2': case '3': case '4':
576 case '5': case '6': case '7': case '8': case '9': case '&':
577 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
579 paren = atoi(mg->mg_ptr); /* $& is in [0] */
581 if (paren <= (I32)rx->nparens &&
582 (s1 = rx->startp[paren]) != -1 &&
583 (t1 = rx->endp[paren]) != -1)
587 if (i > 0 && RX_MATCH_UTF8(rx)) {
588 const char * const s = rx->subbeg + s1;
593 if (is_utf8_string_loclen((U8*)s, i, &ep, &el))
597 Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i);
601 if (ckWARN(WARN_UNINITIALIZED))
606 if (ckWARN(WARN_UNINITIALIZED))
611 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
612 paren = rx->lastparen;
617 case '\016': /* ^N */
618 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
619 paren = rx->lastcloseparen;
625 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
626 if (rx->startp[0] != -1) {
637 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
638 if (rx->endp[0] != -1) {
639 i = rx->sublen - rx->endp[0];
650 if (!SvPOK(sv) && SvNIOK(sv)) {
658 #define SvRTRIM(sv) STMT_START { \
660 STRLEN len = SvCUR(sv); \
661 char * const p = SvPVX(sv); \
662 while (len > 0 && isSPACE(p[len-1])) \
664 SvCUR_set(sv, len); \
670 Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
674 register char *s = NULL;
676 const char * const remaining = mg->mg_ptr + 1;
677 const char nextchar = *remaining;
679 switch (*mg->mg_ptr) {
680 case '\001': /* ^A */
681 sv_setsv(sv, PL_bodytarget);
683 case '\003': /* ^C, ^CHILD_ERROR_NATIVE */
684 if (nextchar == '\0') {
685 sv_setiv(sv, (IV)PL_minus_c);
687 else if (strEQ(remaining, "HILD_ERROR_NATIVE")) {
688 sv_setiv(sv, (IV)STATUS_NATIVE);
692 case '\004': /* ^D */
693 sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK));
695 case '\005': /* ^E */
696 if (nextchar == '\0') {
697 #if defined(MACOS_TRADITIONAL)
701 sv_setnv(sv,(double)gMacPerl_OSErr);
702 sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : "");
706 # include <descrip.h>
707 # include <starlet.h>
709 $DESCRIPTOR(msgdsc,msg);
710 sv_setnv(sv,(NV) vaxc$errno);
711 if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1)
712 sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length);
717 if (!(_emx_env & 0x200)) { /* Under DOS */
718 sv_setnv(sv, (NV)errno);
719 sv_setpv(sv, errno ? Strerror(errno) : "");
721 if (errno != errno_isOS2) {
722 const int tmp = _syserrno();
723 if (tmp) /* 2nd call to _syserrno() makes it 0 */
726 sv_setnv(sv, (NV)Perl_rc);
727 sv_setpv(sv, os2error(Perl_rc));
731 const DWORD dwErr = GetLastError();
732 sv_setnv(sv, (NV)dwErr);
734 PerlProc_GetOSError(sv, dwErr);
737 sv_setpvn(sv, "", 0);
742 const int saveerrno = errno;
743 sv_setnv(sv, (NV)errno);
744 sv_setpv(sv, errno ? Strerror(errno) : "");
749 SvNOK_on(sv); /* what a wonderful hack! */
751 else if (strEQ(remaining, "NCODING"))
752 sv_setsv(sv, PL_encoding);
754 case '\006': /* ^F */
755 sv_setiv(sv, (IV)PL_maxsysfd);
757 case '\010': /* ^H */
758 sv_setiv(sv, (IV)PL_hints);
760 case '\011': /* ^I */ /* NOT \t in EBCDIC */
762 sv_setpv(sv, PL_inplace);
764 sv_setsv(sv, &PL_sv_undef);
766 case '\017': /* ^O & ^OPEN */
767 if (nextchar == '\0') {
768 sv_setpv(sv, PL_osname);
771 else if (strEQ(remaining, "PEN")) {
772 if (!(CopHINTS_get(&PL_compiling) & HINT_LEXICAL_IO))
773 sv_setsv(sv, &PL_sv_undef);
776 Perl_refcounted_he_fetch(aTHX_
777 PL_compiling.cop_hints_hash,
778 0, "open", 4, 0, 0));
782 case '\020': /* ^P */
783 sv_setiv(sv, (IV)PL_perldb);
785 case '\023': /* ^S */
786 if (nextchar == '\0') {
787 if (PL_lex_state != LEX_NOTPARSING)
790 sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE));
795 case '\024': /* ^T */
796 if (nextchar == '\0') {
798 sv_setnv(sv, PL_basetime);
800 sv_setiv(sv, (IV)PL_basetime);
803 else if (strEQ(remaining, "AINT"))
804 sv_setiv(sv, PL_tainting
805 ? (PL_taint_warn || PL_unsafe ? -1 : 1)
808 case '\025': /* $^UNICODE, $^UTF8LOCALE, $^UTF8CACHE */
809 if (strEQ(remaining, "NICODE"))
810 sv_setuv(sv, (UV) PL_unicode);
811 else if (strEQ(remaining, "TF8LOCALE"))
812 sv_setuv(sv, (UV) PL_utf8locale);
813 else if (strEQ(remaining, "TF8CACHE"))
814 sv_setiv(sv, (IV) PL_utf8cache);
816 case '\027': /* ^W & $^WARNING_BITS */
817 if (nextchar == '\0')
818 sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE));
819 else if (strEQ(remaining, "ARNING_BITS")) {
820 if (PL_compiling.cop_warnings == pWARN_NONE) {
821 sv_setpvn(sv, WARN_NONEstring, WARNsize) ;
823 else if (PL_compiling.cop_warnings == pWARN_STD) {
826 (PL_dowarn & G_WARN_ON) ? WARN_ALLstring : WARN_NONEstring,
830 else if (PL_compiling.cop_warnings == pWARN_ALL) {
831 /* Get the bit mask for $warnings::Bits{all}, because
832 * it could have been extended by warnings::register */
833 HV * const bits=get_hv("warnings::Bits", FALSE);
835 SV ** const bits_all = hv_fetchs(bits, "all", FALSE);
837 sv_setsv(sv, *bits_all);
840 sv_setpvn(sv, WARN_ALLstring, WARNsize) ;
844 sv_setpvn(sv, (char *) (PL_compiling.cop_warnings + 1),
845 *PL_compiling.cop_warnings);
850 case '1': case '2': case '3': case '4':
851 case '5': case '6': case '7': case '8': case '9': case '&':
852 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
854 * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj));
855 * XXX Does the new way break anything?
857 paren = atoi(mg->mg_ptr); /* $& is in [0] */
858 reg_numbered_buff_get( paren, rx, sv, 0);
861 sv_setsv(sv,&PL_sv_undef);
864 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
866 reg_numbered_buff_get( rx->lastparen, rx, sv, 0);
870 sv_setsv(sv,&PL_sv_undef);
872 case '\016': /* ^N */
873 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
874 if (rx->lastcloseparen) {
875 reg_numbered_buff_get( rx->lastcloseparen, rx, sv, 0);
880 sv_setsv(sv,&PL_sv_undef);
883 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
884 reg_numbered_buff_get( -2, rx, sv, 0);
887 sv_setsv(sv,&PL_sv_undef);
890 if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
891 reg_numbered_buff_get( -1, rx, sv, 0);
894 sv_setsv(sv,&PL_sv_undef);
897 if (GvIO(PL_last_in_gv)) {
898 sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv)));
903 sv_setiv(sv, (IV)STATUS_CURRENT);
904 #ifdef COMPLEX_STATUS
905 LvTARGOFF(sv) = PL_statusvalue;
906 LvTARGLEN(sv) = PL_statusvalue_vms;
911 if (GvIOp(PL_defoutgv))
912 s = IoTOP_NAME(GvIOp(PL_defoutgv));
916 sv_setpv(sv,GvENAME(PL_defoutgv));
921 if (GvIOp(PL_defoutgv))
922 s = IoFMT_NAME(GvIOp(PL_defoutgv));
924 s = GvENAME(PL_defoutgv);
928 if (GvIOp(PL_defoutgv))
929 sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv)));
932 if (GvIOp(PL_defoutgv))
933 sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv)));
936 if (GvIOp(PL_defoutgv))
937 sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv)));
944 sv_setiv(sv, (IV)CopARYBASE_get(PL_curcop));
947 if (GvIOp(PL_defoutgv))
948 sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 );
954 sv_copypv(sv, PL_ors_sv);
958 sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno));
959 sv_setpv(sv, errno ? Strerror(errno) : "");
962 const int saveerrno = errno;
963 sv_setnv(sv, (NV)errno);
965 if (errno == errno_isOS2 || errno == errno_isOS2_set)
966 sv_setpv(sv, os2error(Perl_rc));
969 sv_setpv(sv, errno ? Strerror(errno) : "");
974 SvNOK_on(sv); /* what a wonderful hack! */
977 sv_setiv(sv, (IV)PL_uid);
980 sv_setiv(sv, (IV)PL_euid);
983 sv_setiv(sv, (IV)PL_gid);
986 sv_setiv(sv, (IV)PL_egid);
990 Groups_t *gary = NULL;
991 I32 i, num_groups = getgroups(0, gary);
992 Newx(gary, num_groups, Groups_t);
993 num_groups = getgroups(num_groups, gary);
994 for (i = 0; i < num_groups; i++)
995 Perl_sv_catpvf(aTHX_ sv, " %"IVdf, (IV)gary[i]);
998 (void)SvIOK_on(sv); /* what a wonderful hack! */
1001 #ifndef MACOS_TRADITIONAL
1010 Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg)
1012 struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
1014 if (uf && uf->uf_val)
1015 (*uf->uf_val)(aTHX_ uf->uf_index, sv);
1020 Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg)
1023 STRLEN len = 0, klen;
1024 const char *s = SvOK(sv) ? SvPV_const(sv,len) : "";
1025 const char * const ptr = MgPV_const(mg,klen);
1028 #ifdef DYNAMIC_ENV_FETCH
1029 /* We just undefd an environment var. Is a replacement */
1030 /* waiting in the wings? */
1032 SV ** const valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE);
1034 s = SvOK(*valp) ? SvPV_const(*valp, len) : "";
1038 #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS)
1039 /* And you'll never guess what the dog had */
1040 /* in its mouth... */
1042 MgTAINTEDDIR_off(mg);
1044 if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) {
1045 char pathbuf[256], eltbuf[256], *cp, *elt;
1049 my_strlcpy(eltbuf, s, sizeof(eltbuf));
1051 do { /* DCL$PATH may be a search list */
1052 while (1) { /* as may dev portion of any element */
1053 if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) {
1054 if ( *(cp+1) == '.' || *(cp+1) == '-' ||
1055 cando_by_name(S_IWUSR,0,elt) ) {
1056 MgTAINTEDDIR_on(mg);
1060 if ((cp = strchr(elt, ':')) != NULL)
1062 if (my_trnlnm(elt, eltbuf, j++))
1068 } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf));
1071 if (s && klen == 4 && strEQ(ptr,"PATH")) {
1072 const char * const strend = s + len;
1074 while (s < strend) {
1078 #ifdef VMS /* Hmm. How do we get $Config{path_sep} from C? */
1079 const char path_sep = '|';
1081 const char path_sep = ':';
1083 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf,
1084 s, strend, path_sep, &i);
1086 if (i >= (I32)sizeof tmpbuf /* too long -- assume the worst */
1088 || !strchr(tmpbuf, ':') /* no colon thus no device name -- assume relative path */
1090 || *tmpbuf != '/' /* no starting slash -- assume relative path */
1092 || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) {
1093 MgTAINTEDDIR_on(mg);
1099 #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */
1105 Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg)
1107 PERL_UNUSED_ARG(sv);
1108 my_setenv(MgPV_nolen_const(mg),NULL);
1113 Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg)
1116 PERL_UNUSED_ARG(mg);
1118 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1120 if (PL_localizing) {
1123 hv_iterinit((HV*)sv);
1124 while ((entry = hv_iternext((HV*)sv))) {
1126 my_setenv(hv_iterkey(entry, &keylen),
1127 SvPV_nolen_const(hv_iterval((HV*)sv, entry)));
1135 Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg)
1138 PERL_UNUSED_ARG(sv);
1139 PERL_UNUSED_ARG(mg);
1141 Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system");
1149 #ifdef HAS_SIGPROCMASK
1151 restore_sigmask(pTHX_ SV *save_sv)
1153 const sigset_t * const ossetp = (const sigset_t *) SvPV_nolen_const( save_sv );
1154 (void)sigprocmask(SIG_SETMASK, ossetp, NULL);
1158 Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg)
1161 /* Are we fetching a signal entry? */
1162 const I32 i = whichsig(MgPV_nolen_const(mg));
1165 sv_setsv(sv,PL_psig_ptr[i]);
1167 Sighandler_t sigstate = rsignal_state(i);
1168 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1169 if (PL_sig_handlers_initted && PL_sig_ignoring[i])
1172 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1173 if (PL_sig_handlers_initted && PL_sig_defaulting[i])
1176 /* cache state so we don't fetch it again */
1177 if(sigstate == (Sighandler_t) SIG_IGN)
1178 sv_setpv(sv,"IGNORE");
1180 sv_setsv(sv,&PL_sv_undef);
1181 PL_psig_ptr[i] = SvREFCNT_inc_simple_NN(sv);
1188 Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg)
1190 /* XXX Some of this code was copied from Perl_magic_setsig. A little
1191 * refactoring might be in order.
1194 register const char * const s = MgPV_nolen_const(mg);
1195 PERL_UNUSED_ARG(sv);
1198 if (strEQ(s,"__DIE__"))
1200 else if (strEQ(s,"__WARN__") && PL_warnhook != PERL_WARNHOOK_FATAL)
1203 SV *const to_dec = *svp;
1205 SvREFCNT_dec(to_dec);
1209 /* Are we clearing a signal entry? */
1210 const I32 i = whichsig(s);
1212 #ifdef HAS_SIGPROCMASK
1215 /* Avoid having the signal arrive at a bad time, if possible. */
1218 sigprocmask(SIG_BLOCK, &set, &save);
1220 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1221 SAVEFREESV(save_sv);
1222 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1225 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1226 if (!PL_sig_handlers_initted) Perl_csighandler_init();
1228 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1229 PL_sig_defaulting[i] = 1;
1230 (void)rsignal(i, PL_csighandlerp);
1232 (void)rsignal(i, (Sighandler_t) SIG_DFL);
1234 if(PL_psig_name[i]) {
1235 SvREFCNT_dec(PL_psig_name[i]);
1238 if(PL_psig_ptr[i]) {
1239 SV * const to_dec=PL_psig_ptr[i];
1242 SvREFCNT_dec(to_dec);
1251 #ifndef SIG_PENDING_DIE_COUNT
1252 # define SIG_PENDING_DIE_COUNT 120
1256 S_raise_signal(pTHX_ int sig)
1259 /* Set a flag to say this signal is pending */
1260 PL_psig_pend[sig]++;
1261 /* And one to say _a_ signal is pending */
1262 if (++PL_sig_pending >= SIG_PENDING_DIE_COUNT)
1263 Perl_croak(aTHX_ "Maximal count of pending signals (%lu) exceeded",
1264 (unsigned long)SIG_PENDING_DIE_COUNT);
1268 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
1269 Perl_csighandler(int sig, ...)
1271 Perl_csighandler(int sig)
1274 #ifdef PERL_GET_SIG_CONTEXT
1275 dTHXa(PERL_GET_SIG_CONTEXT);
1279 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1280 (void) rsignal(sig, PL_csighandlerp);
1281 if (PL_sig_ignoring[sig]) return;
1283 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1284 if (PL_sig_defaulting[sig])
1285 #ifdef KILL_BY_SIGPRC
1286 exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG);
1301 (PL_signals & PERL_SIGNALS_UNSAFE_FLAG))
1302 /* Call the perl level handler now--
1303 * with risk we may be in malloc() etc. */
1304 (*PL_sighandlerp)(sig);
1306 S_raise_signal(aTHX_ sig);
1309 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1311 Perl_csighandler_init(void)
1314 if (PL_sig_handlers_initted) return;
1316 for (sig = 1; sig < SIG_SIZE; sig++) {
1317 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1319 PL_sig_defaulting[sig] = 1;
1320 (void) rsignal(sig, PL_csighandlerp);
1322 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1323 PL_sig_ignoring[sig] = 0;
1326 PL_sig_handlers_initted = 1;
1331 Perl_despatch_signals(pTHX)
1336 for (sig = 1; sig < SIG_SIZE; sig++) {
1337 if (PL_psig_pend[sig]) {
1338 PERL_BLOCKSIG_ADD(set, sig);
1339 PL_psig_pend[sig] = 0;
1340 PERL_BLOCKSIG_BLOCK(set);
1341 (*PL_sighandlerp)(sig);
1342 PERL_BLOCKSIG_UNBLOCK(set);
1348 Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg)
1353 /* Need to be careful with SvREFCNT_dec(), because that can have side
1354 * effects (due to closures). We must make sure that the new disposition
1355 * is in place before it is called.
1359 #ifdef HAS_SIGPROCMASK
1364 register const char *s = MgPV_const(mg,len);
1366 if (strEQ(s,"__DIE__"))
1368 else if (strEQ(s,"__WARN__"))
1371 Perl_croak(aTHX_ "No such hook: %s", s);
1374 if (*svp != PERL_WARNHOOK_FATAL)
1380 i = whichsig(s); /* ...no, a brick */
1382 if (ckWARN(WARN_SIGNAL))
1383 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s);
1386 #ifdef HAS_SIGPROCMASK
1387 /* Avoid having the signal arrive at a bad time, if possible. */
1390 sigprocmask(SIG_BLOCK, &set, &save);
1392 save_sv = newSVpv((char *)(&save), sizeof(sigset_t));
1393 SAVEFREESV(save_sv);
1394 SAVEDESTRUCTOR_X(restore_sigmask, save_sv);
1397 #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS)
1398 if (!PL_sig_handlers_initted) Perl_csighandler_init();
1400 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1401 PL_sig_ignoring[i] = 0;
1403 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1404 PL_sig_defaulting[i] = 0;
1406 SvREFCNT_dec(PL_psig_name[i]);
1407 to_dec = PL_psig_ptr[i];
1408 PL_psig_ptr[i] = SvREFCNT_inc_simple_NN(sv);
1409 SvTEMP_off(sv); /* Make sure it doesn't go away on us */
1410 PL_psig_name[i] = newSVpvn(s, len);
1411 SvREADONLY_on(PL_psig_name[i]);
1413 if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) {
1415 (void)rsignal(i, PL_csighandlerp);
1416 #ifdef HAS_SIGPROCMASK
1421 *svp = SvREFCNT_inc_simple_NN(sv);
1423 SvREFCNT_dec(to_dec);
1426 s = SvOK(sv) ? SvPV_force(sv,len) : "DEFAULT";
1427 if (strEQ(s,"IGNORE")) {
1429 #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS
1430 PL_sig_ignoring[i] = 1;
1431 (void)rsignal(i, PL_csighandlerp);
1433 (void)rsignal(i, (Sighandler_t) SIG_IGN);
1437 else if (strEQ(s,"DEFAULT") || !*s) {
1439 #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS
1441 PL_sig_defaulting[i] = 1;
1442 (void)rsignal(i, PL_csighandlerp);
1445 (void)rsignal(i, (Sighandler_t) SIG_DFL);
1450 * We should warn if HINT_STRICT_REFS, but without
1451 * access to a known hint bit in a known OP, we can't
1452 * tell whether HINT_STRICT_REFS is in force or not.
1454 if (!strchr(s,':') && !strchr(s,'\''))
1455 Perl_sv_insert(aTHX_ sv, 0, 0, STR_WITH_LEN("main::"));
1457 (void)rsignal(i, PL_csighandlerp);
1459 *svp = SvREFCNT_inc_simple_NN(sv);
1461 #ifdef HAS_SIGPROCMASK
1466 SvREFCNT_dec(to_dec);
1469 #endif /* !PERL_MICRO */
1472 Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg)
1475 PERL_UNUSED_ARG(sv);
1476 PERL_UNUSED_ARG(mg);
1477 PL_sub_generation++;
1482 Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg)
1485 PERL_UNUSED_ARG(sv);
1486 PERL_UNUSED_ARG(mg);
1487 /* HV_badAMAGIC_on(Sv_STASH(sv)); */
1488 PL_amagic_generation++;
1494 Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg)
1496 HV * const hv = (HV*)LvTARG(sv);
1498 PERL_UNUSED_ARG(mg);
1501 (void) hv_iterinit(hv);
1502 if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied))
1505 while (hv_iternext(hv))
1510 sv_setiv(sv, (IV)i);
1515 Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg)
1517 PERL_UNUSED_ARG(mg);
1519 hv_ksplit((HV*)LvTARG(sv), SvIV(sv));
1524 /* caller is responsible for stack switching/cleanup */
1526 S_magic_methcall(pTHX_ SV *sv, const MAGIC *mg, const char *meth, I32 flags, int n, SV *val)
1533 PUSHs(SvTIED_obj(sv, mg));
1536 if (mg->mg_len >= 0)
1537 PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len)));
1538 else if (mg->mg_len == HEf_SVKEY)
1539 PUSHs((SV*)mg->mg_ptr);
1541 else if (mg->mg_type == PERL_MAGIC_tiedelem) {
1542 PUSHs(sv_2mortal(newSViv(mg->mg_len)));
1550 return call_method(meth, flags);
1554 S_magic_methpack(pTHX_ SV *sv, const MAGIC *mg, const char *meth)
1560 PUSHSTACKi(PERLSI_MAGIC);
1562 if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) {
1563 sv_setsv(sv, *PL_stack_sp--);
1573 Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg)
1576 mg->mg_flags |= MGf_GSKIP;
1577 magic_methpack(sv,mg,"FETCH");
1582 Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg)
1586 PUSHSTACKi(PERLSI_MAGIC);
1587 magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv);
1594 Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg)
1596 return magic_methpack(sv,mg,"DELETE");
1601 Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg)
1608 PUSHSTACKi(PERLSI_MAGIC);
1609 if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) {
1610 sv = *PL_stack_sp--;
1611 retval = (U32) SvIV(sv)-1;
1620 Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
1625 PUSHSTACKi(PERLSI_MAGIC);
1627 XPUSHs(SvTIED_obj(sv, mg));
1629 call_method("CLEAR", G_SCALAR|G_DISCARD);
1637 Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key)
1640 const char * const meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY";
1644 PUSHSTACKi(PERLSI_MAGIC);
1647 PUSHs(SvTIED_obj(sv, mg));
1652 if (call_method(meth, G_SCALAR))
1653 sv_setsv(key, *PL_stack_sp--);
1662 Perl_magic_existspack(pTHX_ SV *sv, const MAGIC *mg)
1664 return magic_methpack(sv,mg,"EXISTS");
1668 Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg)
1672 SV * const tied = SvTIED_obj((SV*)hv, mg);
1673 HV * const pkg = SvSTASH((SV*)SvRV(tied));
1675 if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) {
1677 if (HvEITER_get(hv))
1678 /* we are in an iteration so the hash cannot be empty */
1680 /* no xhv_eiter so now use FIRSTKEY */
1681 key = sv_newmortal();
1682 magic_nextpack((SV*)hv, mg, key);
1683 HvEITER_set(hv, NULL); /* need to reset iterator */
1684 return SvOK(key) ? &PL_sv_yes : &PL_sv_no;
1687 /* there is a SCALAR method that we can call */
1689 PUSHSTACKi(PERLSI_MAGIC);
1695 if (call_method("SCALAR", G_SCALAR))
1696 retval = *PL_stack_sp--;
1698 retval = &PL_sv_undef;
1705 Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
1708 GV * const gv = PL_DBline;
1709 const I32 i = SvTRUE(sv);
1710 SV ** const svp = av_fetch(GvAV(gv),
1711 atoi(MgPV_nolen_const(mg)), FALSE);
1712 if (svp && SvIOKp(*svp)) {
1713 OP * const o = INT2PTR(OP*,SvIVX(*svp));
1715 /* set or clear breakpoint in the relevant control op */
1717 o->op_flags |= OPf_SPECIAL;
1719 o->op_flags &= ~OPf_SPECIAL;
1726 Perl_magic_getarylen(pTHX_ SV *sv, const MAGIC *mg)
1729 const AV * const obj = (AV*)mg->mg_obj;
1731 sv_setiv(sv, AvFILL(obj) + CopARYBASE_get(PL_curcop));
1739 Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg)
1742 AV * const obj = (AV*)mg->mg_obj;
1744 av_fill(obj, SvIV(sv) - CopARYBASE_get(PL_curcop));
1746 if (ckWARN(WARN_MISC))
1747 Perl_warner(aTHX_ packWARN(WARN_MISC),
1748 "Attempt to set length of freed array");
1754 Perl_magic_freearylen_p(pTHX_ SV *sv, MAGIC *mg)
1757 PERL_UNUSED_ARG(sv);
1758 /* during global destruction, mg_obj may already have been freed */
1759 if (PL_in_clean_all)
1762 mg = mg_find (mg->mg_obj, PERL_MAGIC_arylen);
1765 /* arylen scalar holds a pointer back to the array, but doesn't own a
1766 reference. Hence the we (the array) are about to go away with it
1767 still pointing at us. Clear its pointer, else it would be pointing
1768 at free memory. See the comment in sv_magic about reference loops,
1769 and why it can't own a reference to us. */
1776 Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg)
1779 SV* const lsv = LvTARG(sv);
1780 PERL_UNUSED_ARG(mg);
1782 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) {
1783 MAGIC * const found = mg_find(lsv, PERL_MAGIC_regex_global);
1784 if (found && found->mg_len >= 0) {
1785 I32 i = found->mg_len;
1787 sv_pos_b2u(lsv, &i);
1788 sv_setiv(sv, i + CopARYBASE_get(PL_curcop));
1797 Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg)
1800 SV* const lsv = LvTARG(sv);
1806 PERL_UNUSED_ARG(mg);
1808 if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv))
1809 found = mg_find(lsv, PERL_MAGIC_regex_global);
1815 #ifdef PERL_OLD_COPY_ON_WRITE
1817 sv_force_normal_flags(lsv, 0);
1819 found = sv_magicext(lsv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob,
1822 else if (!SvOK(sv)) {
1826 len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv);
1828 pos = SvIV(sv) - CopARYBASE_get(PL_curcop);
1831 ulen = sv_len_utf8(lsv);
1841 else if (pos > (SSize_t)len)
1846 sv_pos_u2b(lsv, &p, 0);
1850 found->mg_len = pos;
1851 found->mg_flags &= ~MGf_MINMATCH;
1857 Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg)
1860 PERL_UNUSED_ARG(mg);
1864 if (isGV_with_GP(sv)) {
1865 /* We're actually already a typeglob, so don't need the stuff below.
1869 gv = gv_fetchsv(sv, GV_ADD, SVt_PVGV);
1874 GvGP(sv) = gp_ref(GvGP(gv));
1879 Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg)
1882 SV * const lsv = LvTARG(sv);
1883 const char * const tmps = SvPV_const(lsv,len);
1884 I32 offs = LvTARGOFF(sv);
1885 I32 rem = LvTARGLEN(sv);
1886 PERL_UNUSED_ARG(mg);
1889 sv_pos_u2b(lsv, &offs, &rem);
1890 if (offs > (I32)len)
1892 if (rem + offs > (I32)len)
1894 sv_setpvn(sv, tmps + offs, (STRLEN)rem);
1901 Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg)
1905 const char * const tmps = SvPV_const(sv, len);
1906 SV * const lsv = LvTARG(sv);
1907 I32 lvoff = LvTARGOFF(sv);
1908 I32 lvlen = LvTARGLEN(sv);
1909 PERL_UNUSED_ARG(mg);
1912 sv_utf8_upgrade(lsv);
1913 sv_pos_u2b(lsv, &lvoff, &lvlen);
1914 sv_insert(lsv, lvoff, lvlen, tmps, len);
1915 LvTARGLEN(sv) = sv_len_utf8(sv);
1918 else if (lsv && SvUTF8(lsv)) {
1920 sv_pos_u2b(lsv, &lvoff, &lvlen);
1921 LvTARGLEN(sv) = len;
1922 utf8 = (char*)bytes_to_utf8((U8*)tmps, &len);
1923 sv_insert(lsv, lvoff, lvlen, utf8, len);
1927 sv_insert(lsv, lvoff, lvlen, tmps, len);
1928 LvTARGLEN(sv) = len;
1936 Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg)
1939 PERL_UNUSED_ARG(sv);
1940 TAINT_IF((PL_localizing != 1) && (mg->mg_len & 1));
1945 Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg)
1948 PERL_UNUSED_ARG(sv);
1949 /* update taint status unless we're restoring at scope exit */
1950 if (PL_localizing != 2) {
1960 Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg)
1962 SV * const lsv = LvTARG(sv);
1963 PERL_UNUSED_ARG(mg);
1966 sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv)));
1974 Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg)
1976 PERL_UNUSED_ARG(mg);
1977 do_vecset(sv); /* XXX slurp this routine */
1982 Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg)
1986 if (LvTARGLEN(sv)) {
1988 SV * const ahv = LvTARG(sv);
1989 HE * const he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0);
1994 AV* const av = (AV*)LvTARG(sv);
1995 if ((I32)LvTARGOFF(sv) <= AvFILL(av))
1996 targ = AvARRAY(av)[LvTARGOFF(sv)];
1998 if (targ && (targ != &PL_sv_undef)) {
1999 /* somebody else defined it for us */
2000 SvREFCNT_dec(LvTARG(sv));
2001 LvTARG(sv) = SvREFCNT_inc_simple_NN(targ);
2003 SvREFCNT_dec(mg->mg_obj);
2005 mg->mg_flags &= ~MGf_REFCOUNTED;
2010 sv_setsv(sv, targ ? targ : &PL_sv_undef);
2015 Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg)
2017 PERL_UNUSED_ARG(mg);
2021 sv_setsv(LvTARG(sv), sv);
2022 SvSETMAGIC(LvTARG(sv));
2028 Perl_vivify_defelem(pTHX_ SV *sv)
2034 if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem)))
2037 SV * const ahv = LvTARG(sv);
2038 HE * const he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0);
2041 if (!value || value == &PL_sv_undef)
2042 Perl_croak(aTHX_ PL_no_helem_sv, SVfARG(mg->mg_obj));
2045 AV* const av = (AV*)LvTARG(sv);
2046 if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av))
2047 LvTARG(sv) = NULL; /* array can't be extended */
2049 SV* const * const svp = av_fetch(av, LvTARGOFF(sv), TRUE);
2050 if (!svp || (value = *svp) == &PL_sv_undef)
2051 Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv));
2054 SvREFCNT_inc_simple_void(value);
2055 SvREFCNT_dec(LvTARG(sv));
2058 SvREFCNT_dec(mg->mg_obj);
2060 mg->mg_flags &= ~MGf_REFCOUNTED;
2064 Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg)
2066 return Perl_sv_kill_backrefs(aTHX_ sv, (AV*)mg->mg_obj);
2070 Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg)
2072 PERL_UNUSED_CONTEXT;
2079 Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg)
2081 PERL_UNUSED_ARG(mg);
2082 sv_unmagic(sv, PERL_MAGIC_bm);
2089 Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg)
2091 PERL_UNUSED_ARG(mg);
2092 sv_unmagic(sv, PERL_MAGIC_fm);
2098 Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg)
2100 const struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
2102 if (uf && uf->uf_set)
2103 (*uf->uf_set)(aTHX_ uf->uf_index, sv);
2108 Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg)
2110 PERL_UNUSED_ARG(mg);
2111 sv_unmagic(sv, PERL_MAGIC_qr);
2116 Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg)
2119 regexp * const re = (regexp *)mg->mg_obj;
2120 PERL_UNUSED_ARG(sv);
2126 #ifdef USE_LOCALE_COLLATE
2128 Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg)
2131 * RenE<eacute> Descartes said "I think not."
2132 * and vanished with a faint plop.
2134 PERL_UNUSED_CONTEXT;
2135 PERL_UNUSED_ARG(sv);
2137 Safefree(mg->mg_ptr);
2143 #endif /* USE_LOCALE_COLLATE */
2145 /* Just clear the UTF-8 cache data. */
2147 Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg)
2149 PERL_UNUSED_CONTEXT;
2150 PERL_UNUSED_ARG(sv);
2151 Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */
2153 mg->mg_len = -1; /* The mg_len holds the len cache. */
2158 Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
2161 register const char *s;
2164 switch (*mg->mg_ptr) {
2165 case '\001': /* ^A */
2166 sv_setsv(PL_bodytarget, sv);
2168 case '\003': /* ^C */
2169 PL_minus_c = (bool)SvIV(sv);
2172 case '\004': /* ^D */
2174 s = SvPV_nolen_const(sv);
2175 PL_debug = get_debug_opts(&s, 0) | DEBUG_TOP_FLAG;
2176 DEBUG_x(dump_all());
2178 PL_debug = (SvIV(sv)) | DEBUG_TOP_FLAG;
2181 case '\005': /* ^E */
2182 if (*(mg->mg_ptr+1) == '\0') {
2183 #ifdef MACOS_TRADITIONAL
2184 gMacPerl_OSErr = SvIV(sv);
2187 set_vaxc_errno(SvIV(sv));
2190 SetLastError( SvIV(sv) );
2193 os2_setsyserrno(SvIV(sv));
2195 /* will anyone ever use this? */
2196 SETERRNO(SvIV(sv), 4);
2202 else if (strEQ(mg->mg_ptr+1, "NCODING")) {
2204 SvREFCNT_dec(PL_encoding);
2205 if (SvOK(sv) || SvGMAGICAL(sv)) {
2206 PL_encoding = newSVsv(sv);
2213 case '\006': /* ^F */
2214 PL_maxsysfd = SvIV(sv);
2216 case '\010': /* ^H */
2217 PL_hints = SvIV(sv);
2219 case '\011': /* ^I */ /* NOT \t in EBCDIC */
2220 Safefree(PL_inplace);
2221 PL_inplace = SvOK(sv) ? savesvpv(sv) : NULL;
2223 case '\017': /* ^O */
2224 if (*(mg->mg_ptr+1) == '\0') {
2225 Safefree(PL_osname);
2228 TAINT_PROPER("assigning to $^O");
2229 PL_osname = savesvpv(sv);
2232 else if (strEQ(mg->mg_ptr, "\017PEN")) {
2233 PL_compiling.cop_hints |= HINT_LEXICAL_IO;
2234 PL_hints |= HINT_LOCALIZE_HH | HINT_LEXICAL_IO;
2235 PL_compiling.cop_hints_hash
2236 = Perl_refcounted_he_new(aTHX_ PL_compiling.cop_hints_hash,
2237 sv_2mortal(newSVpvs("open")), sv);
2240 case '\020': /* ^P */
2241 PL_perldb = SvIV(sv);
2242 if (PL_perldb && !PL_DBsingle)
2245 case '\024': /* ^T */
2247 PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv));
2249 PL_basetime = (Time_t)SvIV(sv);
2252 case '\025': /* ^UTF8CACHE */
2253 if (strEQ(mg->mg_ptr+1, "TF8CACHE")) {
2254 PL_utf8cache = (signed char) sv_2iv(sv);
2257 case '\027': /* ^W & $^WARNING_BITS */
2258 if (*(mg->mg_ptr+1) == '\0') {
2259 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2261 PL_dowarn = (PL_dowarn & ~G_WARN_ON)
2262 | (i ? G_WARN_ON : G_WARN_OFF) ;
2265 else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
2266 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
2267 if (!SvPOK(sv) && PL_localizing) {
2268 sv_setpvn(sv, WARN_NONEstring, WARNsize);
2269 PL_compiling.cop_warnings = pWARN_NONE;
2274 int accumulate = 0 ;
2275 int any_fatals = 0 ;
2276 const char * const ptr = SvPV_const(sv, len) ;
2277 for (i = 0 ; i < len ; ++i) {
2278 accumulate |= ptr[i] ;
2279 any_fatals |= (ptr[i] & 0xAA) ;
2282 if (!specialWARN(PL_compiling.cop_warnings))
2283 PerlMemShared_free(PL_compiling.cop_warnings);
2284 PL_compiling.cop_warnings = pWARN_NONE;
2286 /* Yuck. I can't see how to abstract this: */
2287 else if (isWARN_on(((STRLEN *)SvPV_nolen_const(sv)) - 1,
2288 WARN_ALL) && !any_fatals) {
2289 if (!specialWARN(PL_compiling.cop_warnings))
2290 PerlMemShared_free(PL_compiling.cop_warnings);
2291 PL_compiling.cop_warnings = pWARN_ALL;
2292 PL_dowarn |= G_WARN_ONCE ;
2296 const char *const p = SvPV_const(sv, len);
2298 PL_compiling.cop_warnings
2299 = Perl_new_warnings_bitfield(aTHX_ PL_compiling.cop_warnings,
2302 if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
2303 PL_dowarn |= G_WARN_ONCE ;
2311 if (PL_localizing) {
2312 if (PL_localizing == 1)
2313 SAVESPTR(PL_last_in_gv);
2315 else if (SvOK(sv) && GvIO(PL_last_in_gv))
2316 IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv);
2319 Safefree(IoTOP_NAME(GvIOp(PL_defoutgv)));
2320 s = IoTOP_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
2321 IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
2324 Safefree(IoFMT_NAME(GvIOp(PL_defoutgv)));
2325 s = IoFMT_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv);
2326 IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO);
2329 IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIV(sv));
2332 IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIV(sv));
2333 if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L)
2334 IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L;
2337 IoPAGE(GvIOp(PL_defoutgv)) = (SvIV(sv));
2341 IO * const io = GvIOp(PL_defoutgv);
2344 if ((SvIV(sv)) == 0)
2345 IoFLAGS(io) &= ~IOf_FLUSH;
2347 if (!(IoFLAGS(io) & IOf_FLUSH)) {
2348 PerlIO *ofp = IoOFP(io);
2350 (void)PerlIO_flush(ofp);
2351 IoFLAGS(io) |= IOf_FLUSH;
2357 SvREFCNT_dec(PL_rs);
2358 PL_rs = newSVsv(sv);
2362 SvREFCNT_dec(PL_ors_sv);
2363 if (SvOK(sv) || SvGMAGICAL(sv)) {
2364 PL_ors_sv = newSVsv(sv);
2372 SvREFCNT_dec(PL_ofs_sv);
2373 if (SvOK(sv) || SvGMAGICAL(sv)) {
2374 PL_ofs_sv = newSVsv(sv);
2381 CopARYBASE_set(&PL_compiling, SvIV(sv));
2384 #ifdef COMPLEX_STATUS
2385 if (PL_localizing == 2) {
2386 PL_statusvalue = LvTARGOFF(sv);
2387 PL_statusvalue_vms = LvTARGLEN(sv);
2391 #ifdef VMSISH_STATUS
2393 STATUS_NATIVE_CHILD_SET((U32)SvIV(sv));
2396 STATUS_UNIX_EXIT_SET(SvIV(sv));
2401 # define PERL_VMS_BANG vaxc$errno
2403 # define PERL_VMS_BANG 0
2405 SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0,
2406 (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG);
2411 if (PL_delaymagic) {
2412 PL_delaymagic |= DM_RUID;
2413 break; /* don't do magic till later */
2416 (void)setruid((Uid_t)PL_uid);
2419 (void)setreuid((Uid_t)PL_uid, (Uid_t)-1);
2421 #ifdef HAS_SETRESUID
2422 (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1);
2424 if (PL_uid == PL_euid) { /* special case $< = $> */
2426 /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */
2427 if (PL_uid != 0 && PerlProc_getuid() == 0)
2428 (void)PerlProc_setuid(0);
2430 (void)PerlProc_setuid(PL_uid);
2432 PL_uid = PerlProc_getuid();
2433 Perl_croak(aTHX_ "setruid() not implemented");
2438 PL_uid = PerlProc_getuid();
2439 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2443 if (PL_delaymagic) {
2444 PL_delaymagic |= DM_EUID;
2445 break; /* don't do magic till later */
2448 (void)seteuid((Uid_t)PL_euid);
2451 (void)setreuid((Uid_t)-1, (Uid_t)PL_euid);
2453 #ifdef HAS_SETRESUID
2454 (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1);
2456 if (PL_euid == PL_uid) /* special case $> = $< */
2457 PerlProc_setuid(PL_euid);
2459 PL_euid = PerlProc_geteuid();
2460 Perl_croak(aTHX_ "seteuid() not implemented");
2465 PL_euid = PerlProc_geteuid();
2466 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2470 if (PL_delaymagic) {
2471 PL_delaymagic |= DM_RGID;
2472 break; /* don't do magic till later */
2475 (void)setrgid((Gid_t)PL_gid);
2478 (void)setregid((Gid_t)PL_gid, (Gid_t)-1);
2480 #ifdef HAS_SETRESGID
2481 (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1);
2483 if (PL_gid == PL_egid) /* special case $( = $) */
2484 (void)PerlProc_setgid(PL_gid);
2486 PL_gid = PerlProc_getgid();
2487 Perl_croak(aTHX_ "setrgid() not implemented");
2492 PL_gid = PerlProc_getgid();
2493 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2496 #ifdef HAS_SETGROUPS
2498 const char *p = SvPV_const(sv, len);
2499 Groups_t *gary = NULL;
2504 for (i = 0; i < NGROUPS; ++i) {
2505 while (*p && !isSPACE(*p))
2512 Newx(gary, i + 1, Groups_t);
2514 Renew(gary, i + 1, Groups_t);
2518 (void)setgroups(i, gary);
2521 #else /* HAS_SETGROUPS */
2523 #endif /* HAS_SETGROUPS */
2524 if (PL_delaymagic) {
2525 PL_delaymagic |= DM_EGID;
2526 break; /* don't do magic till later */
2529 (void)setegid((Gid_t)PL_egid);
2532 (void)setregid((Gid_t)-1, (Gid_t)PL_egid);
2534 #ifdef HAS_SETRESGID
2535 (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1);
2537 if (PL_egid == PL_gid) /* special case $) = $( */
2538 (void)PerlProc_setgid(PL_egid);
2540 PL_egid = PerlProc_getegid();
2541 Perl_croak(aTHX_ "setegid() not implemented");
2546 PL_egid = PerlProc_getegid();
2547 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
2550 PL_chopset = SvPV_force(sv,len);
2552 #ifndef MACOS_TRADITIONAL
2554 LOCK_DOLLARZERO_MUTEX;
2555 #ifdef HAS_SETPROCTITLE
2556 /* The BSDs don't show the argv[] in ps(1) output, they
2557 * show a string from the process struct and provide
2558 * the setproctitle() routine to manipulate that. */
2559 if (PL_origalen != 1) {
2560 s = SvPV_const(sv, len);
2561 # if __FreeBSD_version > 410001
2562 /* The leading "-" removes the "perl: " prefix,
2563 * but not the "(perl) suffix from the ps(1)
2564 * output, because that's what ps(1) shows if the
2565 * argv[] is modified. */
2566 setproctitle("-%s", s);
2567 # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */
2568 /* This doesn't really work if you assume that
2569 * $0 = 'foobar'; will wipe out 'perl' from the $0
2570 * because in ps(1) output the result will be like
2571 * sprintf("perl: %s (perl)", s)
2572 * I guess this is a security feature:
2573 * one (a user process) cannot get rid of the original name.
2575 setproctitle("%s", s);
2578 #elif defined(__hpux) && defined(PSTAT_SETCMD)
2579 if (PL_origalen != 1) {
2581 s = SvPV_const(sv, len);
2582 un.pst_command = (char *)s;
2583 pstat(PSTAT_SETCMD, un, len, 0, 0);
2586 if (PL_origalen > 1) {
2587 /* PL_origalen is set in perl_parse(). */
2588 s = SvPV_force(sv,len);
2589 if (len >= (STRLEN)PL_origalen-1) {
2590 /* Longer than original, will be truncated. We assume that
2591 * PL_origalen bytes are available. */
2592 Copy(s, PL_origargv[0], PL_origalen-1, char);
2595 /* Shorter than original, will be padded. */
2597 /* Special case for Mac OS X: see [perl #38868] */
2600 /* Is the space counterintuitive? Yes.
2601 * (You were expecting \0?)
2602 * Does it work? Seems to. (In Linux 2.4.20 at least.)
2604 const int pad = ' ';
2606 Copy(s, PL_origargv[0], len, char);
2607 PL_origargv[0][len] = 0;
2608 memset(PL_origargv[0] + len + 1,
2609 pad, PL_origalen - len - 1);
2611 PL_origargv[0][PL_origalen-1] = 0;
2612 for (i = 1; i < PL_origargc; i++)
2616 UNLOCK_DOLLARZERO_MUTEX;
2624 Perl_whichsig(pTHX_ const char *sig)
2626 register char* const* sigv;
2627 PERL_UNUSED_CONTEXT;
2629 for (sigv = (char* const*)PL_sig_name; *sigv; sigv++)
2630 if (strEQ(sig,*sigv))
2631 return PL_sig_num[sigv - (char* const*)PL_sig_name];
2633 if (strEQ(sig,"CHLD"))
2637 if (strEQ(sig,"CLD"))
2644 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
2645 Perl_sighandler(int sig, ...)
2647 Perl_sighandler(int sig)
2650 #ifdef PERL_GET_SIG_CONTEXT
2651 dTHXa(PERL_GET_SIG_CONTEXT);
2658 SV * const tSv = PL_Sv;
2662 XPV * const tXpv = PL_Xpv;
2664 if (PL_savestack_ix + 15 <= PL_savestack_max)
2666 if (PL_markstack_ptr < PL_markstack_max - 2)
2668 if (PL_scopestack_ix < PL_scopestack_max - 3)
2671 if (!PL_psig_ptr[sig]) {
2672 PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n",
2677 /* Max number of items pushed there is 3*n or 4. We cannot fix
2678 infinity, so we fix 4 (in fact 5): */
2680 PL_savestack_ix += 5; /* Protect save in progress. */
2681 SAVEDESTRUCTOR_X(S_unwind_handler_stack, (void*)&flags);
2684 PL_markstack_ptr++; /* Protect mark. */
2686 PL_scopestack_ix += 1;
2687 /* sv_2cv is too complicated, try a simpler variant first: */
2688 if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig]))
2689 || SvTYPE(cv) != SVt_PVCV) {
2691 cv = sv_2cv(PL_psig_ptr[sig], &st, &gv, GV_ADD);
2694 if (!cv || !CvROOT(cv)) {
2695 if (ckWARN(WARN_SIGNAL))
2696 Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n",
2697 PL_sig_name[sig], (gv ? GvENAME(gv)
2704 if(PL_psig_name[sig]) {
2705 sv = SvREFCNT_inc_NN(PL_psig_name[sig]);
2707 #if !defined(PERL_IMPLICIT_CONTEXT)
2711 sv = sv_newmortal();
2712 sv_setpv(sv,PL_sig_name[sig]);
2715 PUSHSTACKi(PERLSI_SIGNAL);
2718 #if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
2720 struct sigaction oact;
2722 if (sigaction(sig, 0, &oact) == 0 && oact.sa_flags & SA_SIGINFO) {
2726 va_start(args, sig);
2727 sip = (siginfo_t*)va_arg(args, siginfo_t*);
2730 SV *rv = newRV_noinc((SV*)sih);
2731 /* The siginfo fields signo, code, errno, pid, uid,
2732 * addr, status, and band are defined by POSIX/SUSv3. */
2733 hv_store(sih, "signo", 5, newSViv(sip->si_signo), 0);
2734 hv_store(sih, "code", 4, newSViv(sip->si_code), 0);
2735 #if 0 /* XXX TODO: Configure scan for the existence of these, but even that does not help if the SA_SIGINFO is not implemented according to the spec. */
2736 hv_store(sih, "errno", 5, newSViv(sip->si_errno), 0);
2737 hv_store(sih, "status", 6, newSViv(sip->si_status), 0);
2738 hv_store(sih, "uid", 3, newSViv(sip->si_uid), 0);
2739 hv_store(sih, "pid", 3, newSViv(sip->si_pid), 0);
2740 hv_store(sih, "addr", 4, newSVuv(PTR2UV(sip->si_addr)), 0);
2741 hv_store(sih, "band", 4, newSViv(sip->si_band), 0);
2745 PUSHs(newSVpv((char *)sip, sizeof(*sip)));
2754 call_sv((SV*)cv, G_DISCARD|G_EVAL);
2757 if (SvTRUE(ERRSV)) {
2759 #ifdef HAS_SIGPROCMASK
2760 /* Handler "died", for example to get out of a restart-able read().
2761 * Before we re-do that on its behalf re-enable the signal which was
2762 * blocked by the system when we entered.
2766 sigaddset(&set,sig);
2767 sigprocmask(SIG_UNBLOCK, &set, NULL);
2769 /* Not clear if this will work */
2770 (void)rsignal(sig, SIG_IGN);
2771 (void)rsignal(sig, PL_csighandlerp);
2773 #endif /* !PERL_MICRO */
2774 Perl_die(aTHX_ NULL);
2778 PL_savestack_ix -= 8; /* Unprotect save in progress. */
2782 PL_scopestack_ix -= 1;
2785 PL_op = myop; /* Apparently not needed... */
2787 PL_Sv = tSv; /* Restore global temporaries. */
2794 S_restore_magic(pTHX_ const void *p)
2797 MGS* const mgs = SSPTR(PTR2IV(p), MGS*);
2798 SV* const sv = mgs->mgs_sv;
2803 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv))
2805 #ifdef PERL_OLD_COPY_ON_WRITE
2806 /* While magic was saved (and off) sv_setsv may well have seen
2807 this SV as a prime candidate for COW. */
2809 sv_force_normal_flags(sv, 0);
2813 SvFLAGS(sv) |= mgs->mgs_flags;
2816 if (SvGMAGICAL(sv)) {
2817 /* downgrade public flags to private,
2818 and discard any other private flags */
2820 const U32 pubflags = SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK);
2822 SvFLAGS(sv) &= ~( pubflags | (SVp_IOK|SVp_NOK|SVp_POK) );
2823 SvFLAGS(sv) |= ( pubflags << PRIVSHIFT );
2828 mgs->mgs_sv = NULL; /* mark the MGS structure as restored */
2830 /* If we're still on top of the stack, pop us off. (That condition
2831 * will be satisfied if restore_magic was called explicitly, but *not*
2832 * if it's being called via leave_scope.)
2833 * The reason for doing this is that otherwise, things like sv_2cv()
2834 * may leave alloc gunk on the savestack, and some code
2835 * (e.g. sighandler) doesn't expect that...
2837 if (PL_savestack_ix == mgs->mgs_ss_ix)
2839 I32 popval = SSPOPINT;
2840 assert(popval == SAVEt_DESTRUCTOR_X);
2841 PL_savestack_ix -= 2;
2843 assert(popval == SAVEt_ALLOC);
2845 PL_savestack_ix -= popval;
2851 S_unwind_handler_stack(pTHX_ const void *p)
2854 const U32 flags = *(const U32*)p;
2857 PL_savestack_ix -= 5; /* Unprotect save in progress. */
2858 #if !defined(PERL_IMPLICIT_CONTEXT)
2860 SvREFCNT_dec(PL_sig_sv);
2865 =for apidoc magic_sethint
2867 Triggered by a store to %^H, records the key/value pair to
2868 C<PL_compiling.cop_hints_hash>. It is assumed that hints aren't storing
2869 anything that would need a deep copy. Maybe we should warn if we find a
2875 Perl_magic_sethint(pTHX_ SV *sv, MAGIC *mg)
2878 assert(mg->mg_len == HEf_SVKEY);
2880 /* mg->mg_obj isn't being used. If needed, it would be possible to store
2881 an alternative leaf in there, with PL_compiling.cop_hints being used if
2882 it's NULL. If needed for threads, the alternative could lock a mutex,
2883 or take other more complex action. */
2885 /* Something changed in %^H, so it will need to be restored on scope exit.
2886 Doing this here saves a lot of doing it manually in perl code (and
2887 forgetting to do it, and consequent subtle errors. */
2888 PL_hints |= HINT_LOCALIZE_HH;
2889 PL_compiling.cop_hints_hash
2890 = Perl_refcounted_he_new(aTHX_ PL_compiling.cop_hints_hash,
2891 (SV *)mg->mg_ptr, sv);
2896 =for apidoc magic_sethint
2898 Triggered by a delete from %^H, records the key to
2899 C<PL_compiling.cop_hints_hash>.
2904 Perl_magic_clearhint(pTHX_ SV *sv, MAGIC *mg)
2907 PERL_UNUSED_ARG(sv);
2909 assert(mg->mg_len == HEf_SVKEY);
2911 PERL_UNUSED_ARG(sv);
2913 PL_hints |= HINT_LOCALIZE_HH;
2914 PL_compiling.cop_hints_hash
2915 = Perl_refcounted_he_new(aTHX_ PL_compiling.cop_hints_hash,
2916 (SV *)mg->mg_ptr, &PL_sv_placeholder);
2922 * c-indentation-style: bsd
2924 * indent-tabs-mode: t
2927 * ex: set ts=8 sts=4 sw=4 noet: