|
Perl code:
|
# ¿ Jan 4, 2020 14:27 |
|
|
# ¿ May 13, 2024 09:18 |
|
Actually to be honest that post is fairly mean-spirited and I apologise. This is a thread for people who like Perl and people liking Perl doesn't hurt me so I'm going to back off. To answer your question, Perl has a unique feature known as context-sensitivity. Every Perl expression is evaluated in one of two contexts, list context or scalar context. The context is provided by the code surrounding the expression, and the expression can (and frequently does) return different results depending on that context. In this case the expression in question is reverse "hello world". The print statement evaluates all of its arguments in list context. In list context, the reverse function treats all of its arguments as entries in a list and then returns a reversed list. In this case the list has a single scalar entry, the string "hello world", and so the returned value is the same single-entry list again, which print then prints out. The my $string = assignment evalutes the right-hand side in scalar context. In scalar context, reverse treats all of its arguments as a single scalar - casting them to strings and concatenating them all together if necessary - and then returns a single reversed string.
|
# ¿ Jan 4, 2020 18:42 |