Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

my $v if (0); leaves $v around #8097

Closed
p5pRT opened this issue Sep 8, 2005 · 6 comments
Closed

my $v if (0); leaves $v around #8097

p5pRT opened this issue Sep 8, 2005 · 6 comments

Comments

@p5pRT
Copy link

p5pRT commented Sep 8, 2005

Migrated from rt.perl.org#37101 (status was 'rejected')

Searchable as RT37101$

@p5pRT
Copy link
Author

p5pRT commented Sep 8, 2005

From brucer@gsg-lnx-bld1.cisco.com

This is a bug report for perl from brucer@​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

@p5pRT
Copy link
Author

p5pRT commented Sep 8, 2005

From @iabyn

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.

@p5pRT
Copy link
Author

p5pRT commented Sep 8, 2005

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Sep 8, 2005

From pfusik@op.pl

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.

@p5pRT
Copy link
Author

p5pRT commented Sep 8, 2005

@rgs - Status changed from 'open' to 'rejected'

@p5pRT p5pRT closed this as completed Sep 8, 2005
@p5pRT
Copy link
Author

p5pRT commented Sep 13, 2005

From @schwern

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 schwern@​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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant