Comparing Python to Perl and Ruby

Z mj41.cz

Clark Maurer published Comparing Python to Perl and Ruby probably without Perl users feedback. So here is my little help:

use strict;
use warnings;
 
sub func { # You've got to be kidding parameter passing.
           # Perl 6 intends to fix this.
    my ( $x, $arrayRef ) = @_;
    my $i = 2;  # This declares a new local variable
    ++$x;
    $arrayRef->[0] = 4;  #Modify the callers array.
    print "$x\n";
    my $newvar = 'abc'; # This declares a new local variable
    return 1;
}
my $i=1;
# Choose the right variable prefix for a list
my @array = (1, 2);
#Passing an array by reference is ugly
my $x = func( 1, \@array );
#print "$newvar\n";  # This won't compile
print "i = $i\n"; # i is 1
# You're kidding, I need to change the prefix
$array[1] = 0;
print "$array[0] $array[1]\n";  # prints 4 0

And here is Perl 6 code (not tested):

use v6;
 
sub func( $x is rw, @array is rw, $z is rw ='Cool! Default value' ) { 
    my $i = 2;  # This declares a new local variable
    ++$x; # $x and @array paremeters are defined as rw (readwrite, 
          # instead of default ro) so this compile
    @array[0] = 4; # Modify the callers array.
    say "$x $z";
    my $newvar = 'abc'; # This declares a new local variable
    return 1;
}
 
my $i=1;
# Choose the right variable prefix for a list
my @array = 1, 2;
my $x = func( 1, @array );
#say $newvar;  # This won't compile
say "i is $i"; # i is 1
@array[1] = 0;
say "@array";  # prints 4 0

See also

spřízněné weby
Jazyk