This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
sv_force_normal: Don’t confuse regexps with cows
[perl5.git] / sv.h
diff --git a/sv.h b/sv.h
index bbf41c8..935f4ff 100644 (file)
--- a/sv.h
+++ b/sv.h
@@ -309,10 +309,10 @@ perform the upgrade if necessary.  See C<svtype>.
                                       subroutine in another package. Set the
                                       CvIMPORTED_CV_ON() if it needs to be
                                       expanded to a real GV */
-
-#define SVs_PADSTALE   0x00010000  /* lexical has gone out of scope */
-#define SVpad_STATE    0x00010000  /* pad name is a "state" var */
-#define SVs_PADTMP     0x00020000  /* in use as tmp */
+/*                      0x00010000  *** FREE SLOT */
+#define SVs_PADTMP     0x00020000  /* in use as tmp; only if ! SVs_PADMY */
+#define SVs_PADSTALE   0x00020000  /* lexical has gone out of scope;
+                                       only valid for SVs_PADMY */
 #define SVpad_TYPED    0x00020000  /* pad name is a Typed Lexical */
 #define SVs_PADMY      0x00040000  /* in use a "my" variable */
 #define SVpad_OUR      0x00040000  /* pad name is "our" instead of "my" */
@@ -322,7 +322,7 @@ perform the upgrade if necessary.  See C<svtype>.
 #define SVs_SMG                0x00400000  /* has magical set method */
 #define SVs_RMG                0x00800000  /* has random magical methods */
 
-#define SVf_FAKE       0x01000000  /* 0: glob or lexical is just a copy
+#define SVf_FAKE       0x01000000  /* 0: glob is just a copy
                                       1: SV head arena wasn't malloc()ed
                                       2: in conjunction with SVf_READONLY
                                          marks a shared hash key scalar
@@ -409,6 +409,8 @@ perform the upgrade if necessary.  See C<svtype>.
 #define SVpbm_TAIL     0x80000000
 /* RV upwards. However, SVf_ROK and SVp_IOK are exclusive  */
 #define SVprv_WEAKREF   0x80000000  /* Weak reference */
+/* pad name vars only */
+#define SVpad_STATE    0x80000000  /* pad name is a "state" var */
 
 #define _XPV_HEAD                                                      \
     HV*                xmg_stash;      /* class package */                     \
@@ -480,6 +482,7 @@ struct xpvlv {
     SV*                xlv_targ;
     char       xlv_type;       /* k=keys .=pos x=substr v=vec /=join/re
                                 * y=alem/helem/iter t=tie T=tied HE */
+    char       xlv_flags;      /* 1 = negative offset  2 = negative len */
 };
 
 /* This structure works in 3 ways - regular scalar, GV with GP, or fast
@@ -669,20 +672,23 @@ Dereferences an RV to return the SV.
 
 =for apidoc Am|IV|SvIVX|SV* sv
 Returns the raw value in the SV's IV slot, without checks or conversions.
-Only use when you are sure SvIOK is true. See also C<SvIV()>.
+Only use when you are sure SvIOK is true.  See also C<SvIV()>.
 
 =for apidoc Am|UV|SvUVX|SV* sv
 Returns the raw value in the SV's UV slot, without checks or conversions.
-Only use when you are sure SvIOK is true. See also C<SvUV()>.
+Only use when you are sure SvIOK is true.  See also C<SvUV()>.
 
 =for apidoc Am|NV|SvNVX|SV* sv
 Returns the raw value in the SV's NV slot, without checks or conversions.
-Only use when you are sure SvNOK is true. See also C<SvNV()>.
+Only use when you are sure SvNOK is true.  See also C<SvNV()>.
 
 =for apidoc Am|char*|SvPVX|SV* sv
 Returns a pointer to the physical string in the SV.  The SV must contain a
 string.
 
+This is also used to store the name of an autoloaded subroutine in an XS
+AUTOLOAD routine.  See L<perlguts/Autoloading with XSUBs>.
+
 =for apidoc Am|STRLEN|SvCUR|SV* sv
 Returns the length of the string which is in the SV.  See C<SvLEN>.
 
@@ -691,9 +697,14 @@ Returns the size of the string buffer in the SV, not including any part
 attributable to C<SvOOK>.  See C<SvCUR>.
 
 =for apidoc Am|char*|SvEND|SV* sv
-Returns a pointer to the last character in the string which is in the SV.
+Returns a pointer to the spot just after the last character in
+the string which is in the SV, where there is usually a trailing
+null (even though Perl scalars do not strictly require it).
 See C<SvCUR>.  Access the character as *(SvEND(sv)).
 
+Warning: If C<SvCUR> is equal to C<SvLEN>, then C<SvEND> points to
+unallocated memory.
+
 =for apidoc Am|HV*|SvSTASH|SV* sv
 Returns the stash of the SV.
 
@@ -838,7 +849,7 @@ in gv.h: */
                                 ? mg_find(sv,PERL_MAGIC_vstring) : NULL)
 
 #define SvOOK(sv)              (SvFLAGS(sv) & SVf_OOK)
-#define SvOOK_on(sv)           ((void)SvIOK_off(sv), SvFLAGS(sv) |= SVf_OOK)
+#define SvOOK_on(sv)           (SvFLAGS(sv) |= SVf_OOK)
 #define SvOOK_off(sv)          ((void)(SvOOK(sv) && sv_backoff(sv)))
 
 #define SvFAKE(sv)             (SvFLAGS(sv) & SVf_FAKE)
@@ -884,10 +895,12 @@ in gv.h: */
 /*
 =for apidoc Am|U32|SvGAMAGIC|SV* sv
 
-Returns true if the SV has get magic or overloading. If either is true then
+Returns true if the SV has get magic or
+overloading.  If either is true then
 the scalar is active data, and has the potential to return a new value every
-time it is accessed. Hence you must be careful to only read it once per user
-logical operation and work with that returned value. If neither is true then
+time it is accessed.  Hence you must be careful to
+only read it once per user logical operation and work
+with that returned value.  If neither is true then
 the scalar's value cannot change unless written to.
 
 =cut
@@ -909,34 +922,41 @@ the scalar's value cannot change unless written to.
 
 #define SvTHINKFIRST(sv)       (SvFLAGS(sv) & SVf_THINKFIRST)
 
-#define SvPADSTALE(sv)         (SvFLAGS(sv) & SVs_PADSTALE)
-#define SvPADSTALE_off(sv)     (SvFLAGS(sv) &= ~SVs_PADSTALE)
+#define SvPADMY(sv)            (SvFLAGS(sv) & SVs_PADMY)
+#define SvPADMY_on(sv)         (SvFLAGS(sv) |= SVs_PADMY)
 
-#define SvPADTMP(sv)           (SvFLAGS(sv) & SVs_PADTMP)
-#define SvPADTMP_off(sv)       (SvFLAGS(sv) &= ~SVs_PADTMP)
+/* SVs_PADTMP and SVs_PADSTALE share the same bit, mediated by SVs_PADMY */
 
-#define SvPADMY(sv)            (SvFLAGS(sv) & SVs_PADMY)
+#define SvPADTMP(sv)   ((SvFLAGS(sv) & (SVs_PADMY|SVs_PADTMP)) == SVs_PADTMP)
+#define SvPADSTALE(sv) ((SvFLAGS(sv) & (SVs_PADMY|SVs_PADSTALE)) \
+                                   == (SVs_PADMY|SVs_PADSTALE))
 
 #if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
-#  define SvPADTMP_on(sv)      ({                                      \
-           SV *const _svpad = MUTABLE_SV(sv);                          \
-           assert(!(SvFLAGS(_svpad) & (SVs_PADMY|SVs_PADSTALE)));      \
-           (SvFLAGS(_svpad) |= SVs_PADTMP);            \
+#  define SvPADTMP_on(sv)      ({                      \
+           SV *const _svpad = MUTABLE_SV(sv);          \
+           assert(!(SvFLAGS(_svpad) & SVs_PADMY));     \
+           SvFLAGS(_svpad) |= SVs_PADTMP;              \
        })
-#  define SvPADMY_on(sv)       ({                                      \
-           SV *const _svpad = MUTABLE_SV(sv);                          \
-           assert(!(SvFLAGS(_svpad) & SVs_PADTMP));    \
-           (SvFLAGS(_svpad) |= SVs_PADMY);             \
+#  define SvPADTMP_off(sv)     ({                      \
+           SV *const _svpad = MUTABLE_SV(sv);          \
+           assert(!(SvFLAGS(_svpad) & SVs_PADMY));     \
+           SvFLAGS(_svpad) &= ~SVs_PADTMP;             \
        })
-#  define SvPADSTALE_on(sv)    ({                                      \
-           SV *const _svpad = MUTABLE_SV(sv);                          \
-           assert(!(SvFLAGS(_svpad) & SVs_PADTMP));    \
-           (SvFLAGS(_svpad) |= SVs_PADSTALE);          \
+#  define SvPADSTALE_on(sv)    ({                      \
+           SV *const _svpad = MUTABLE_SV(sv);          \
+           assert(SvFLAGS(_svpad) & SVs_PADMY);        \
+           SvFLAGS(_svpad) |= SVs_PADSTALE;            \
+       })
+#  define SvPADSTALE_off(sv)   ({                      \
+           SV *const _svpad = MUTABLE_SV(sv);          \
+           assert(SvFLAGS(_svpad) & SVs_PADMY);        \
+           SvFLAGS(_svpad) &= ~SVs_PADSTALE;           \
        })
 #else
 #  define SvPADTMP_on(sv)      (SvFLAGS(sv) |= SVs_PADTMP)
-#  define SvPADMY_on(sv)       (SvFLAGS(sv) |= SVs_PADMY)
+#  define SvPADTMP_off(sv)     (SvFLAGS(sv) &= ~SVs_PADTMP)
 #  define SvPADSTALE_on(sv)    (SvFLAGS(sv) |= SVs_PADSTALE)
+#  define SvPADSTALE_off(sv)   (SvFLAGS(sv) &= ~SVs_PADSTALE)
 #endif
 
 #define SvTEMP(sv)             (SvFLAGS(sv) & SVs_TEMP)
@@ -1269,10 +1289,6 @@ the scalar's value cannot change unless written to.
                 } STMT_END
 #endif
 
-/* SvPOKp not SvPOK in the assertion because the string can be tainted! eg
-   perl -T -e '/$^X/'
-*/
-
 #ifndef PERL_CORE
 #  define BmFLAGS(sv)          (SvTAIL(sv) ? FBMcf_TAIL : 0)
 #endif
@@ -1310,6 +1326,7 @@ the scalar's value cannot change unless written to.
 #define LvTARG(sv)     ((XPVLV*)  SvANY(sv))->xlv_targ
 #define LvTARGOFF(sv)  ((XPVLV*)  SvANY(sv))->xlv_targoff
 #define LvTARGLEN(sv)  ((XPVLV*)  SvANY(sv))->xlv_targlen
+#define LvFLAGS(sv)    ((XPVLV*)  SvANY(sv))->xlv_flags
 
 #define IoIFP(sv)      (sv)->sv_u.svu_fp
 #define IoOFP(sv)      ((XPVIO*)  SvANY(sv))->xio_ofp
@@ -1342,22 +1359,26 @@ the scalar's value cannot change unless written to.
 
 /*
 =for apidoc Am|bool|SvTAINTED|SV* sv
-Checks to see if an SV is tainted. Returns TRUE if it is, FALSE if
+Checks to see if an SV is tainted.  Returns TRUE if it is, FALSE if
 not.
 
 =for apidoc Am|void|SvTAINTED_on|SV* sv
 Marks an SV as tainted if tainting is enabled.
 
 =for apidoc Am|void|SvTAINTED_off|SV* sv
-Untaints an SV. Be I<very> careful with this routine, as it short-circuits
-some of Perl's fundamental security features. XS module authors should not
+Untaints an SV.  Be I<very> careful with this routine, as it short-circuits
+some of Perl's fundamental security features.  XS module authors should not
 use this function unless they fully understand all the implications of
 unconditionally untainting the value. Untainting should be done in the
 standard perl fashion, via a carefully crafted regexp, rather than directly
 untainting variables.
 
 =for apidoc Am|void|SvTAINT|SV* sv
-Taints an SV if tainting is enabled.
+Taints an SV if tainting is enabled, and if some input to the current
+expression is tainted--usually a variable, but possibly also implicit
+inputs such as locale settings.  C<SvTAINT> propagates that taintedness to
+the outputs of an expression in a pessimistic fashion; i.e., without paying
+attention to precisely which outputs are influenced by which inputs.
 
 =cut
 */
@@ -1385,18 +1406,18 @@ directly.
 =for apidoc Am|char*|SvPV_force_nomg|SV* sv|STRLEN len
 Like C<SvPV> but will force the SV into containing just a string
 (C<SvPOK_only>).  You want force if you are going to update the C<SvPVX>
-directly. Doesn't process magic.
+directly.  Doesn't process magic.
 
 =for apidoc Am|char*|SvPV|SV* sv|STRLEN len
 Returns a pointer to the string in the SV, or a stringified form of
 the SV if the SV does not contain a string.  The SV may cache the
-stringified version becoming C<SvPOK>.  Handles 'get' magic. See also
+stringified version becoming C<SvPOK>.  Handles 'get' magic.  See also
 C<SvPVx> for a version which guarantees to evaluate sv only once.
 
 =for apidoc Am|char*|SvPVx|SV* sv|STRLEN len
 A version of C<SvPV> which guarantees to evaluate C<sv> only once.
 Only use this if C<sv> is an expression with side effects, otherwise use the
-more efficient C<SvPVX>.
+more efficient C<SvPV>.
 
 =for apidoc Am|char*|SvPV_nomg|SV* sv|STRLEN len
 Like C<SvPV> but doesn't process magic.
@@ -1410,27 +1431,29 @@ stringified form becoming C<SvPOK>.  Handles 'get' magic.
 Like C<SvPV_nolen> but doesn't process magic.
 
 =for apidoc Am|IV|SvIV|SV* sv
-Coerces the given SV to an integer and returns it. See C<SvIVx> for a
+Coerces the given SV to an integer and returns it.  See C<SvIVx> for a
 version which guarantees to evaluate sv only once.
 
 =for apidoc Am|IV|SvIV_nomg|SV* sv
 Like C<SvIV> but doesn't process magic.
 
 =for apidoc Am|IV|SvIVx|SV* sv
-Coerces the given SV to an integer and returns it. Guarantees to evaluate
-C<sv> only once. Only use this if C<sv> is an expression with side effects,
+Coerces the given SV to an integer and returns it.
+Guarantees to evaluate C<sv> only once.  Only use
+this if C<sv> is an expression with side effects,
 otherwise use the more efficient C<SvIV>.
 
 =for apidoc Am|NV|SvNV|SV* sv
-Coerce the given SV to a double and return it. See C<SvNVx> for a version
+Coerce the given SV to a double and return it.  See C<SvNVx> for a version
 which guarantees to evaluate sv only once.
 
 =for apidoc Am|NV|SvNV_nomg|SV* sv
 Like C<SvNV> but doesn't process magic.
 
 =for apidoc Am|NV|SvNVx|SV* sv
-Coerces the given SV to a double and returns it. Guarantees to evaluate
-C<sv> only once. Only use this if C<sv> is an expression with side effects,
+Coerces the given SV to a double and returns it.
+Guarantees to evaluate C<sv> only once.  Only use
+this if C<sv> is an expression with side effects,
 otherwise use the more efficient C<SvNV>.
 
 =for apidoc Am|UV|SvUV|SV* sv
@@ -1441,8 +1464,9 @@ for a version which guarantees to evaluate sv only once.
 Like C<SvUV> but doesn't process magic.
 
 =for apidoc Am|UV|SvUVx|SV* sv
-Coerces the given SV to an unsigned integer and returns it. Guarantees to
-C<sv> only once. Only use this if C<sv> is an expression with side effects,
+Coerces the given SV to an unsigned integer and
+returns it.  Guarantees to C<sv> only once.  Only
+use this if C<sv> is an expression with side effects,
 otherwise use the more efficient C<SvUV>.
 
 =for apidoc Am|bool|SvTRUE|SV* sv
@@ -1494,9 +1518,9 @@ Guarantees to evaluate sv only once; use the more efficient C<SvPVbyte>
 otherwise.
 
 =for apidoc Am|bool|SvIsCOW|SV* sv
-Returns a boolean indicating whether the SV is Copy-On-Write. (either shared
+Returns a boolean indicating whether the SV is Copy-On-Write (either shared
 hash key scalars, or full Copy On Write scalars if 5.9.0 is configured for
-COW)
+COW).
 
 =for apidoc Am|bool|SvIsCOW_shared_hash|SV* sv
 Returns a boolean indicating whether the SV is Copy-On-Write shared hash key
@@ -1516,7 +1540,7 @@ Like C<sv_catsv> but doesn't process magic.
 
 =for apidoc Amdb|STRLEN|sv_utf8_upgrade_nomg|NN SV *sv
 
-Like sv_utf8_upgrade, but doesn't do magic on C<sv>
+Like sv_utf8_upgrade, but doesn't do magic on C<sv>.
 
 =cut
 */
@@ -1721,7 +1745,8 @@ Like sv_utf8_upgrade, but doesn't do magic on C<sv>
 #endif /* __GNU__ */
 
 #define SvIsCOW(sv)    ((SvFLAGS(sv) & (SVf_FAKE | SVf_READONLY)) == \
-                          (SVf_FAKE | SVf_READONLY) && !isGV_with_GP(sv))
+                          (SVf_FAKE | SVf_READONLY) && !isGV_with_GP(sv) \
+                          && SvTYPE(sv) != SVt_REGEXP)
 #define SvIsCOW_shared_hash(sv)        (SvIsCOW(sv) && SvLEN(sv) == 0)
 
 #define SvSHARED_HEK_FROM_PV(pvx) \
@@ -1793,7 +1818,7 @@ Like sv_utf8_upgrade, but doesn't do magic on C<sv>
 #else
 #  define SvRELEASE_IVX(sv)   0
 /* This little game brought to you by the need to shut this warning up:
-mg.c: In function `Perl_magic_get':
+mg.c: In function 'Perl_magic_get':
 mg.c:1024: warning: left-hand operand of comma expression has no effect
 */
 #  define SvRELEASE_IVX_(sv)  /**/
@@ -1889,7 +1914,7 @@ more than once.
 
 =for apidoc Am|void|SvSetSV_nosteal|SV* dsv|SV* ssv
 Calls a non-destructive version of C<sv_setsv> if dsv is not the same as
-ssv. May evaluate arguments more than once.
+ssv.  May evaluate arguments more than once.
 
 =for apidoc Am|void|SvSetMagicSV|SV* dsb|SV* ssv
 Like C<SvSetSV>, but does any set magic required afterwards.
@@ -1962,6 +1987,16 @@ Returns a pointer to the character buffer.
 
 #define SvIMMORTAL(sv) ((sv)==&PL_sv_undef || (sv)==&PL_sv_yes || (sv)==&PL_sv_no || (sv)==&PL_sv_placeholder)
 
+/*
+=for apidoc Am|SV *|boolSV|bool b
+
+Returns a true SV if C<b> is a true value, or a false SV if C<b> is 0.
+
+See also C<PL_sv_yes> and C<PL_sv_no>.
+
+=cut
+*/
+
 #define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
 
 #define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
@@ -2018,9 +2053,9 @@ C<SvUTF8_on> on the new SV.  Implemented as a wrapper around C<newSVpvn_flags>.
 
 Reads into I<len> the offset from SvPVX back to the true start of the
 allocated buffer, which will be non-zero if C<sv_chop> has been used to
-efficiently remove characters from start of the buffer. Implemented as a
+efficiently remove characters from start of the buffer.  Implemented as a
 macro, which takes the address of I<len>, which must be of type C<STRLEN>.
-Evaluates I<sv> more than once. Sets I<len> to 0 if C<SvOOK(sv)> is false.
+Evaluates I<sv> more than once.  Sets I<len> to 0 if C<SvOOK(sv)> is false.
 
 =cut
 */