This is a small collection of ideas for the Perl6 language.  Think of this
posting as a light and refreshing summer fruit salad, composed of three
ideas to while away the time during this August lull in perl6-language.


--------------------------------------------------------
Give split an option to keep the delimiters in the returned array

I often find that I want to split an expression, but I don't want to get rid
of the delimiters.  For example, I've been parsing a lot of SQL lately, and
I find myself needing to split expressions like this:

   rank=?

It would be really groovy if that expression could be split with the
delimiters in place, something like this:

   @tokens = split _/[?=*-+]/, $sql, keep=>'all';

and get back an array with these values: ('rank', '=', '?')

But that raises a problem: what if the expression is this (note the spaces):

   rank = ?

In that case I would want the = and ? but I wouldn't want the spaces.  A
slightly different option could keep just stuff in parens:

   @tokens = split _/\s*([?=*-+])\s*/, $sql, keep=>'parens';


--------------------------------------------------------
Set preferred boolean string for scope

In Perl5, if you use a boolean expression (e.g. $x==$y) you get back 1 for
true and an empty string for false.  That makes sense, of course, but I've
always preferred 1 for true and 0 for false.  I generally use exactly only
those two values for true and false in my databases, and I find I'm forever
writing things like ($x==$y ? 1 : 0) to tidy up my booleans.

It would be cool if in Perl6 you could indicate the preferred default values
of true and false for a given namespace or scope, something like this:

   use BooleanValues TRUE=>1, FALSE=>0;

Perl itself could respect these requests in expressions like $x==$y.
Functions that declare themselves as booleans can return anything they like,
but the results would be translated into the caller's preferred true/false
values.  If no such values are indicated for a namespace then whatever the
functions returns is returned.

--------------------------------------------------------
Push with []

Our friends over in PHP have a nifty little way of saying "push this onto
the end of the array".  You simply assign the value to the array using an
empty index.  In Perl6 it could look like this:

  @arr[] = $var;

The expression above would be exactly equivalent to

  push @arr, $var;

I've always found the first form more intuitive: it feels like I'm assigning
something. It's a paradigm issue... I'm not suggesting that we get rid of
push, just that we create this additional form that allows the programmer to
think of it in a different way.

Reply via email to