[ Executive Summary: Install Perl 6; it will not disturb your Activestate Perl 
5 : https://rakudo.org/files ]


> On Aug 8, 2019, at 3:07 PM, Rui Fernandes <rui.kep...@gmail.com> wrote:
> 
> Greetings

Hi Rui!

> 
> 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).

I think you have an all-too-common misconception. 
Perl 6 is not the next version of Perl 5; Perl 6 is a “sister language”; the 
first Perl language to completely break backwards compatibility with 1..5.

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

The compiler is only showing you the *initial* syntax errors; the total number 
of changes needed to make this run in Perl 5 will be many more than “several”.
Translating from Perl 5 to Perl 6 often only requires minimal syntax changes. 
After that, you have the option to refactor the Perl 5 idioms in the converted 
code into more concise expressions.

When code has been written in idiomatic Perl 6 (as your code has been), 
translating back to Perl 5 can be quite effortful.
For example, just the first line that defines a plus operator for lists/arrays 
representing vectors:
        multi infix:<+>(@a, @b) { @a Z+ @b }
, which would be simply invoked in Perl 6 as:
        my @foo = @this + @that;
However, translated it into Perl 5, it would look something like this 
(untested):
        sub vector_add {
                die if @_ != 2;
                my ($x_aref, $y_aref) = @_;
                # warn if @{$x_aref} != @{$y_aref};
                my $r = [];
                for my $i ( keys @{$x_aref} ) {
                        $r->[$i] = $x_aref->[$i] + $y_aref->[$i];
                }
                return $r;
        }
        my @foo = @{ vector_add( \@this, \@that };
Even `norm` takes 4 SLOCs:
        sub norm { sqrt [+] @_ X** 2 }
vs:
        sub norm {
                my $r = 0;
                $r += $_ ** 2 for @_;
                return sqrt $r;
        }

I am curious as to why you need to use this Perl 6 code in Perl 5.
You could just run it in Perl 6, since the “Rakudo Star” Perl 6 distribution 
co-exists just fine with Activestate; you can get it here: 
https://rakudo.org/files .
Or, you could use the Perl 5 version of runge_kutta from just above where you 
found the Perl 6 version, on RosettaCode: 
http://rosettacode.org/wiki/Runge-Kutta_method#Perl .
Or, you could use one of the CPAN modules that provides the same function for 
Perl 5, like Math::RungeKutta or PDL::RungeKutta .

BTW, I think your `month` constant is wrong; You have it as 21 days, where I 
usually see it as either 28 or 29.53 or 30.4375.
Also, I have no idea what the line `$t < .2;` is supposed to do; it looks like 
dead code.

(LATE EDIT)
Oh, I just saw that the entire code that you posted comes from 
https://rosettacode.org/wiki/N-body_problem#Perl_6 , and that no one has made a 
Perl 5 solution for that RosettaCode task.
If you are intending to translate the Perl 6 code to Perl 5 in order to start 
contributing to RosettaCode, please contact me directly and I will be glad to 
work with you on it. (This will be more than appropriate for “Perl Beginners”)
If you are wanting to play with it for your own purposes, definitely just 
install Perl 6 as linked above.

> Here's the script:
—code snipped--
> I'm having problem specially in the "multi infix" and even in the "norm" sub 
> routine. The problem is that I do not understand the construction of these in 
> Perl 5 (otherwise, I would translate this easely, and I wouldn't be asking 
> for help...)
> 
> Any help is apreciated.
> 
> Clear skies
> 
> Rui Fernandes


— 
Hope this helps,
Bruce Gray (Util of PerlMonks)

Reply via email to