develooper Front page | perl.perl5.porters | Postings from September 2005

[perl #37289] Erratic return of hashref from sub

From:
ron @ savage . net . au
Date:
September 27, 2005 04:06
Subject:
[perl #37289] Erratic return of hashref from sub
Message ID:
rt-3.0.11-37289-121614.1.34672161974414@perl.org
# New Ticket Created by  ron@savage.net.au 
# Please include the string:  [perl #37289]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org/rt3/Ticket/Display.html?id=37289 >


To: perlbug@perl.org
Subject: Erratic return of hashref from sub
Reply-To: ron@savage.net.au
Message-Id: <5.8.6_3088_1127809933@c220-239-109-96.eburwd3.vic.optusnet.com.au>

This is a bug report for perl from ron@savage.net.au,
generated with the help of perlbug 1.35 running under perl v5.8.6.

#!/usr/bin/perl
#
# Name:
#	map-inside-hashref.pl.
#
# Author:
#	Ron Savage. 27-Sep-2005.
#	ron@savage.net.au.

# Purpose:
#	Demonstrate that Perl is erratic when returning hashrefs from subs.

use strict;
use warnings;

# -----------------------------------------------
# Return a hashref.

sub test_1
{
	print "test_1. Success expected\n";

	{
		1 => 11,
		2 => 22,
		3 => 33,
	};

}	# End of test_1.

# -----------------------------------------------
# Return a hashref.

sub test_2
{
	print "test_2. Success expected\n";

	my(%h) = (1 => 11, 2 => 22);

	{
		3 => 33,
		map{($_ => $h{$_})} keys %h,
	};

}	# End of test_2.

# -----------------------------------------------
# Can't put 3 => 33 after map.
# The 3 => 33 is gobbled up by the comma after %h.

sub test_3
{
	print "test_3. Failure expected\n";

	my(%h) = (1 => 11, 2 => 22);

	{
		map{($_ => $h{$_})} keys %h,
		3 => 33,
	};

}	# End of test_3.

# -----------------------------------------------
# Can't put 3 => 33 after (map). Perl bug?

sub test_4
{
	print "test_4. Success expected. Perl bug? \n";

	my(%h) = (1 => 11, 2 => 22);

	{
		(map{($_ => $h{$_})} keys %h),
		3 => 33,
	};

}	# End of test_4.

# -----------------------------------------------
# Return a hashref.

sub test_41
{
	print "test_41. Success expected. \n";

	my(%h) = (1 => 11, 2 => 22);
	my($r) =
	{
		(map{($_ => $h{$_})} keys %h),
		3 => 33,
	};

	$r;

}	# End of test_41.

# -----------------------------------------------
# Can't omit 3 => 33. Perl bug?
# Requires something, eg, 3 => 33, before map, as per test_2.

sub test_5
{
	print "test_5. Success expected. Perl bug? \n";

	my(%h) = (1 => 11, 2 => 22);

	{
		map{($_ => $h{$_})} keys %h,
	};

}	# End of test_5.

# -----------------------------------------------
# Return a hashref.

sub test_51
{
	print "test_51. Success expected. \n";

	my(%h) = (1 => 11, 2 => 22);
	my($r) =
	{
		map{($_ => $h{$_})} keys %h,
	};

	$r;

}	# End of test_51.

# -----------------------------------------------
# Return a hashref.

sub test_6
{
	print "test_6. Success expected. \n";

	my(%h) = (1 => 11, 2 => 22);

	my($h) =
	{
		map{($_ => $h{$_})} keys %h,
	};

	$h;

}	# End of test_6.

# -----------------------------------------------

sub pwint
{
	my($h) = @_;

	print join("\n", map{"$_ => $$h{$_}"} sort keys %$h), ". \n", '-' x 10, "\n";

}	# End of pwint.

# -----------------------------------------------

pwint(test_1() );

pwint(test_2() );

eval{pwint(test_3() );};

eval{pwint(test_4() );};

eval{pwint(test_41() );};

eval{pwint(test_5() );};

eval{pwint(test_51() );};

pwint(test_6() );
---
Flags:
    category=core
    severity=medium
---
Site configuration information for perl v5.8.6:

Configured by Ron at Wed Dec 22 15:20:14 2004.

Summary of my perl5 (revision 5 version 8 subversion 6) configuration:
  Platform:
    osname=MSWin32, osvers=4.0, archname=MSWin32-x86-multi-thread
    uname=''
    config_args='undef'
    hint=recommended, useposix=true, d_sigaction=undef
    usethreads=define use5005threads=undef useithreads=define
usemultiplicity=define
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='cl', ccflags ='-nologo -Gf -W3 -MD -DNDEBUG -O1 -DWIN32 -D_CONSOLE
-DNO_STRICT -DHAVE_DES_FCRYPT  -DNO_HASH_SEED -DPERL_IMPLICIT_CONTEXT
-DPERL_IMPLICIT_SYS -DUSE_PERLIO -DPERL_MSVCRT_READFIX',
    optimize='-MD -DNDEBUG -O1',
    cppflags='-DWIN32'
    ccversion='', gccversion='', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=10
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='__int64',
lseeksize=8
    alignbytes=8, prototype=define
  Linker and Libraries:
    ld='link', ldflags ='-nologo -nodefaultlib -release
-libpath:"C:\Perl\lib\CORE"  -machine:x86'
    libpth=\lib
    libs=  oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib
comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib  netapi32.lib
uuid.lib ws2_32.lib mpr.lib winmm.lib  version.lib odbc32.lib odbccp32.lib
msvcrt.lib
    perllibs=  oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib 
comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib  netapi32.lib
uuid.lib ws2_32.lib mpr.lib winmm.lib  version.lib odbc32.lib odbccp32.lib
msvcrt.lib
    libc=msvcrt.lib, so=dll, useshrplib=yes, libperl=perl58.lib
    gnulibc_version='undef'
  Dynamic Linking:
    dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' '
    cccdlflags=' ', lddlflags='-dll -nologo -nodefaultlib -release
-libpath:"C:\Perl\lib\CORE"  -machine:x86'

Locally applied patches:
    ACTIVEPERL_LOCAL_PATCHES_ENTRY
    21540 Fix backward-compatibility issues in if.pm
    23565 Wrong MANIFEST.SKIP

---
@INC for perl v5.8.6:
    C:/Perl/lib
    C:/Perl/site/lib
    .

---
Environment for perl v5.8.6:
    HOME=C:\
    LANG (unset)
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=C:\Bin;C:\Bin1;C:\Perl\bin;C:\MySQL\bin;c:\Program
Files\Postgresql\8.0-beta1\bin;C:\PROGRA~1\ATT\Graphviz\bin;C:\PROGRA~1\ATT\Grap
hviz\bin\tools;C:\Apache2\bin;c:\program files\imagemagick-6.2.0-q16;C:\Program
Files\SSH Communications Security\SSH Secure Shell;C:\Program
Files\UltraEdit;C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;C:\Program
Files\Microsoft Visual Studio\Common\Tools\WinNT;C:\Program Files\Microsoft
Visual Studio\Common\MSDev98\Bin;C:\Program Files\Microsoft Visual
Studio\Common\Tools;C:\Program Files\Microsoft Visual Studio\VC98\bin;C:\Program
Files\Microsoft SQL Server\80\Tools\BINN;C:\Program Files\MySQL\MySQL Server
4.1\bin;c:\Program Files\GNU\GNUPG;c:\gs\gs8.50\bin;C:\Program Files\IDM
Computer Solutions\UltraEdit-32
    PERL_BADLANG (unset)
    SHELL (unset)

--
Cheers
Ron Savage, ron@savage.net.au on 27/09/2005
http://savage.net.au/index.html
Let the record show: Microsoft is not an Australian company





nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About