Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Odud (1047)

Odud
  (email not shown publicly)

Journal of Odud (1047)

Wednesday June 04, 2003
10:00 AM

References hurt my brain

[ #12616 ]

Why do references always trip me up? I tend not to use them greatly and always struggle when I do. Context: I was trying to make a subroutine return a reference to a slice of an array. I wrote
 
  \@array[0..3]
 
which just returned a reference to a scalar before finding what I needed was
 
  [@array[0..3]]

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • same problem (Score:2, Informative)

    I struggled with the same thing today. Then I looked it up in the Cookbook, and it said:

    "An array slice is exactly the same as a list of individual array elements. Because you can't take a reference to a list, you can't take a reference to an array slice".

    • I knew the answer would be there - I just didn't have it with me at work today. It's one of those things where it looks so right (but just doesn't work the way you expect).
    • Now if you want some serious brain hurting, try this :

      perl -le '$x="abcdef";$y=\substr $x,2,2;$$y="gh";print $x'

      (warning ! this behaviour is not documented ! use at your own risk !)