Skip to content

Instantly share code, notes, and snippets.

@koorchik
Last active December 7, 2017 19:20
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save koorchik/0619dca60b4b72ccbe1716ade5376d55 to your computer and use it in GitHub Desktop.
Perl6 consistency example

In most programming languages you have a shorter form for incrementing a value

In JavaScript you can write:

a = a + 10; // original form
a += 10; // shorter form

a = a * 10; // original form
a *= 10; // shorter form

but why you cannot write something like that:

a = a || 10; // original form
a ||= 10 // this will not work

Perl5 is more consistent

$a = $a + 10; # original form
$a += 10; # shorter form

$a = $a * 10; # original form
$a *= 10; # shorter form

$a = $a || 10; # original form
$a ||= 10; # shorter form

$a = $a && 10; # original form
$a &&= 10; # shorter form

Perl6 shocks with its consistency

$a = $a + 10; # original form
$a += 10; # shorter form

$a = $a || 10; # original form
$a ||= 10; # shorter form

# It works with method calls
my @numbers = [7,2,4,9,11,3];
@numbers = @numbers.sort(); # sorts and returns new sorted array
@numbers .= sort();  # shorter form. sort and reassign

# It works for everything, even for comma
# merge hashes
my %a = a => 1, b => 2;
%a = %a, { a => 2, c => 3} # original form
%a ,= { a => 2, c => 3} # shorter form
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment