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

One feature which I believe is distinctly available only in Perl 6 is it's grammar feature. ( think class/method based regular expressions that look similar to BNF )

As an example look at this Perl 6 grammar for JSON: https://github.com/moritz/json/blob/master/lib/JSON/Tiny/Gra...

Perl 6's best feature is that it combines many features from many languages in a way that allows combining them cleanly, and without any of them feeling like they were glommed on. (unlike Perl 5's object system which was inspired by Python's object system)

If you can get past the syntax differences from other languages, and program in it for a while, you may never want to go back to any other language.

If you can't get past the syntax, please say that you "don't like the syntax" instead of "it's unreadable" or "it's a write-only language" or "it's difficult to learn". ( I for one found it quite easy to learn, and can read it quickly. )

---

Really can you make all of the following examples clearer in any other single language?

  constant powers-of-two = 2,4,8 ... *; # an infinite deductive list

  # perhaps that would be more clear as:
  constant powers-of-two = 2, 4, 8, 16, 32, 64 ... *;

  # list of pairs using the zip meta-operator (Z)
  # combined with the pair constructor operator (=>)
  my @a = 'a'..'z' Z=> 0..*; # ( a => 0, b => 1, etc )
  my $new-password = ('a'..'z','A'..'Z',0..9).roll(12).join; # 'ygHIHbi4XgUV'

  # the zero in the following is to prevent an off-by-one error
  # when accessing by index
  # * + * creates a code ref that takes two inputs and numerically adds them together
  constant fibonacci-list = 0, 1, 1, * + * ... *;

   # takes about a second the first time it's accessed currently
  say fibonacci-list[10000].chars == 2090;

  # if you wanted it to look more like haskell you could write it like this:
  constant fibonacci-list = 0, 1, 1, &[+] ... *;

  my @dice-rolls := ('⚀'..'⚅').roll(*); # infinite list of dice rolls

  # deck of cards using the cross meta-operator (X)
  # and the string concatenation operator (~)

  my @deck = 2..10,<J Q K A> X~ <♡ ♢ ♣ ♠>;

  my @shuffled = @deck.pick(*);

  # ( .say ) is short for ( $_.say )
  # prints the first 6 numbers (0..5) each on it's own line
  for ^6 { .say }

  # prints the numbers 0..5 each on it's own line,
  # possibly randomized,
  # possibly each in it's own thread ( texas version )

  (0..^6)>>.say;
  (^6)».say; # ditto ( french version )

  # ( those were purposefully misusing an auto-threading list operator >> )

  # says a random number (1..5), 5 times out of 6
  if (0..5).pick -> $number { say $number }
  if (0..5).pick { say $^number } # ditto
---

That was just a sampling of the bread-and-butter constructs (some of which were used in a way that most people wouldn't use them). There are also operators for dealing with Sets for example. ( ⋃ and (elem) being two of them )

see: http://doc.perl6.org/

You can even create new operators the same way you define a subroutine ( That's all they are in the Rakudo implementation, for most of them. )

  # common factorial example
  sub postfix:<!> ( Int \n where * >= 0 ) is tighter(&infix:<*>) { [*] 1..n }
  say 5!; # 120



That is a quite convincing argumentation in favour of Perl6. Thanks! I really like this kind of code.




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

Search: