References hurt my brain

Odud on 2003-06-04T15:00:45

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]]


same problem

spur on 2003-06-04T16:41:13

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".

Re:Cookbook

Odud on 2003-06-04T21:09:42

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).

Re:same problem

rafael on 2003-06-04T21:12:57

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 !)