Re: Can't pack literals as pointers
John Tobey [Tue, 24 Jun 1997 11:44:33 +0000 (23:44 +1200)]
MHO, pack("p","foo") should evaluate to a pointer that's valid in the
urrent context.  pack("p",undef) should return the NULL value.
urrently, they both produce the error "Modification of a read-only
alue attempted".

This looks pretty easy to fix, so I've prepared a diff against the
5.004_01 distribution.  This tests fine on my Linux.  I hope I'm not
introducing a memory leak or other ailment...

Credited: Tim Bunce <Tim.Bunce@ig.co.uk>
Credited: Gurusamy Sarathy <gsar@engin.umich.edu>

pp.c

diff --git a/pp.c b/pp.c
index 1cea53f..fa36c50 100644 (file)
--- a/pp.c
+++ b/pp.c
@@ -3834,7 +3834,14 @@ PP(pp_pack)
        case 'p':
            while (len-- > 0) {
                fromstr = NEXTFROM;
-               aptr = SvPV_force(fromstr, na); /* XXX Error if TEMP? */
+               if (fromstr == &sv_undef)
+                 aptr = NULL;
+               else {
+                 if (SvREADONLY(fromstr) && curcop != &compiling) {
+                   fromstr = sv_mortalcopy(fromstr);
+                 }
+                 aptr = SvPV_force(fromstr, na);
+               }
                sv_catpvn(cat, (char*)&aptr, sizeof(char*));
            }
            break;