develooper Front page | perl.perl6.users | Postings from December 2021

Subscripting, semicolons, and adverbs

Thread Next
From:
Sean McAfee
Date:
December 10, 2021 21:49
Subject:
Subscripting, semicolons, and adverbs
Message ID:
CANan03ZmT4ZXYBzDmGqM7tkD_7qfz5uQraHk5kf0fbX2VZNQbA@mail.gmail.com
I was writing some code that involved accessing an array-of-arrays.  I
wanted to check whether both of a given pair of indices were in bounds, so
I wrote:

    return () if @array[$y]:!exists or @array[$y][$x]:!exists or ...

The double index check bugged me a bit, since I wondered if there might be
a shorter way to express it.  Then I remembered nested arrays can be
accessed by separating the indices with semicolons, so I tried:

    return () if @array[$y;$x]:!exists or ...

A whole bunch of errors ensued when I ran the program.  I experimented some
more on the console and saw some pretty strange behavior.

    > my @a = <a b c>, <d e f>
    [(a b c) (d e f)]
    > my $i = -1
    -1
    > @a[$i;$i]:exists
    False

(Indexing with $i instead of a literal -1 is necessary to get around the
fact that providing a negative literal as an array index is apparently a
compilation error, not a runtime error, meaning that @array[-1]:exists is a
syntax error, even though it looks like it should just evaluate to True.
That confused me for a little while.)

So anyway, that last evaluation appears correct, but if I repeat it over
and over, eventually I get this error:

    WARNING: unhandled Failure detected in DESTROY. If you meant to ignore
it, you can mark it as handled by calling .Bool, .so, .not, or .defined
methods. The Failure was:
    Index out of range. Is: -1, should be in 0..^Inf
      in block <unit> at <unknown file> line 1

Actually, I get a bunch of those errors, as if they were stored up by all
my previous evaluations of @a[$i;$i]:exists.  If I continue to evaluate the
expression repeatedly, I get a run of Falses, then another batch of errors
at some later point.

So this looks pretty buggy, but I wonder if I'm somehow invoking undefined
behavior by combining semicolons and adverbs while subscripting.  The
online docs cover both features separately, but not in tandem that I can
see.

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