Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Truth and Falsehood

by jcb (Parson)
on Aug 17, 2019 at 22:38 UTC ( [id://11104622]=note: print w/replies, xml ) Need Help??


in reply to Truth and Falsehood

While I know that you did not label this "[RFC]", this looks like a good draft for a brief tutorial.

As currently written, this neglects $#array. I think it would be better to write the first paragraph in "Hash and Array Size" as:

To get the number of elements in an array, use scalar, as in scalar @array, or use the array in a scalar context, for example my $size = @array;. To test whether an array is empty or not, simply use it in a boolean context, such as if (@array). To get the largest index in an array, use $#array. To explicitly resize an array, assign a value to $#array. Perl normally grows arrays as needed, but explicitly assigning to $#array can also shrink an array, discarding elements from the high end.

I have tried to follow your style in the above text, but I expect that you will further edit that before using it, if you use it.

The exists and defined tests also apply to array indexes, in the same way that they apply to hash keys. This can be explained briefly and would go well with adding mention of $#.

And one minor stylistic quibble: I think of exceptions as "thrown" with die because they can be "caught" with eval BLOCK, but can warnings be similarly "caught" in Perl or would it be better to say that length returns undef without producing a warning?

Replies are listed 'Best First'.
Re^2: Truth and Falsehood
by haukex (Archbishop) on Aug 18, 2019 at 06:46 UTC
    While I know that you did not label this "[RFC]", this looks like a good draft for a brief tutorial.

    The intent was a little different, as I mentioned at the very bottom of the node: There used to be a section "Truth and Falsehood" in perlsyn that was nice and short, and easy to link to whenever someone's question touched on Perl's concept of true/false, but that section was merged into a much longer block of text in perldata, which IMHO makes it harder to provide a link to people who just want to know about that one topic. (The title is such that it's easy to link to as [Truth and Falsehood].) So I want to keep the main point of the node focused on just true/false, hence I tried to separate out the "Related Topics" so that the top is just a "TL;DR".

    As currently written, this neglects $#array.

    Yes, that's a good point, I'll add a brief mention of it, thanks. IMHO, the more detailed description you wrote starts getting a little off-topic to the main point of the node, so I'll keep it a little shorter.

    The exists and defined tests also apply to array indexes

    Not entirely; from exists:

    WARNING: Calling exists on array values is strongly discouraged. The notion of deleting or checking the existence of Perl array elements is not conceptually coherent, and can lead to surprising behavior.
    would it be better to say that length returns undef without producing a warning?

    Yes, good point, thanks!

      the more detailed description you wrote starts getting a little off-topic

      Fair enough, and the link to the reference manual makes up for trimming it.

      [for exists on arrays]

      The tests apply, even though the results can be surprising, mostly due to the fact that Perl's arrays have a contiguous range of index values. I suspect that exists $array[$i] is equivalent to $i <= $#array. If not, then that is really surprising and probably better left "under the rug". :-)

      perl -M5.27.6 -e '$_=`perldoc -Tt perldata`;s/.*(A scalar.*?way).*/$1. +/s;say'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11104622]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-20 06:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found