develooper Front page | perl.perl5.porters | Postings from April 2011

[perl #89514] Inconsistent & nonintuitive initialization of private "my" variables in a scoped code block

Thread Next
From:
perl @ g . zazu . com
Date:
April 29, 2011 02:23
Subject:
[perl #89514] Inconsistent & nonintuitive initialization of private "my" variables in a scoped code block
Message ID:
rt-3.6.HEAD-32285-1304029470-19.89514-75-0@perl.org
# New Ticket Created by  perl@g.zazu.com 
# Please include the string:  [perl #89514]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=89514 >



This is a bug report for perl from perl@g.zazu.com,
generated with the help of perlbug 1.39 running under perl 5.10.1.


-----------------------------------------------------------------
[Please describe your issue here]

There seems to be a bug (or at least mis-documented non-intuitive handling)
with persisting scoped private "my" variables.  The following code snippet
illustrates the perl private variable misbehavior:

#######################

use strict;

print "Run 1:\n";
foreach (1 .. 2) {
    my $var;
    print "var = $var\n";
    $var = 5;
}

print "\nRun 2:\n";
foreach (1 .. 2) {
    my $var = 1  if 2 == 3;
    print "var = $var\n";
    $var = 5;
}

########################

Run 1:
var = 
var = 

Run 2:
var = 
var = 5

########################

Note that $var has a previous value in Run 2 on the second pass into the loop. 
Whether this is an actual perl language bug or not, it is certainly
counter-intuitive:  in other languages (e.g., C, C++, java), this would be a
stack variable that is allocated on entry into the code block and released at
the end of the code block.  Perl seems to be using a more permanent heap
variable that should be garbage-collected when refcount=0 but is being reused. 
In case the above code snippet doesn't clearly show that the refcount = 0 at
the top of the loop block, perhaps the following more clearly illustrates the
misbehavior (the run results matches above):

########################

foreach (1 .. 2) {
    &try;
}

sub try
{
    my $var = 1  if 2 == 3;
    print "var = $var\n";
    $var = 5;
}

########################

I later discovered that the perl behavior is even more inconsistent than
implied by the above example.  The following code snippet illustrates the
additonal twist:

########################

use strict;

foreach my $i (-3 .. 3) {
    my $var = 2  if !$i;
    print "var: $var \t(i: $i)\n";
    $var = 5;
}

########################

var:    (i: -3)
var: 5  (i: -2)
var: 5  (i: -1)
var: 2  (i: 0)
var:    (i: 1)
var: 5  (i: 2)
var: 5  (i: 3)

########################

Notice that the first three iterations above exhibit the same behavior as noted
previously.  Then, the 4th iteration (i=0) properly executes the expected
conditional initialization.  The twist is with the 5th iteration (i=1), which
again properly initializes to undef when the conditional fails, only to return
to the misbehavior after that (i=2).  So, it's not simply the case that the
private variable always persists with the previous value unless the conditional
initialization occurs.

[Please do not change anything below this line]
-----------------------------------------------------------------
---
Flags:
    category=core
    severity=high
---
This perlbug was built using Perl 5.10.1 in the Fedora build system.
It is being executed now by Perl 5.10.1 - Tue Apr  5 07:59:51 UTC 2011.

Site configuration information for perl 5.10.1:

Configured by Red Hat, Inc. at Tue Apr  5 07:59:51 UTC 2011.

Summary of my perl5 (revision 5 version 10 subversion 1) configuration:
   
  Platform:
    osname=linux, osvers=2.6.32-71.14.1.el6.x86_64, archname=x86_64-linux-thread-multi
    uname='linux x86-06.phx2.fedoraproject.org 2.6.32-71.14.1.el6.x86_64 #1 smp wed jan 5 17:01:01 est 2011 x86_64 x86_64 x86_64 gnulinux '
    config_args='-des -Doptimize=-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Dccdlflags=-Wl,--enable-new-dtags -DDEBUGGING=-g -Dversion=5.10.1 -Dmyhostname=localhost -Dperladmin=root@localhost -Dcc=gcc -Dcf_by=Red Hat, Inc. -Dprefix=/usr -Dvendorprefix=/usr -Dsiteprefix=/usr/local -Dsitelib=/usr/local/share/perl5 -Dsitearch=/usr/local/lib64/perl5 -Dprivlib=/usr/share/perl5 -Dvendorlib=/usr/share/perl5 -Darchlib=/usr/lib64/perl5 -Dvendorarch=/usr/lib64/perl5 -Dinc_version_list=5.10.0 -Darchname=x86_64-linux-thread-multi -Dlibpth=/usr/local/lib64 /lib64 /usr/lib64 -Duseshrplib -Dusethreads -Duseithreads -Duselargefiles -Dd_dosuid -Dd_semctl_semun -Di_db -Ui_ndbm -Di_gdbm -Di_shadow -Di_syslog -Dman3ext=3pm -Duseperlio -Dinstallusrbinperl=n -Ubincompat5005 -Uversiononly -Dpager=/usr/bin/less -isr -Dd_gethostent_r_proto -Ud_endhostent_r_proto -Ud_sethostent_r_proto -Ud_endprotoent_r_proto -Ud_setprotoen!
 t_r_proto -Ud_endservent_r_proto -Ud_setservent_r_proto -Dscriptdir=/usr/bin -Dotherlibdirs=/usr/local/lib64/perl5/site_perl/5.10.0/x86_64-linux-thread-multi:/usr/local/lib/perl5/site_perl/5.10.0:/usr/lib64/perl5/vendor_perl/5.10.0/x86_64-linux-thread-multi:/usr/lib/perl5/vendor_perl:/usr/lib/perl5/site_perl'
    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='gcc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic',
    cppflags='-D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include'
    ccversion='', gccversion='4.4.5 20101112 (Red Hat 4.4.5-2)', 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='gcc', ldflags =' -fstack-protector'
    libpth=/usr/local/lib64 /lib64 /usr/lib64
    libs=-lresolv -lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lpthread -lc
    perllibs=-lresolv -lnsl -ldl -lm -lcrypt -lutil -lpthread -lc
    libc=, so=so, useshrplib=true, libperl=libperl.so
    gnulibc_version='2.12.2'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,--enable-new-dtags -Wl,-rpath,/usr/lib64/perl5/CORE'
    cccdlflags='-fPIC', lddlflags='-shared -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'

Locally applied patches:
    

---
@INC for perl 5.10.1:
    /usr/local/lib64/perl5
    /usr/local/share/perl5
    /usr/lib64/perl5
    /usr/share/perl5
    /usr/lib64/perl5
    /usr/share/perl5
    /usr/local/lib64/perl5/site_perl/5.10.0/x86_64-linux-thread-multi
    /usr/local/lib/perl5/site_perl/5.10.0
    /usr/lib64/perl5/vendor_perl/5.10.0/x86_64-linux-thread-multi
    /usr/lib/perl5/vendor_perl/5.10.0
    /usr/lib/perl5/vendor_perl
    /usr/lib/perl5/site_perl
    .

---
Environment for perl 5.10.1:
    HOME=/home/glenn
    LANG=en_US.UTF-8
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=/usr/lib64/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/sbin:/usr/sbin:/usr/local/sbin
    PERL_BADLANG (unset)
    SHELL=/bin/bash


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