I was playing about with the different methods that could be called on built-in types:

pugs> my @n = (12, 13, 14)
undef
pugs> @n
(12, 13, 14)
pugs> @n.elems
3

As I understand, these methods are also built-in subs (operators?), so that they can be called as such:

pugs> elems @n
3

What I found surprising was that this equivalence appears to work for other builtins as well, even those that one would not expect to be methods on an array:

pugs> sqrt @n
1.7320508075688772
pugs> @n.sqrt
1.7320508075688772

That last one I'm guessing is evaluating @n in scalar context and then calling "sqrt" on it. Yet literals are not objects the way they are in Ruby (right?):

pugs> sqrt 3
1.7320508075688772
pugs> 3.sqrt
Internal error while running expression:
*** Error:
unexpected "s"
expecting ".", fraction, exponent, term postfix, operator, postfix conditional, postfix loop, postfix iteration, ";" or end of input
at <interactive> at line 1, column 3


But what I find especially weird is that subs in declared in main *also* show this behavior:

pugs> sub mysub { print @_ ; say "Hi mom!" }
undef
pugs> mysub @n
121314Hi mom!
bool::true
pugs> @n.mysub
121314Hi mom!
bool::true

So, is this the way that Perl 6 is supposed to behave, or is this a quirk of the Pugs implementation, or what? Do subs in main automagically become methods on built-in types? It's not immediately obvious to me after glancing at S06 and S12, so please forgive me if this is a nuance of Perl 6 that I'm missing.

--
      Matt

      Matthew Zimmerman
      Interdisciplinary Biophysics, University of Virginia
      http://www.people.virginia.edu/~mdz4c/

Reply via email to