develooper Front page | perl.perl5.porters | Postings from January 2014

[perl #120947] Array changes size when accessing past the end using for or grep

Thread Previous | Thread Next
From:
James E Keenan via RT
Date:
January 7, 2014 23:51
Subject:
[perl #120947] Array changes size when accessing past the end using for or grep
Message ID:
rt-4.0.18-13838-1389138712-461.120947-15-0@perl.org
On Tue Jan 07 15:38:06 2014, jkeenan wrote:
> On Tue Jan 07 10:40:31 2014, chris_jack@msn.com wrote:
> > This is a bug report for perl from chris_jack@msn.com,generated with
> > the help of perlbug 1.39 running under perl 5.14.4.
> >
> > -----------------------------------------------------------------[Please
> > describe your issue here]
> > # The below perl script bug.pl walks through the issue# I get the
> > output:Original length:    2Length is the same: 2Length is more:
> > 4Original length:    2Length is more:     4
> > when I would expect the output:Original length:    2Length is the
> > same: 2Length is more:     2Original length:    2Length is more:
> > 2
> > Basically both for and grep seem to extend an array simply by
> > lookingat the contents of elements past the end of the array however
> > thisdoes not happen if I use an element past the end of the array in
> > astraight scalar context.--start bug.pl--# We set up an array of
> > length 2my @array = (1, 2);
> > # Yes: it really is length 2print "Original length:    " .
> > scalar(@array) . "\n";
> > # When we access an element past the end as followsunless
> > (defined($array[3])) {}
> > # Yes: it is still length 2print "Length is the same: " .
> > scalar(@array) . "\n";
> > # When we access an element past the end using for as followsfor my
> > $a
> > ($array[3]) {}
> > # Suddenly the length has shot up to 4: is this a bug?print "Length
> > is
> > more:     " . scalar(@array) . "\n";
> > # Reset the arraymy @array = (1, 2);
> > # Yes: it is length 2 againprint "Original length:    " .
> > scalar(@array) . "\n";
> > # When we access an element past the end using grep as followsgrep
> > {defined} ($array[3]);
> > # Again the length has shot up to 4: is this a bug?print "Length is
> > more:     " . scalar(@array) . "\n";--end bug.pl--
> >
> 
> Chris, thanks for your bug report.
> 
> However, before submitting a report concerning a possible bug in Perl
> 5, we expect that one will take certain basic steps to reduce or even
> rule out the possibility of errors in the sample code being submitted
> as part of the bug report.
> 
> I extracted the code from your report and placed it in a file.  The
> first thing I did was to see whether that program would compile and
> run without warnings being emitted.  This was the output I got:
> 
> #####
> $ perl -cw 120947-grep.pl
> Unquoted string "pl" may clash with future reserved word at 120947-
> grep.pl line 1.
> Unquoted string "pl" may clash with future reserved word at 120947-
> grep.pl line 33.
> Can't modify constant item in postdecrement (--) at 120947-grep.pl
> line 1, near "pl--"
> syntax error at 120947-grep.pl line 3, near "my "
> 120947-grep.pl had compilation errors.
> #####
> 
> It would not be an efficient use of our time to correct this program
> so that it runs without all those warnings.
> 
> Could I ask that you clean up the program before resubmitting -- i.e.,
> get it to run without warnings.  What you think are bugs *may* be
> cleaned up by that alone.  (No guarantees, though.)
> 
> In addition, should you resubmit the bug report, please *attach* the
> file to your email or attach it to your post through the GUI interface
> at https://rt.perl.org.  Please don't post it inline; it was difficult
> to read.
> 
> Also, since I have a hunch there's no real bug in Perl here, I'd like
> to suggest that your first try getting help with this on a perl-help
> oriented mailing list or at a site such as perlmonks.org.
> 
> Thank you very much.
> Jim Keenan


I commented out --start bug.pl-- and one other line and reduced the warnings to:

#####
$ perl -cw 120947-grep.pl 
"my" variable @array masks earlier declaration in same scope at 120947-grep.pl line 23.
120947-grep.pl syntax OK
#####



---
via perlbug:  queue: perl5 status: open
https://rt.perl.org/Ticket/Display.html?id=120947

Thread Previous | Thread Next


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