Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[perl #37101] my $v if (0); leaves $v around

0 views
Skip to first unread message

Brucer @ Gsg-Lnx-Bld1 . Cisco . Com

unread,
Sep 7, 2005, 8:19:55 PM9/7/05
to bugs-bi...@rt.perl.org
# New Ticket Created by bru...@gsg-lnx-bld1.cisco.com
# Please include the string: [perl #37101]
# in the subject line of all future correspondence about this issue.
# <URL: https://rt.perl.org/rt3/Ticket/Display.html?id=37101 >

This is a bug report for perl from bru...@gsg-lnx-bld1.cisco.com,
generated with the help of perlbug 1.35 running under perl v5.8.6.


-----------------------------------------------------------------
The following program invokes beta once instead of twice.
Extremely non-intuitive.
moving the "#' down one line fixes it.
1) seems like it shouldn't compile.
2) $c is static and keeps its value between invocations if
one instead of "if (0)" one has "if ($b)"
This is boiled down from a real program, of course.

thanks,
Bruce

--cut--
use strict 'vars';

sub beta ($) {
my $a = shift;
print "beta($a)\n";
"xyz";
}

sub alpha ($) {
my ( $a, $b ) = @_;
print "alpha($a)\n";
# my $c;
my
$c = 1 if (0);
$c = beta ($a) if ( ! defined $c );
}

alpha( 'A');
alpha( 'B');
--cut--
haven't analyzed this one thoroughly:
--cut 2--
use strict 'vars';

sub beta ($) {
my $a = shift;
print "beta($a)\n";
"xyz";
}

sub alpha ($$) {
my ( $a, $b ) = @_;
print "alpha($a)\n";
# my $c;
my
$c = 1 if ($b);
$c = beta ($a) if ( ! defined $c );
}

alpha( 'A',0);
alpha( 'B',0);
--cut 2--
-----------------------------------------------------------------
---
Flags:
category=core
severity=low
---
Site configuration information for perl v5.8.6:

Configured by brucer at Tue Jan 4 17:17:23 PST 2005.

Summary of my perl5 (revision 5 version 8 subversion 6) configuration:
Platform:
osname=linux, osvers=2.4.20-34.7.cisco.1smp, archname=i686-linux
uname='linux gsg-lnx-bld1.cisco.com 2.4.20-34.7.cisco.1smp #1 smp wed jun 23 15:03:42 edt 2004 i686 unknown '
config_args='-Dcc=gcc -Dprefix=/auto/gsg-sw/inst/i686-pc-linux-gnu -des'
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
Compiler:
cc='gcc', ccflags ='-fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm',
optimize='-O2',
cppflags='-fno-strict-aliasing -pipe -I/usr/local/include -I/usr/include/gdbm'
ccversion='', gccversion='2.96 20000731 (Red Hat Linux 7.3 2.96-112)', 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='gcc', ldflags =' -L/usr/local/lib'
libpth=/usr/local/lib /lib /usr/lib
libs=-lnsl -lndbm -lgdbm -ldl -lm -lcrypt -lutil -lc
perllibs=-lnsl -ldl -lm -lcrypt -lutil -lc
libc=/lib/libc-2.2.5.so, so=so, useshrplib=false, libperl=libperl.a
gnulibc_version='2.2.5'
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'

Locally applied patches:

---
@INC for perl v5.8.6:
/auto/gsg-sw/inst/i686-pc-linux-gnu/lib/perl5/5.8.6/i686-linux
/auto/gsg-sw/inst/i686-pc-linux-gnu/lib/perl5/5.8.6
/auto/gsg-sw/inst/i686-pc-linux-gnu/lib/perl5/site_perl/5.8.6/i686-linux
/auto/gsg-sw/inst/i686-pc-linux-gnu/lib/perl5/site_perl/5.8.6
/auto/gsg-sw/inst/i686-pc-linux-gnu/lib/perl5/site_perl
.

---
Environment for perl v5.8.6:
HOME=/users/brucer
LANG=en_US
LANGUAGE (unset)
LD_LIBRARY_PATH=
LOGDIR (unset)
PATH=/auto/gsg-sw/inst/i686-pc-linux-gnu/bin:/auto/gsg-sw/inst/share.brucer/sbin:/auto/gsg-sw/inst/share.brucer/bin:/usr/java/j2re1.4.1_01/bin:/users/brucer/share/bin:/router/bin:/usr/local/packages/atria/current/contrib:/usr/cisco/bin:/usr/atria/bin:/nfs/ddts/ddtshome/bin:/bin:/usr/sbin:/usr/bin:/auto/gsg-sw/share:/usr/openwin/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin
PERL_BADLANG (unset)
SHELL=/bin/bash

Dave Mitchell

unread,
Sep 8, 2005, 7:36:56 AM9/8/05
to perl5-...@perl.org
On Wed, Sep 07, 2005 at 05:19:55PM -0700, brucer @ gsg-lnx-bld1. cisco. com wrote:
> The following program invokes beta once instead of twice.
> Extremely non-intuitive.
> moving the "#' down one line fixes it.
> 1) seems like it shouldn't compile.
> 2) $c is static and keeps its value between invocations if
> one instead of "if (0)" one has "if ($b)"
> This is boiled down from a real program, of course.

This is a well-known problem, which is difficult to fix without breaking
backwards compatibility (some people make use of the 'feature' to create
static variables).

The problem boils down to:

If the 'my $x' is skipped at runtime, then at scope exit the variable isn't
freed, meaning that on next scope entry the variable maintains its
old value.

--
Standards (n). Battle insignia or tribal totems.

Unknown Sender

unread,
Sep 8, 2005, 7:44:14 AM9/8/05
to perl5-...@perl.org
>The following program invokes beta once instead of twice.
>Extremely non-intuitive.

Really? It is effectively:

$c = beta ($a) if ( ! defined $c ); # the "if" condition is true
$c = beta ($a) if ( ! defined $c ); # the "if" condition is false

>1) seems like it shouldn\'t compile.

Why not?

>2) $c is static and keeps its value between invocations

Yes, because it is not my-ed.
I've seen the exact explaination that "my" declarations with "if 0"
are no-ops, somewhere in Perl PODs.

Michael G Schwern

unread,
Sep 12, 2005, 5:16:20 PM9/12/05
to perl5-...@perl.org
On Wed, Sep 07, 2005 at 05:19:55PM -0700, brucer @ gsg-lnx-bld1. cisco. com wrote:
> The following program invokes beta once instead of twice.
> Extremely non-intuitive.
> moving the "#' down one line fixes it.
> 1) seems like it shouldn't compile.
> 2) $c is static and keeps its value between invocations if
> one instead of "if (0)" one has "if ($b)"
> This is boiled down from a real program, of course.

The short answer is "don't do that".

C<my $v if 0;> has already been deprecated in the development version of
Perl because its behavior is too confusing. The behavior cannot be
changed because too many people rely on it.

$ bleadperl -wle 'my $v if 0;'
Deprecated use of my() in false conditional at -e line 1.


--
Michael G Schwern sch...@pobox.com http://www.pobox.com/~schwern
'All anyone gets in a mirror is themselves,' she said. 'But what you
gets in a good gumbo is everything.'
-- "Witches Abroad" by Terry Prachett

0 new messages