Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Having ported 20 Perl 5 CPAN modules to Perl 6 in the last few days, these are some things I noticed:

  • Perl 6 is sexy! :)
  • Before you start porting a module, make sure you understand the class hierarchy of that module. It helps if you've actually used that module in Perl 5 :)
  • Port a module even if it depends on some other (not yet ported) modules -- the dependencies can be ported later on.
  • Often, the translation P5 → P6 is quite mechanic:
    • $array[idx]
      @array[idx]
    • a ? b : c
      a ?? b :: c
    • $self->method(...)
      .method(...)
    • sub { my ($self, $a, $b) = @_; ... }
      method($a, $b) { ... }
    • $x =~ s/.../.../g
      $x ~~ s:g/.../.../
    • $self->{foo}
      $.foo
    • $foo = "bar" unless defined $foo
      $foo //= "bar" # (//) and (//=) will be in 5.9, too, IIRC
    • if($foo eq "a" or $foo eq "b" or $foo eq "c") {...}
      if $foo eq "a"|"b"|"c" {...}
    • foreach my $foo (@baz) {...}
      for @baz -> $foo {...}
    • Regular expressions:
      • [abc]<[abc]>
      • [^abc]<-[abc]>
      • (?:...)[...]
  • Often, you can remove all that Perl 5 argument parsing and simply substitute it by a nice subroutine|method|whatever signature.
  • # This Perl 5 exporting code... require Exporter; our @ISA = qw< Exporter >; our @EXPORT = qw< foo >; sub foo {...} # ...becomes this in Perl 6: sub foo(...) is export {...}
  • return map {.4.} sort {.3.} grep {.2.} map {.1.}
    map {.1.} ==> grep {.2.} ==> sort {.3.} ==> map {.4.} ==> return
  • Especially Perl 6's translation of Perl 5's getter/setter idiom is cool:
    # Perl 5 sub get_foo { my $self = shift; my $ret = $self->{foo}; return lc $ret; # always normalize } sub set_foo { my ($self, $to) = @_; $to =~ s/\s+$//; # strip whitespace at the end $self->{foo} = $to; } # Perl 6: has $:foo; sub foo() is rw { return new Proxy: FETCH => { lc $:foo }, STORE => -> $to is copy { $to ~~ s/\s+$//; $:foo = $to; }; } # And then: say $obj.foo; $obj.foo = "..."; # Notice: Standard assignment syntax! # Assignments should look line assingments, not like method calls
  • If you trust the user to give correct data to accessors, you can also use:
    has $.foo is rw;
    Alternatively, if you don't trust The Evil Users:
    subtype OddInt of Int where { $^n % 2 == 1 } has OddInt $.foo is rw; # And then: $obj.foo = 12; # will die (at compile-time!)

You can find a perhaps more recent version of this document in the Pugs SVN repository.

--Ingo


In reply to Mini HowTo: How to port Perl 5 modules to Perl 6 by iblech

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (16)
    As of 2014-07-23 09:41 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (139 votes), past polls