[ I don't see any relation between your post and mine or any other post in this thread, so it's rather odd that you posted it here. Seekers of Perl Wisdom would have been a better place. Meditations would also have worked if you gave your opinion on the answers to these questions. ]

If we want the fifth element of an array why don't we say $array[ 5 ]? Isn't that DWIM?

For some, yes. For some, no. It depends on whether you think of array indexes as offsets or as positions. Beginners think in terms of positions, but there are many reasons to use offsets.

Why does @array in scalar context evaluate to the number of elements?

Because it's very convenient for it to do so.

Why not just use $#array + 1 and have an array in scalar context return the first element

It's much more common to want to know the number of elements in an array than to get its first element. Besides, it's already super easy to get the first element of the array ($array[0]).

or the sum of all the elements, or the total length of all the elements?

Aside that it would be rather arbitrary to choose between the two, these are very rarely needed operations that can easily be achieved by other means, such as

sum(@array)
and
length(join '', @array) -or- sum(map length, @array)

In reply to Re^4: The maybe it is better written this way tool by ikegami
in thread The maybe it is better written this way tool by szabgab

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.