develooper Front page | perl.perl5.porters | Postings from February 2001

[ID 20010226.007] weird array size problem...

Thread Next
February 26, 2001 12:12
[ID 20010226.007] weird array size problem...
Message ID:
Subject: werid array size problem...

This is a bug report for perl from,
generated with the help of perlbug 1.26 running under perl 5.005.

[Please enter your report here]

This is a sample program to show something that works just fine, the second sample program shows something that in my opinion should 
behave the same way, but somehow doesn't... I'm running perl 5.005 under Windows2000 pro.

#Here's the first program...
use strict "vars";

	#Suppose we declare an ordinary array that will eventually contain scalars and set it to NULL
	my @array = ();

	#Let's see what the array's size is...
	print scalar @array; #prints 0, of course

	#Suppose we want to test the array's first element, and the array is empty... $array[0] doesn't exist, so the interpreter won't
	#execute the if statement
	if($array[0] eq "something")
		print "something";

	#Now let's print the array's size again
	print scalar @array; #prints 0, just as we expect

#And here's the second program...
use strict "vars";
	#Now, let's say we declare an array that will eventually contain hash references and we set it to NULL
	my @array  = ();

	#Let's say the hash that will be referenced in each of the array's indexes looks like this (unimportant)
	my $struct = {FIELD1 => "hi", FIELD2 => "hello", FIELD3 => "greetings"};

	#What's the array size now?
	print scalar @array; #prints 0, nothing new here

	#Now, we want to perform a test on the array's first element, knowing that this array is supposed to contain hash references... 
	#$array[0] doesn't exist, so the interpreter won't execute the if statement
	if($array[0]->{FIELD2} eq "something")
		print "something";

	#BUT... now let's see the array's size...
	print scalar @array; #prints 1 (d'oh!)

WOW... the array just magically expanded... There's now a phoney record reference in the array for some reason (probably because
I tried to do an operation (->) on something that didn't exist in the if condition...) Sure, I could easily fix the problem by tweaking
the if statement a little (like in the following example :)

	if(scalar @array > 0 && $array[0]->{FIELD2} eq "something")
		print "something";
but I for one expected this to behave to behave the same way the first example with the regular scalar array did... Maybe this is 
indeed a problem... 

[Please do not change anything below this line]

Site configuration information for perl 5.005:

Summary of my perl5 (5.0 patchlevel 5 subversion 00) configuration:
    osname=MSWin32, osvers=4.0, archname=MSWin32-x86-object
    hint=recommended, useposix=true, d_sigaction=undef
    usethreads=undef useperlio=undef d_sfio=undef
    cc='cl.exe', optimize='-O2 -MD -DNDEBUG -TP -GX', gccversion=
    stdchar='char', d_stdstdio=define, usevfork=false
    intsize=4, longsize=4, ptrsize=4, doublesize=8
    d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=10
    alignbytes=8, usemymalloc=n, prototype=define
  Linker and Libraries:
    ld='link', ldflags ='-nologo -nodefaultlib -release -machine:x86'
    libpth="d:\program files\devstudio\vc\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 wsock32.lib mpr.lib winmm.lib  version.lib odbc32.lib odbccp32.lib PerlCRT.lib
    libc=d:\perl\5.005\bin\MSWin32-x86-object\PerlCRT.lib, so=dll, useshrplib=yes, libperl=perlcore.lib
  Dynamic Linking:
    dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' '
    cccdlflags=' ', lddlflags='-dll -nologo -nodefaultlib -release -machine:x86'

Locally applied patches:

@INC for perl 5.005:

Environment for perl 5.005:
    HOME (unset)
    LANG (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PERL5DB=BEGIN { require 'd:\perl\ACTIVE~1\' }
    PERL_BADLANG (unset)
    SHELL (unset)

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About