Hacker News new | past | comments | ask | show | jobs | submit login

That certainly seems way better than the old way of having to include the inscrutable line noise to construct, fill and then bless a hash reference in every single class, which is what we had to do back when I programmed perl.



Since about 2008-01 or so, using Moose, we've been able to do something like:

    { package Point;
      use Moose;
      has 'x' => (is => 'ro', isa => 'Int', required => 1);
      has 'y' => (is => 'ro', isa => 'Int', required => 1);
      sub as_string {
        my $self = shift;
        return sprintf '(%d,%d)', $self->x, $self->y;
      }
    }
    my $origin = Point->new(0, 0);
    say $origin->as_string;


Moose is one of those things I always mention when people ask what Perl did right.

It wouldn't hurt other languages to have a higher order object templating system.

Other things it did right is the taint flag to keep user supplied data from hitting sensitive functions by way of the type system. (In Rust you can declare your own type to achieve this, and it's actually enforced.)

The unicode system was actually decent in ways nothing else is, at least until the Go and Rust generation. Python 3 wasn't nearly as complete despite being more than a decade later.

If people ask what Perl did wrong, the dereferencing syntax is at the top of my list. Not having multidimensional arrays as a first class type probably did more to scare people away than anything else.


After the first deref, deref is assumed. Given:

  my $aref = [1,2,[3,4,5,[6,7] ] ];
You can do

  print $aref->[2][3][1];
which yields:

  7
Or

  my @array = (1,2,[3,4,5,[6,7] ] );
  print $array[2][3][1];
  7


And in the new class syntax, when it's done, that's (roughly):

    class Point {
        field $x :param :reader;
        field $y :param :reader;

        method as_string() {
            return "($x,$y)";
        }
    }
    my $origin = Point->new( x => 0, y => 0 );
    say $origin->as_string;
I know which of the two I'd rather write :)

The main limitation is that we don't have the `isa => 'Int'` check. That's largely because adding types is something for the entire language, not just this new syntax, so it had to be delayed.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: