Indeed, you are correct. I initially thought that this was a semantic detail as your demonstrations both use constructs, for and @_, that are already known to produce aliases; whether you are performing a slice operation or not. You could also use map or grep or List::Util::reduce and it's meta-ops etc.

But, I thought, that outside of those aliasing operators, that your argument was semantics because there was no way to get a handle on or utilise the aliasing of a slice operation. And that to use the aliasing, meant having to re-slice the array and re-create the aliases, each time you wanted to use them.

I was wrong! In attempting to prove my point, I came up with something I've never seen described or used before.

It's a little discused and used, and slightly aberrant and confusing, fact that in Perl, \( 1, 2, 3 ) doesn't give you a reference to the list, but rather a list of references to the items in the list.

Unlike \@a which gives a reference to the array.

Attempting to use that to discount your semantic argument, I tried the following:

@a = 1 .. 5; @b = \@a[ 0, 2, 4 ]; print "@b"; ## An array of references to aliases or copies? SCALAR(0x18eece0) SCALAR(0x18eecf8) SCALAR(0x18eed10) $$_ += 10 for @b; print $$_ for @b; 11 13 15 print "@a"; 11 2 13 4 15 ## It was mutated!

Which means that it isn't necessary to use the sub trick to get a reference to an array slice, provided you don't mind dereferencing the references.

That came as a big surprise to me. I think it may come as a surprise to a few other also?

So, you are correct. And it's not just a semantic detail :)

I could see it being classed as a bug by some, but since it has (probably) been around for a long time, it's unlikely to go away now.

However, there is one significant difference between the two methods:

@a = 1 .. 1e6;; print $$;; 2156 !tasklist /fi "pid eq 2156";; perl.exe 2156 0 63,320 +K sub refSlice{ \@_ };; $b = refSlice @a[ 1 .. $#a ];; !tasklist /fi "pid eq 2156";; perl.exe 2156 0 71,144 +K

Which shows that taking a reference to a slice of 999,999 aliases (using the sub method), requires an extra 7.824 MB. A significant saving over the 60 MB required to make a copy of the slice of the array.

Using the other method:

@a = 1 .. 1e6;; print $$;; 4108 !tasklist /fi "pid eq 4108";; perl.exe 4108 0 63,296 +K @b = \@a[ 1 .. $#a ];; !tasklist /fi "pid eq 4108";; perl.exe 4108 0 91,392 +K

Which shows this method requires 28.096 MB. Roughly 4 times as much. Understandable, because we created an array of references to a list (or array?) of aliases to the original data.

It seems that (at least in the latest versions of perl), that aliases are significantly lighter than copies of an array. I'll modify my post above to reflect this.

I'm sure the memory saving of aliases was far less significant the last time I looked at this back when Juerd originally posted about it?)

Thanks for forcing me to re-evaluate old knowledge :)


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^3: Array Slice Referencing by BrowserUk
in thread Array Slice Referencing by PerlPhi

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.