the > I have this Perl 6 script from Rosetta, which I wanted to run on Perl 5
> (due to the Active Sate Perl and App version that I have).

If ActiveState have packaged https://metacpan.org/pod/Inline::Perl6
then please install and use that. (If they haven't, please ask them to
do so.)

> However, several syntax errors appear given the difference (some) in language.

For some simple examples P5 and P6 code is very similar and so your
"(some)" qualifier is about as appropriate as saying there's (some) difference
between Python 2 and Python 3. (There is, but not a lot.)

For code like you've picked the difference is halfway to the difference
between English and German (both are West Germanic languages but
saying there's (some) difference in language doesn't really do justice to
the difference).

With that understood...

> I'm having problem specially in the "multi infix" and even in the "norm" sub 
> routine.

----

# Simple Vector implementation
multi infix:<+>(@a, @b) { @a Z+ @b }

multi is short for multiple dispatch.

In P5, perhaps using the multi features of https://metacpan.org/pod/Dios
is the way to go, or maybe there's some other P5 package that implements
multiple dispatch.

Alternatively you could manually write the logic for routing certain
argument combinations to particular code. (That would drive me crazy.)

If there's no sub or method after the multi then it's a sub.

Reminder before continuing:

# Simple Vector implementation
multi infix:<+>(@a, @b) { @a Z+ @b }

infix:<+> names the sub.
It requires two Positional arguments (@ as a parameter).
Positional is a role; the arguments must do that role.
(Sorta like a signature expecting two arrayrefs.)
It zips them by calling infix + on pairings of their elements.

For example:

    say infix:<+> (1,2), (3,4); # (4,6)

Because it's called infix:<+> the declaration also immediately
installs it in the grammar as an infix operator, so you can
instead just write:

    say (1,2) + (3,4); # (4,6)

Note that this means it's the same as:

    say (1,2) Z+ (3,4); # (4,6)

In this latter case the infix Z metaop means  to call
the + on pairings of its two lists, i.e. 1 + 3 and 2 + 4.
Because this latter call passes two *scalar* arguments
to the infix + the multiple dispatch doesn't recursively
call the infix:<+> we defined above but instead the built
in infix:<+> which is something vaguely like:

    multi infix:<+> ($a, $b) { ... treats args as two numbers ... }

----

[+] arg1, arg2, arg3, ...

Does the same as:

arg1 + arg2 + arg3 ...

which, if args are scalars, is same as:

sum arg1, arg2, arg3

----

    return -> \t, \y, \δt {

-> ... { ... }

is similar to an anonymous sub routine.

The first ... is its parameter list.

\foo in a parameter list declares a sigil-free "single static binding variable",
a *run-time* constant value.

# gravitational constant
constant G = 6.674e-11;

That's a *compile-time* constant value.

The e-11 means the number is a floating point number.

constant year = 365.25*24*60*60;

The 365.25 (no trailing e) means the number is a rational number.

    my @a = @ABC[0..2];

sigils are "invariant". @ABC[0..2] is a slice, a list of the first
three elements of the Positional variable @ABC.

For convenience I'll call a Positional variable a "list" from now on.

    my $ab = norm(@a - @b);

Without a multi infix:<-> defined the above code would norm
the difference between the length (number of elements) of the
two lists.

        flat

P5 generally autoflattens sub lists into one long list. You have to
explicitly mark when you want an arrayref instead. In contrast P6
generally treats @foo as an arrayref. You have to explicitly flatten
if you want that.

        map G * *,

The first * is in infix operator position. It denotes the infix
multiply operator.

The second is in term position. It denotes a Whatever.

When a Whatever appears with an operator, the operator decides
whether it cares (by having a multi that includes it as a parameter).

If it doesn't care, the compiler converts the expression it's in into an
anonymous routine. So the above code fragment is the same as:

        map { G * $_ },

If you'd like to thank me, send love to Larry Wall and either install
Inline::Perl6 or ask ActiveState to package it.

love, ralph

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to