Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Tanners
Dec 13, 2011

woof

qntm posted:

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.

Thats interesting. Is there an example of idiomatic Perl that uses this? Cause its still kindof hard to wrap my head around.

Adbot
ADBOT LOVES YOU

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply