develooper Front page | perl.perl5.porters | Postings from July 2008

is $1 really readonly?

Thread Next
From:
Torsten Foertsch
Date:
July 17, 2008 01:47
Subject:
is $1 really readonly?
Message ID:
200807171047.08955.torsten.foertsch@gmx.net
Hi,

when I call read() with an readonly buffer I'd expect that perl throws an 
exception and doesn't read anything.

But see:

$ echo huhu | 
  perl -MDevel::Peek -le '".............."=~/(.*)/;
                          Dump($1);
                          eval {read STDIN, $1, 2, 3};
                          Dump($1);
                          print "$1: $@"'
SV = PVMG(0x7c4308) at 0x7d9a88
  REFCNT = 1
  FLAGS = (GMG,SMG)
  IV = 0
  NV = 0
  PV = 0
  MAGIC = 0x7d2118
    MG_VIRTUAL = &PL_vtbl_sv
    MG_TYPE = PERL_MAGIC_sv(\0)
    MG_OBJ = 0x7d9a70
    MG_LEN = 1
    MG_PTR = 0x7d1ed8 "1"
SV = PVMG(0x7c4308) at 0x7d9a88
  REFCNT = 1
  FLAGS = (GMG,SMG,pPOK)
  IV = 0
  NV = 0
  PV = 0x7ac8b8 "\0\0\0hu"\0
  CUR = 5
  LEN = 8
  MAGIC = 0x7d2118
    MG_VIRTUAL = &PL_vtbl_sv
    MG_TYPE = PERL_MAGIC_sv(\0)
    MG_OBJ = 0x7d9a70
    MG_LEN = 1
    MG_PTR = 0x7d1ed8 "1"
..............: Modification of a read-only value attempted at -e line 1.

It throws an exception but it also eats input. Further, it modifies the PV 
value of $1. Sysread behaves the same.

Is that expected/known?

And how can I know in XS if a variable should not be modified? The READONLY 
flag is not set on $1 but is set on $^V although both are documented as 
read-only.

Also, if I use $^V instead of $1 in the code above I don't get a "Modification 
of a read-only..." exception but "Can't coerce readonly REF to string in 
sysread". Now it doesn't consume any input.

But simply assigning something to $^V throws the read-only exception.

Torsten

Summary of my perl5 (revision 5 version 10 subversion 0) configuration:
  Platform:
    osname=linux, osvers=2.6.25, archname=x86_64-linux-thread-multi
    uname='linux kimball 2.6.25 #1 smp 20080201 19:36:55 utc x86_64 x86_64 
x86_64 gnulinux '
    
config_args='-ds -e -Dprefix=/usr -Dvendorprefix=/usr -Dinstallusrbinperl -Dusethreads -Di_db -Di_dbm -Di_ndbm -Di_gdbm -Duseshrplib=true -Doptimize=-O2 -fmessage-length=0 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector -g -Wall -pipe -Accflags=-DPERL_USE_SAFE_PUTENV'
    hint=recommended, useposix=true, d_sigaction=define
    useithreads=define, usemultiplicity=define
    useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
    use64bitint=define, use64bitall=define, uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='cc', ccflags 
='-D_REENTRANT -D_GNU_SOURCE -DPERL_USE_SAFE_PUTENV -DDEBUGGING -fno-strict-aliasing -pipe -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    
optimize='-O2 -fmessage-length=0 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector -g -Wall -pipe',
    
cppflags='-D_REENTRANT -D_GNU_SOURCE -DPERL_USE_SAFE_PUTENV -DDEBUGGING -fno-strict-aliasing -pipe'
    ccversion='', gccversion='4.3.1 20080507 (prerelease) [gcc-4_3-branch 
revision 135036]', gccosandvers=''
    intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
    ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', 
lseeksize=8
    alignbytes=8, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -L/usr/local/lib64'
    libpth=/lib64 /usr/lib64 /usr/local/lib64
    libs=-lm -ldl -lcrypt -lpthread
    perllibs=-lm -ldl -lcrypt -lpthread
    libc=/lib64/libc-2.8.so, so=so, useshrplib=true, libperl=libperl.so
    gnulibc_version='2.8'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, 
ccdlflags='-Wl,-E -Wl,-rpath,/usr/lib/perl5/5.10.0/x86_64-linux-thread-multi/CORE'
    cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib64'


Characteristics of this binary (from libperl):
  Compile-time options: DEBUGGING MULTIPLICITY PERL_DONT_CREATE_GVSV
                        PERL_IMPLICIT_CONTEXT PERL_MALLOC_WRAP
                        PERL_TRACK_MEMPOOL PERL_USE_SAFE_PUTENV
                        USE_64_BIT_ALL USE_64_BIT_INT USE_ITHREADS
                        USE_LARGE_FILES USE_PERLIO USE_REENTRANT_API
  Built under linux
  Compiled at Jun  6 2008 19:30:18
  %ENV:
    PERL5LIB="/opt/mod_perl"
  @INC:
    /opt/mod_perl/5.10.0/x86_64-linux-thread-multi
    /opt/mod_perl/5.10.0
    /opt/mod_perl
    /usr/lib/perl5/5.10.0/x86_64-linux-thread-multi
    /usr/lib/perl5/5.10.0
    /usr/lib/perl5/site_perl/5.10.0/x86_64-linux-thread-multi
    /usr/lib/perl5/site_perl/5.10.0
    /usr/lib/perl5/vendor_perl/5.10.0/x86_64-linux-thread-multi
    /usr/lib/perl5/vendor_perl/5.10.0
    /usr/lib/perl5/vendor_perl
    .


--
Need professional mod_perl support?
Just hire me: torsten.foertsch@gmx.net

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About