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

bug in 'my $x = 0 if $y->{Z}' #1416

Closed
p5pRT opened this issue Mar 21, 2000 · 3 comments
Closed

bug in 'my $x = 0 if $y->{Z}' #1416

p5pRT opened this issue Mar 21, 2000 · 3 comments

Comments

@p5pRT
Copy link

p5pRT commented Mar 21, 2000

Migrated from rt.perl.org#2556 (status was 'resolved')

Searchable as RT2556$

@p5pRT
Copy link
Author

p5pRT commented Mar 21, 2000

From rmiller@rmiller1.idx.com

Created by Rich_Miller@idx.com

The statement
  my $x = 0 if $y->{Z};
usually BUT NOT ALWAYS sets $x to undef or 0. When it fails, $x appears to
be set to a value to which it was set in a previous call to the subroutine
in which this statement occurs. Is this correct behavior?

Splitting the statement into two parts
  my $x;
  $x = 0 if $y->{Z};
results in code which always works correctly.

This occurs deep within the evaluation of a large and reasonably complex
Perl data structure, using only native Perl code, which is warning-free
with 'use strict' and the -w option supplied to Perl. I have been completely
unable to show this in anything remotely resembling a simple test case.

Perl Info


Site configuration information for perl 5.00503:

Configured by root at Mon Aug 30 23:08:56 EDT 1999.

Summary of my perl5 (5.0 patchlevel 5 subversion 3) configuration:
  Platform:
    osname=linux, osvers=2.2.5-22smp, archname=i386-linux
    uname='linux porky.devel.redhat.com 2.2.5-22smp #1 smp wed jun 2 09:11:51 edt 1999 i686 unknown '
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef useperlio=undef d_sfio=undef
  Compiler:
    cc='cc', optimize='-O2', gccversion=egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
    cppflags='-Dbool=char -DHAS_BOOL -I/usr/local/include'
    ccflags ='-Dbool=char -DHAS_BOOL -I/usr/local/include'
    stdchar='char', d_stdstdio=undef, usevfork=false
    intsize=4, longsize=4, ptrsize=4, doublesize=8
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    alignbytes=4, usemymalloc=n, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lnsl -ldl -lm -lc -lposix -lcrypt
    libc=, 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 5.00503:
    /usr/local/bin/perl
    /home/rmiller/rel/pmake
    /usr/lib/perl5/5.00503/i386-linux
    /usr/lib/perl5/5.00503
    /usr/lib/perl5/site_perl/5.005/i386-linux
    /usr/lib/perl5/site_perl/5.005
    .


Environment for perl 5.00503:
    HOME=/home/rmiller
    LANG=en_US
    LANGUAGE (unset)
    LC_ALL=en_US
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/rmiller/bin:.
    PERL5LIB=/usr/local/bin/perl:/home/rmiller/rel/pmake
    PERL_BADLANG (unset)
    SHELL=/bin/bash


@p5pRT
Copy link
Author

p5pRT commented Mar 21, 2000

From [Unknown Contact. See original ticket]

The statement
my $x = 0 if $y->{Z};
usually BUT NOT ALWAYS sets $x to undef or 0. When it fails, $x appears to
be set to a value to which it was set in a previous call to the subroutine
in which this statement occurs. Is this correct behavior?

This is actually a quasi-known "feature". Contrast​:

  use strict;
  sub counter {
  my $value if 0;
  return ++$value;
  }
  for (1..5) {
  printf "that's %d\n", counter();
  }

With​:

  use strict;
  {
  my $value;
  sub counter {
  return ++$value;
  }
  }
  for (1..5) {
  printf "that's %d\n", counter();
  }

But the former admits no initialization, because you can't say

  my $value = 0 unless defined $value;

As it's not yet in scope. Also, the second form allows this kind of thing​:

  BEGIN {
  my $value = 0;
  sub incr { return ++$value }
  sub decr { return --$value }
  }

I assume one could/should patch perlsyn/perlsub/perlfunc if one
wishes to condone this activity. Whether one wishes such, however,
remains unclear. :-)

--tom

@p5pRT
Copy link
Author

p5pRT commented Mar 21, 2000

From [Unknown Contact. See original ticket]

Thanks much for the explanation. I used the construct
  my $x = 0 if $y->{Z}
in a only one line of code, so it's easy to fix. I have no reason to "condone
the behavior" -- but it would have been nice to get a warning (either on
subsequent use of $x, if the "my $x" wasn't executed by or that a conditional
assignment isn't allowed in this context...). But no big deal.

Perl's a wonderful tool!
-- Rich Miller

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

No branches or pull requests

2 participants