Change this line:
my $ref = \{@list[2..4]};
to
my $ref = [@list[2..4]]
and you should have what you want.

(update: well not a reference but a copy, as my fellow monks have kindly pointed out, and sauoq (among others) explains nicely below).

What $ref is holding is a reference to a reference of an anonymous hash. (what happens with the curly braces, rather than with the square ones.) As Data::Dumper clearly shows:

#!/usr/bin/perl use Data::Dumper; use strict; use warnings; my @list = (0, 1, 2, 3, 4, 5, 6, 7); print "@list\n"; my @short = @list[1..4]; print "@short\n"; my $ref = \{@list[2..4]}; print Dumper $ref; __DATA__ 0 1 2 3 4 5 6 7 1 2 3 4 Odd number of elements in anonymous hash at C:\test_bed\testme.pl line + 10. $VAR1 = \{ '4' => undef, '2' => 3 };

-enlil


In reply to Re: reference to an array slice? by Enlil
in thread reference to an array slice? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.