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

Silly function syndrome #7109

Closed
p5pRT opened this issue Feb 18, 2004 · 8 comments
Closed

Silly function syndrome #7109

p5pRT opened this issue Feb 18, 2004 · 8 comments

Comments

@p5pRT
Copy link

p5pRT commented Feb 18, 2004

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

Searchable as RT26839$

@p5pRT
Copy link
Author

p5pRT commented Feb 18, 2004

From dom@idealx.com

Created by dom@idealx.com

The empty sub (just "sub {}") behaves funny​:

perl -we 'sub zoo { wantarray ? @​_ : undef } print scalar zoo(1,2); print zoo(1,2);'
Use of uninitialized value in print at -e line 1.
12

In other words, it returns undef in scalar context, but @​_ in list
context! This is a "don't do that, then" issue once one finds about
it, but... still.

Perl Info

Flags:
    category=core
    severity=low

Site configuration information for perl v5.6.1:

Configured by root at Sun Aug 10 01:12:15 UTC 2003.

Summary of my perl5 (revision 5.0 version 6 subversion 1) configuration:
  Platform:
    osname=linux, osvers=2.4.20-7um, archname=i386-linux
    uname='linux (none) 2.4.20-7um #1 smp fri aug 8 18:30:28 edt 2003 i686 unknown '
    config_args='-Dccflags=-DDEBIAN -Dcccdlflags=-fPIC -Darchname=i386-linux -Dprefix=/usr -Dprivlib=/usr/share/perl/5.6.1 -Darchlib=/usr/lib/perl/5.6.1 -Dvendorprefix=/usr -Dvendorlib=/usr/share/perl5 -Dvendorarch=/usr/lib/perl5 -Dsiteprefix=/usr/local -Dsitelib=/usr/local/share/perl/5.6.1 -Dsitearch=/usr/local/lib/perl/5.6.1 -Dman1dir=/usr/share/man/man1 -Dman3dir=/usr/share/man/man3 -Dman1ext=1 -Dman3ext=3perl -Dpager=/usr/bin/sensible-pager -Uafs -Ud_csh -Uusesfio -Duseshrplib -Dlibperl=libperl.so.5.6.1 -Dd_dosuid -des'
    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 ='-DDEBIAN -fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-O2',
    cppflags='-DDEBIAN -fno-strict-aliasing -I/usr/local/include'
    ccversion='', gccversion='2.95.4 20011002 (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, usemymalloc=n, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lgdbm -ldb -ldl -lm -lc -lcrypt
    perllibs=-ldl -lm -lc -lcrypt
    libc=/lib/libc-2.2.5.so, so=so, useshrplib=true, libperl=libperl.so.5.6.1
  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/perl/5.6.1
    /usr/local/share/perl/5.6.1
    /usr/lib/perl5
    /usr/share/perl5
    /usr/lib/perl/5.6.1
    /usr/share/perl/5.6.1
    /usr/local/lib/site_perl
    .


Environment for perl v5.6.1:
    HOME=/home/dom
    LANG=français
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:/usr/local/sbin:/usr/sbin:/sbin:/home/dom/bin:/usr/local/lib/cdk/bin:/usr/X11R6/bin
    PERL_BADLANG (unset)
    SHELL=/bin/bash


@p5pRT
Copy link
Author

p5pRT commented Feb 18, 2004

From kstar@cpan.org

On Feb 18, Dominique Quatravaux wrote​:

The empty sub (just "sub {}") behaves funny​:

perl -we 'sub zoo { wantarray ? @​_ : undef } print scalar zoo(1,2); print zoo(1,2);'
Use of uninitialized value in print at -e line 1.
12

In other words, it returns undef in scalar context, but @​_ in list
context! This is a "don't do that, then" issue once one finds about
it, but... still.

  What exactly would you want or expect? This is appropriate
behavior. The subroutine is a little funny (i.e., of dubious
utility), but its behavior is perfectly normal, given what it's
designed to do.

  - Kurt

@p5pRT
Copy link
Author

p5pRT commented Feb 18, 2004

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

@p5pRT
Copy link
Author

p5pRT commented Feb 19, 2004

From @pjcj

On Wed, Feb 18, 2004 at 06​:52​:00PM -0500, Kurt Starsinic wrote​:

On Feb 18, Dominique Quatravaux wrote​:

The empty sub (just "sub {}") behaves funny​:

perl -we 'sub zoo { wantarray ? @​_ : undef } print scalar zoo(1,2); print zoo(1,2);'
Use of uninitialized value in print at -e line 1.
12

In other words, it returns undef in scalar context, but @​_ in list
context! This is a "don't do that, then" issue once one finds about
it, but... still.

What exactly would you want or expect?  This is appropriate

behavior. The subroutine is a little funny (i.e., of dubious
utility), but its behavior is perfectly normal, given what it's
designed to do.

I suspect that the test case should have been​:

  perl -we 'sub zoo {} print scalar zoo(1,2); print zoo(1,2);'

This has been fixed in 5.8.0, broken again in 5.8.1, and fixed again in
5.8.2.

--
Paul Johnson - paul@​pjcj.net
http​://www.pjcj.net

@p5pRT
Copy link
Author

p5pRT commented Feb 19, 2004

From @mjdominus

Created by @mjdominus

What does this print?

  sub foo {
  my $x = 18;
  bar($x);
  }

  sub bar { }

  print foo();

Says "perlsub"​: "The return value of a subroutine is the value of the
last expression evaluated." It seems strange to me that this isn't
qualified with "last expression evaluated by the sub", so that
sub bar {} always returns undefined, but that if that is OK with
you, it is OK with me. Here, the last expression evaluated is the
computation of the argument $x, back in foo(), so bar() returns 18.

Now consider this​:

  sub foo {
  my $x = 18;
  $x;
  bar();
  }

  sub bar { }

  print foo();

The last expression evaluated is still $x. But bar() no longer
returns 18; instead, it returns undefined.

Perl Info

Flags:
    category=core
    severity=medium

Site configuration information for perl v5.6.1:

Configured by mjd at Mon Apr  9 13:10:50 EDT 2001.

Summary of my perl5 (revision 5.0 version 6 subversion 1) configuration:
  Platform:
    osname=linux, osvers=2.2.16, archname=i586-linux
    uname='linux plover 2.2.16 #5 wed sep 27 19:05:46 edt 2000 i586 unknown '
    config_args='-des'
    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 -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-O2',
    cppflags='-fno-strict-aliasing -I/usr/local/include'
    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/i586-linux
    /usr/local/lib/perl5/5.6.1
    /usr/local/lib/perl5/site_perl/5.6.1/i586-linux
    /usr/local/lib/perl5/site_perl/5.6.1
    /usr/local/lib/perl5/site_perl/5.6.0/i586-linux
    /usr/local/lib/perl5/site_perl/5.6.0
    /usr/local/lib/perl5/site_perl
    .


Environment for perl v5.6.1:
    HOME=/home/mjd
    LANG (unset)
    LANGUAGE (unset)
    LC_ALL=POSIX
    LD_LIBRARY_PATH=/lib:/usr/lib:/usr/X11R6/lib
    LOGDIR (unset)
    PATH=/home/mjd/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11/bin:/usr/games:/sbin:/usr/sbin:/usr/local/bin/X11:/usr/local/bin/mh:/data/mysql/bin:/usr/local/bin/pbm:/usr/local/bin/ezmlm:/home/mjd/TPI/bin:/usr/local/teTeX/bin:/usr/local/mysql/bin
    PERL_BADLANG (unset)
    SHELL=/bin/bash


@p5pRT
Copy link
Author

p5pRT commented Feb 19, 2004

From @rspier

I sent a bug report back in 2001 about something like this​:

To​: perl5-porters@​perl.org
Subject​: [ID 20010812.007] trivial function has wrong return value

That maps to #7518 in the new system.

@p5pRT
Copy link
Author

p5pRT commented Mar 26, 2004

From @iabyn

On Wed, Feb 18, 2004 at 08​:56​:38PM -0500, Mark Jason Dominus wrote​:

Paul Johnson <paul@​pjcj.net>​:

I suspect that the test case should have been​:

perl -we 'sub zoo {} print scalar zoo(1,2); print zoo(1,2);'

This has been fixed in 5.8.0, broken again in 5.8.1, and fixed again in
5.8.2.

Says "perlsub"​: "The return value of a subroutine is the value of the
last expression evaluated." It seems strange to me that this isn't
qualified with "last expression evaluated by the sub"

It does now...

--
You never really learn to swear until you learn to drive.

Change 22595 by davem@​davem-percy on 2004/03/26 17​:28​:48

  [perl #26839] document the return value of an empty sub

Affected files ...

... //depot/perl/pod/perlsub.pod#57 edit

Differences ...

==== //depot/perl/pod/perlsub.pod#57 (text) ====

@​@​ -67,7 +67,8 @​@​
not update any arguments.

The return value of a subroutine is the value of the last expression
-evaluated. More explicitly, a C<return> statement may be used to exit the
+evaluated by that sub, or the empty list in the case of an empty sub.
+More explicitly, a C<return> statement may be used to exit the
subroutine, optionally specifying the returned value, which will be
evaluated in the appropriate context (list, scalar, or void) depending
on the context of the subroutine call. If you specify no return value,

@p5pRT
Copy link
Author

p5pRT commented Mar 26, 2004

@iabyn - Status changed from 'open' to 'resolved'

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

1 participant