New "dot" feature

I wrote the following email to the Perl 5 Porters mailing list a few days ago. I thought you might enjoy it.

Hello Porters,

I had a dream that Perl 5 had moved from using -> to using . like most modern languages. And moved the existing . for concatenation to ~ like Perl 6. Then I wrote the code and was shocked how tiny it was.

I have this in the leonbrocard/dot branch, but is is really one commit:

http://perl5.git.perl.org/perl.git/commit/0c1fbeee

It's a very small tokenizer patch which interacts with a new "dot" feature. Then you can do things like:

  use feature qw(say dot);
  use CGI;
  my $q = CGI.new;
  say ref($q);
  say $q.blockquote(
    "Many years ago on the island of",
    $q.a({href=>"http://crete.org/"},"Crete"),
    "there lived a Minotaur named",
    $q.strong("Fred.") ,
  ),
  $q.hr;

  my $arrayref = [1,2,3,4,5];
  say $arrayref.[-1];
  my $hashref = {a => 1, b => 2};
  say $hashref.{b};

  say "Hello" ~ "world";

I think this is quite a cute example of how features can do neat little tricks. There may be complicated interactions which break things in ways I haven't thought about, but it seems like a tiny patch that doesn't seem to slow parsing down. I'm not entirely sure that I'm proposing this seriously, but...

What do you think?

10 Comments

Personally, I don't like it (I find "->" much more Perlish and easier to read), but I admire how easily and efficiently it was possible to implement it.

I don't think I'd vote for this to become part of Perl - I don't think we need yet another way to do it :)

really like it, is there already a solution for $hashref.{key} ? As $hashref->{key} still makes more sense.

I like the ->

I don't like ~ for concatenation.

That is all.

$aryref.[1] and $hashref.{foo} do look a bit wtf-ish to me ($obj.method is fine though).

wicked !

Belated reply but what the hell... :)

I agree with BigPresh and rephrase to "we probably don't need another way to do it" but the fact you were able to do it so effortlessly was truly a mark of a good programmer and a good language. :)

I think this looks very interesting Leon. I'm interested in trying it out on a larger class to see how it compares visually.

Thanks for posting it!

I'm not fond of the shift key, but since this trades a nearby shifted key for one I have to reach for ( > versus ~ ), I don't suppose I'd use it myself. And I'm eccentric enough to like that Perl has some odd syntax that makes programmers of other languages blanch. But it's pretty cool that you can do something like this fairly easily, and it speaks well of efforts to continue making Perl 5 more powerful and pleasant to use. Good stuff.

Does this tokenizer patch work with floats?

I don't think you can use ~ for concat. Some people still use bitwise negation. Also, mistaking concat-assignment and pattern-matching will make for many unpleasant bugs.

all in all, it doesn't really seem like a 'feature' - more of an Acme module, or a source filter - with about as much expectation on reliability.

Leave a comment

About acme

user-pic Léon Brocard (aka acme) is an orange-loving Perl eurohacker with many varied contributions to the Perl community, including the GraphViz module on the CPAN. YAPC::Europe was all his fault. He is still looking for a Perl Monger group he can start which begins with the letter 'D'.