This is just your friendly neighborhood curmudgeon reminding you that in
Perl 6, everything is an object.  This is a concept that, as Perl
programmers, we're not familiar with.

What are the consequences of this in language design?  Well, it means not
every new piece of functionality requires a new operator.  Instead, consider
a method.  Its an actual word.

Stemming the tide of new grammar and operators heads off another
problem, typos becoming meaningful.  The more operators you have, the more
chance that this sort of thing:

   if $num1 === $num2 {
      ...

will be hard to find.  Was it a typo or did they really mean to do a shallow
object comparison?


Instead of introducing new built-in functions they can be done as methods.
This reduces core namespace pollution.  Think of the core like a big CPAN
module.  Every built-in is like an exported function.  Every word used as a
built-in is one that noone else can safely use for something else in their
own code.


If you want a tactile grasp of the "Everything is an object" concept, try
some Ruby.

Many pieces of edge functionality which are useful but would have been
rejected because they don't really warrent a new built-in or operator can be
put in as methods.  For example, in Ruby you can alter the default value of
a hash like so:

    h = {"foo" => 42};
    h.default = "wibble";
    
    # Prints 'wibble' rather than undef.
    print h["bar"];

Isn't that neat?  Not important enough to warrent a new built-in, but
something nice to have in your toolbox.

How many times have you done this in perl:

    print grep defined, @array;

in Ruby, its just this:

    print array.compact

I find myself doing this alot:

   $string =~ s/^\s+//;
   $string =~ s/\s+$//;

in Ruby:

   string.strip!

Ruby can add neat little features without long deliberations about the
impact on language growth and backwards compatibilty because they have the
ability to add methods to core types.

A flip through the Ruby builtin methods is nice for building up some envy. :)
http://www.rubycentral.com/book/builtins.html


Sometimes you do need to use an operator or built-in.  Usually to make data
transformations flow nicely:

  @foo = join "\n", map { ... } grep { ... } sort @bar;

the concept can be reversed:

  @foo = @bar.sort.grep(...).map(...).join("\n");

but is that Perlish?

Or you will commonly use the operator on expressions rather than variables.

   1 + 2 == 3

would suck as:

   (1 + 2).eq 3


There's lots of reasons to use an operator or built-in instead of a method,
but remember to make the consideration first.  Don't go grammar happy.


In closing:  Consider a method rather than a built-in or operator.
Everything is an object.  Tattoo it on your forehead today.


-- 

Michael G. Schwern   <[EMAIL PROTECTED]>    http://www.pobox.com/~schwern/
Perl Quality Assurance      <[EMAIL PROTECTED]>         Kwalitee Is Job One
Do you actually think about what you are saying or is it an improvisational 
game of Mad Libs that you play in your head?

Reply via email to