Mailing List Archive

Support open source code!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Python discussion, anybody?



On Fri, Oct 27, 2000 at 04:49:18PM +0900, Stephen J. Turnbull wrote:
> Simon gave me an example the other day:
> 
> 		 perl -e 'print reverse "backwards"'
> produces
> 			      backwards
> 
> on output.  Why?  Reverse operates on arrays.  "backwards" is a
> string, and therefore scalar.  But Perl promotes that scalar to a
> one-element array, and reverses it.  And then, covering its tracks,
> converts the result back to a scalar and returns it.  Ouch.

Hm, that's not what happens at all. Built-ins like 'reverse' do different
things when you're asking for a list and when you're asking for a scalar.
If you ask for a scalar, reverse *will* reverse the scalar argument, hence
this will work:

 		 perl -e '$a = reverse "backwards"; print $a'

However, "print" takes a list, so reverse goes into list context, which means
it reverses the order of its arguments.

There's no "promoting to a one-element array": all functions take a list of
arguments. There's no "converting back into a scalar": "print" prints a list.

-- 
`First Up Against The Wall When The Revolution Comes!  Woohoo!
So long as they promise to use latex gloves, I'm happy. :)'
 - Thorfinn, in the Monastery.


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links