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()' incorrectly scoped in 'xxx if yyy' statement. #4642

Closed
p5pRT opened this issue Dec 4, 2001 · 2 comments
Closed

'my()' incorrectly scoped in 'xxx if yyy' statement. #4642

p5pRT opened this issue Dec 4, 2001 · 2 comments

Comments

@p5pRT
Copy link

p5pRT commented Dec 4, 2001

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

Searchable as RT7982$

@p5pRT
Copy link
Author

p5pRT commented Dec 4, 2001

From stevep@marketview.co.nz

Created by stevep@marketview.co.nz

The following code should, in my opinion, print five lines, with the
word 'quux' once on each of them.

  use strict;
  use warnings;

  sub foo {

  my $bar = 'baz' if $_[0];
  $bar .= 'quux';

  return $bar;
  }

  print foo(0), "\n";
  print foo(0), "\n";
  print foo(0), "\n";
  print foo(0), "\n";
  print foo(0), "\n";

Instead it prints​:

  quux
  quuxquux
  quuxquuxquux
  quuxquuxquuxquux
  quuxquuxquuxquuxquux

This behaviour violates DWIM, and/or 'use strict'.

It violates DWIM because I wouldn't expect 'my()' to be scoped in this
context - I expected it to be equivalent to the following snippet.

  my $bar;
  $bar = 'baz' if $_[0];

Even if it was expected that 'my()' would work like that, then it
breaks 'use strict', since $bar should no longer exist after that
line, and wouldn't be accessible in the $bar .= 'quux' line.

Perl Info

Flags:
    category=core
    severity=medium

Site configuration information for perl v5.6.1:

Configured by stevep at Wed Sep  5 14:14:16 NZST 2001.

Summary of my perl5 (revision 5.0 version 6 subversion 1) configuration:
  Platform:
    osname=linux, osvers=2.2.17-14, archname=i686-linux
    uname='linux spinneret 2.2.17-14 #1 mon feb 5 17:53:36 est 2001 i686
unknown '
    config_args=''
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef use5005threads=undef useithreads=undef
usemultiplicity=undef
    useperlio=undef d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
  Compiler:
    cc='cc', ccflags ='-fno-strict-aliasing -D_LARGEFILE_SOURCE
-D_FILE_OFFSET_BITS=64',
    optimize='-O2',
    cppflags='-fno-strict-aliasing'
    ccversion='', gccversion='egcs-2.91.66 19990314/Linux (egcs-1.1.2
release)', 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, usemymalloc=n, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lnsl -lndbm -lgdbm -ldb -ldl -lm -lc -lposix -lcrypt -lutil
    perllibs=-lnsl -ldl -lm -lc -lposix -lcrypt -lutil
    libc=/lib/libc-2.1.3.so, 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.6.1:
    /usr/local/lib/perl5/5.6.1/i686-linux
    /usr/local/lib/perl5/5.6.1
    /usr/local/lib/perl5/site_perl/5.6.1/i686-linux
    /usr/local/lib/perl5/site_perl/5.6.1
    /usr/local/lib/perl5/site_perl
    .


Environment for perl v5.6.1:
    HOME=/home/stevep
    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/stevep/bin
    PERL_BADLANG (unset)
    SHELL=/bin/bash2


@p5pRT
Copy link
Author

p5pRT commented Dec 4, 2001

From @tamias

On Wed, Dec 05, 2001 at 06​:34​:07PM +1300, Steve Piner wrote​:

Thank you for your bug report.

The following code should, in my opinion, print five lines, with the
word 'quux' once on each of them.

use strict;
use warnings;

sub foo \{

    my $bar = 'baz' if $\_\[0\];

my has both a compile time effect and a run time effect. The compile time
effect declares the variable and allocates storage. The run time effect
clears out the storage when the scope is entered. By putting a conditional
on the my, you are skipping the run time effect, so the previous value
stays around.

This behavior was unintentional, but there is now code that depends on it,
so it probably won't be changed anytime soon.

In conclusion, don't do that. :)

Ronald

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