develooper Front page | perl.perl5.porters | Postings from June 2002

[ID 20020626.011] wantarray() causes clobbering of unrelated varsoutside the sub

Thread Next
Tassilo von Parseval
June 26, 2002 11:08
[ID 20020626.011] wantarray() causes clobbering of unrelated varsoutside the sub
Message ID:

This is a bug report for perl from,
generated with the help of perlbug 1.34 running under perl v5.8.0.

[Please enter your report here]

The following code produces unexplicable behaviour. It can probably be pinned
down to wantarray(), but obviously other factors are playing a role as well
(notably keys()):

#! /usr/bin/perl

use strict;
use warnings;

my %tmp_hash = ( first => 'A', second => 'B' );

foreach my $aaa ( keys %tmp_hash ) {
    print "$aaa --> $tmp_hash{ $aaa }\n";

print "First loop:\n";
for my $bbb ( keys %tmp_hash ) {
    print "key1: $bbb\n";
    my ($jjj) = mysub();
    print "key2: $bbb\n";

print "Second loop:\n";
my @arr = keys %tmp_hash;
foreach my $bbb ( @arr ) {
    print "key3: $bbb\n";
    my ( $jjj ) = mysub();
    print "key4: $bbb\n";

sub mysub {
    my $text = 'select * from employee';
    my $val;
    if ( wantarray and $text =~ /^update/ ) {

first --> A
second --> B
First loop:
key1: first
key2: first
Use of uninitialized value in concatenation (.) or string at line 14.
key2: select * from employee
Second loop:
key3: first
key4: first
key3: second
key4: second

The warning of unitialized value is spurious here. Furthermore, the second
iteration through the First-Loop-foreach goes terribly wrong. Calling mysub()
in list context changes the content of the lexical $bbb. Interestingly enough,
this does not happen in the second loop that iterates over a copy of keys() and
not over the keys directly.

According to Slaven Rezic, this bug occurs beginning with 5.00503 whereas it
works fine with perl5.00404. I personally observed the faulty behaviour in
5.8.0rc2 and 5.6.1.

[Please do not change anything below this line]
Site configuration information for perl v5.8.0:

Configured by root at Mon Jun 24 08:29:49 CEST 2002.

Summary of my perl5 (revision 5.0 version 8 subversion 0) configuration:
    osname=linux, osvers=2.4.18, archname=i686-linux
    uname='linux ethan 2.4.18 #1 tue feb 26 09:52:36 cet 2002 i686 unknown '
    config_args='-e -Dprefix=/usr/opt'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
    cc='cc', ccflags ='-fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    cppflags='-fno-strict-aliasing -I/usr/local/include'
    ccversion='', gccversion='2.95.4 20011006 (Debian prerelease)', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=4, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lnsl -lgdbm -ldbm -ldb -ldl -lm -lc -lcrypt -lutil
    perllibs=-lnsl -ldl -lm -lc -lcrypt -lutil
    libc=/lib/, so=so, useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
    cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'

Locally applied patches:

@INC for perl v5.8.0:

Environment for perl v5.8.0:
    LANGUAGE (unset)
    LOGDIR (unset)
    PERL_BADLANG (unset)

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About