Having ported 20 Perl 5 CPAN modules to Perl 6 in the last few days, these are some things I noticed:
# 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 {...}
# 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
Alternatively, if you don't trust The Evil Users:has $.foo is rw;
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Mini HowTo: How to port Perl 5 modules to Perl 6
by webfiend (Vicar) on Mar 25, 2005 at 20:55 UTC | |
by iblech (Friar) on Mar 25, 2005 at 21:29 UTC | |
Re: Mini HowTo: How to port Perl 5 modules to Perl 6
by crenz (Priest) on Mar 30, 2005 at 09:08 UTC | |
by mkirank (Chaplain) on Mar 31, 2005 at 08:32 UTC |