From 3c175719dcecc21be65c0261c508a7119c6812c2 Mon Sep 17 00:00:00 2001 From: fluffyhedgehog Date: Thu, 8 Jun 2017 20:04:48 +0300 Subject: [PATCH 1/7] Adds perl 5 implementation of first 3 examples --- Perl_5/1-simple.pl | 44 +++++++++++++++++++++++++++++++++++++++++ Perl_5/2-scopes.pl | 43 ++++++++++++++++++++++++++++++++++++++++ Perl_5/3-abstraction.pl | 13 ++++++++++++ 3 files changed, 100 insertions(+) create mode 100755 Perl_5/1-simple.pl create mode 100755 Perl_5/2-scopes.pl create mode 100755 Perl_5/3-abstraction.pl diff --git a/Perl_5/1-simple.pl b/Perl_5/1-simple.pl new file mode 100755 index 0000000..3747b52 --- /dev/null +++ b/Perl_5/1-simple.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl + +use strict; + +sub inc1 { + return 1 + shift; +} +# everytime shift is called inside a function without array passed to shift operator +# it returns next argument +# note that shift is an operator, so it thinks of things next to it as of its arguments + +sub inc2 { + return @_[0] + 1; +} +# @_ is an array of arguments. Dont do $var = _@; +# because context of this operation is scalar +# and any array in scalar context represents its length. +# Not talking about context here. +# also note that @_ is immutable + +sub inc3 { + my $arg = shift; + $arg++; +} +# perl functions always return value. +# Explicitly via return or implicitly last calculated value. +# Thus, its better to use return. +# note that $arg++ in perl is actually computed after return +# so mind using prefix in such operations + +sub sum { + my $var; + my $result = 0; + foreach $var (@_) { + $result += $var; + } + return $result; +} +# but we can easily iterate through args with a foreach + +print inc1(2); # returns 3 +print inc2(2); # returns 3 +print inc3(2); # returns 2 (look in comments near function definition) +print sum(1, 2, 3, 4); # returns 10 diff --git a/Perl_5/2-scopes.pl b/Perl_5/2-scopes.pl new file mode 100755 index 0000000..3a84435 --- /dev/null +++ b/Perl_5/2-scopes.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl + +use strict; +# in original repo this file is named "context" +# i take the responsibility to rename it to "scopes" +# because of two reasons: +# 1. context is name of entirely different principle +# 2. scope is just more logical :) + + +my @cities = ("Athens", "Roma", "London", "Beijing", "Kiev", "Riga"); + +my $f = sub {return scalar @_;}; +# we pass an array to function, @_ represents it, and shift starts getting values from array. +# if we need to pass multiple arrays, or array and variable - we need to use references +# (not talking about them here) +# also, its nearly always better to use references, in terms of memory usage. + +sub f2 { + my @cities = ("Athens", "Roma"); + my $f = sub {return uc shift}; # uc stands for uppercase + print @cities; + print map($f->($_), @cities); # here we map by passing each element to function via $_ + print "\n"; + { + my $f = sub {return lc shift}; + print @cities; + print map $f->($_), @cities; # actually we can call any function with or without braces + print "\n"; + } + { + my @cities = ("London", "Beijing", "Kiev"); + print @cities; + print map $f->($_), @cities; + print "\n"; + } +} + +f2(); + +print @cities; +print $f->(@cities); +print "\n"; diff --git a/Perl_5/3-abstraction.pl b/Perl_5/3-abstraction.pl new file mode 100755 index 0000000..190b479 --- /dev/null +++ b/Perl_5/3-abstraction.pl @@ -0,0 +1,13 @@ +#!/usr/bin/perl + +sub square { + return shift() ** 2; +} +sub cube { + return shift() ** 3; +} +print 10 ** 2 . "\n"; +print square(10) . "\n"; + +print 10 ** 3 . "\n"; +print cube 10 . "\n"; # demonstrating that feature again From 79930ea479ae4905f199c4e35b9bc7b95ba3d6e1 Mon Sep 17 00:00:00 2001 From: fluffyhedgehog Date: Thu, 8 Jun 2017 20:15:37 +0300 Subject: [PATCH 2/7] Completes 1 --- Perl_5/1-simple.pl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Perl_5/1-simple.pl b/Perl_5/1-simple.pl index 3747b52..7a19d18 100755 --- a/Perl_5/1-simple.pl +++ b/Perl_5/1-simple.pl @@ -38,7 +38,14 @@ sub sum { } # but we can easily iterate through args with a foreach +my $max = sub { + ($a, $b) = @_; + return $a > $b ? $a : $b; +}; +# A lambda function! Also, note how we get args + print inc1(2); # returns 3 print inc2(2); # returns 3 print inc3(2); # returns 2 (look in comments near function definition) print sum(1, 2, 3, 4); # returns 10 +print $max->(8, 10); # returns 10, note how we make function call From 2950c6e687ad4a1beb92776e55e567c29c4a00ba Mon Sep 17 00:00:00 2001 From: fluffyhedgehog Date: Thu, 8 Jun 2017 22:58:32 +0300 Subject: [PATCH 3/7] Adds some more examples and modifies 1 to show correct argument syntax --- Perl_5/1-simple.pl | 17 ++++++++-------- Perl_5/4-introspection.pl | 4 ++++ Perl_5/5-default.pl | 42 +++++++++++++++++++++++++++++++++++++++ Perl_5/6-spread.pl | 34 +++++++++++++++++++++++++++++++ Perl_5/7-new.pl | 15 ++++++++++++++ 5 files changed, 103 insertions(+), 9 deletions(-) create mode 100755 Perl_5/4-introspection.pl create mode 100755 Perl_5/5-default.pl create mode 100755 Perl_5/6-spread.pl create mode 100755 Perl_5/7-new.pl diff --git a/Perl_5/1-simple.pl b/Perl_5/1-simple.pl index 7a19d18..b13356c 100755 --- a/Perl_5/1-simple.pl +++ b/Perl_5/1-simple.pl @@ -28,15 +28,13 @@ sub inc3 { # note that $arg++ in perl is actually computed after return # so mind using prefix in such operations -sub sum { - my $var; - my $result = 0; - foreach $var (@_) { - $result += $var; - } - return $result; +sub sum($$) { + ($a, $b) = (shift, shift); + return $a + $b; } -# but we can easily iterate through args with a foreach +# we declare what do we wait on input, an then do +# fancy argument retrieval +# really the right way of doing this my $max = sub { ($a, $b) = @_; @@ -47,5 +45,6 @@ sub sum { print inc1(2); # returns 3 print inc2(2); # returns 3 print inc3(2); # returns 2 (look in comments near function definition) -print sum(1, 2, 3, 4); # returns 10 +print sum(1, 2); # returns 3 +#print sum(1, 2, 3); # produces an error. Uncomment and try it! print $max->(8, 10); # returns 10, note how we make function call diff --git a/Perl_5/4-introspection.pl b/Perl_5/4-introspection.pl new file mode 100755 index 0000000..2de6682 --- /dev/null +++ b/Perl_5/4-introspection.pl @@ -0,0 +1,4 @@ +#!/usr/bin/perl + +print "Actually, perl cant introspect functions :(\n"; + diff --git a/Perl_5/5-default.pl b/Perl_5/5-default.pl new file mode 100755 index 0000000..d1748e2 --- /dev/null +++ b/Perl_5/5-default.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl + +use strict; +use Data::Dumper; +# unless is equal to if not +# also note that we can type two ways: +# action if condition +# if (condition) action +# will assign a default only if undef comes as an arg, or there is no arg +sub unlessDef { + my $a = shift; + my $b = shift; $b = "Hello" unless defined $b; + my $c = shift; $c = 5 unless defined $c; + print Dumper { + a => $a, + b => $b, + c => $c + }; +} +print "unlessDef\n"; +unlessDef(1, 2, 3); +unlessDef(1, 2); +unlessDef(1); +unlessDef(1, 0); +# will assign a default if false is passed as a parameter +sub orDef { + my $a = shift; + my $b = shift || "Hello"; + my $c = shift || 5; + print Dumper { + a => $a, + b => $b, + c => $c + }; # prints hash +} + +print "orDef\n"; +orDef(1, 2, 3); +orDef(1, 2); +orDef(1); +orDef(1, 0); + diff --git a/Perl_5/6-spread.pl b/Perl_5/6-spread.pl new file mode 100755 index 0000000..257a5be --- /dev/null +++ b/Perl_5/6-spread.pl @@ -0,0 +1,34 @@ +#!/usr/bin/perl + +use strict; + +use Data::Dumper; +# spread operator is not needed in perl +# because only way for us to get our params is through array +# but i'll show how to iterate through params + +# ref gets us type of reference, \ creates reference + + +my $f1 = sub {print Dumper @_;}; + +$f1->(1, 2, 3); +print "\n"; + +sub f2 { + my $arg; + foreach $arg (@_) { + print ref(\$arg) . " : " . Dumper($arg); + } +} + +f2($f1, 1, 'Marcus', {field => "value"}); +print "\n"; + +# anonymous hash higner is automatically passed as a reference +# BUT unlike js arrays and hashes are defaultly passed not as arrays, +# if they arent anonymous +# here we try passing a real hash to a ref, just to show that not all hashes +# are references +my %hash; +print ref \%hash; diff --git a/Perl_5/7-new.pl b/Perl_5/7-new.pl new file mode 100755 index 0000000..fcaff22 --- /dev/null +++ b/Perl_5/7-new.pl @@ -0,0 +1,15 @@ +#!/usr/bin/perl + +use strict; + +# there is no new Function syntax in perl, but there is a fancy thing +# that is worth looking at + +my $func = 'sub ttt($$) {($a, $b) = (shift, shift); return $a+$b;}'; + +my $res = (eval($func), ttt(1, 2)); +print $res . "\n"; + +# fancy thing is that (, , , ) looks through all actions left to right and returns +# result of last +# Without braces it will go right to left From d41560cedab53853f4e932b0b9ecd029d2bc0328 Mon Sep 17 00:00:00 2001 From: fluffyhedgehog Date: Fri, 9 Jun 2017 00:27:59 +0300 Subject: [PATCH 4/7] Adds lacking chapters --- Perl_5/1-simple.pl | 4 ++-- Perl_5/7-new.pl | 1 + Perl_5/8-method.pl | 37 +++++++++++++++++++++++++++++++++++++ Perl_5/9-self.pl | 29 +++++++++++++++++++++++++++++ Perl_5/a-iife.pl | 5 +++++ 5 files changed, 74 insertions(+), 2 deletions(-) create mode 100755 Perl_5/8-method.pl create mode 100755 Perl_5/9-self.pl create mode 100755 Perl_5/a-iife.pl diff --git a/Perl_5/1-simple.pl b/Perl_5/1-simple.pl index b13356c..e4c8d1b 100755 --- a/Perl_5/1-simple.pl +++ b/Perl_5/1-simple.pl @@ -29,7 +29,7 @@ sub inc3 { # so mind using prefix in such operations sub sum($$) { - ($a, $b) = (shift, shift); + my ($a, $b) = (shift, shift); return $a + $b; } # we declare what do we wait on input, an then do @@ -37,7 +37,7 @@ ($$) # really the right way of doing this my $max = sub { - ($a, $b) = @_; + my ($a, $b) = @_; return $a > $b ? $a : $b; }; # A lambda function! Also, note how we get args diff --git a/Perl_5/7-new.pl b/Perl_5/7-new.pl index fcaff22..2f00985 100755 --- a/Perl_5/7-new.pl +++ b/Perl_5/7-new.pl @@ -4,6 +4,7 @@ # there is no new Function syntax in perl, but there is a fancy thing # that is worth looking at +# KIDS DO NOT TRY THIS AT HOME my $func = 'sub ttt($$) {($a, $b) = (shift, shift); return $a+$b;}'; diff --git a/Perl_5/8-method.pl b/Perl_5/8-method.pl new file mode 100755 index 0000000..35df2bf --- /dev/null +++ b/Perl_5/8-method.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl + +use strict; + + +package obj1; +sub new { + my $class = shift; + my $self = { + max => sub ($$) { + my ($a, $b) = (shift, shift); + return $a > $b ? $a : $b; + } # note that this method is not given $self in arguments + }; + bless $self, $class; # this is better shown in next chapter + return $self; +} +sub inc($) { + my $self = shift; # we always get $self as a first argument to a method + my $a = shift; + return ++$a; +} +sub sum($$) { + my $self = shift; + my ($a, $b) = (shift, shift); + return $a + $b; +} + +# declarement of package is between package statements. +# so we need to explicitly declare main code +package main; + +my $obj = new obj1(); +print ref $obj; +print "\n"; +print $obj->sum(1, 2) . "\n"; +print $$obj{'max'}->(1, 2) . "\n"; diff --git a/Perl_5/9-self.pl b/Perl_5/9-self.pl new file mode 100755 index 0000000..402cee6 --- /dev/null +++ b/Perl_5/9-self.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +use strict; +use Data::Dumper; +# i take the responsibility to rename this chapter too +# because of naming conventions in perl + + +package city; +sub new { + my $class = shift; + my $self = { + _name => shift, + _year => shift + }; + bless $self, $class; + # bless tells perl that $self is an object of package (class) $class + # that is passed to the constructor + return $self; +} +sub name { + my $self = shift; + return $self->{_name}; +} + +package main; + +my $kyiv = new city('Kyiv', 482); +print $kyiv->name() . "\n"; diff --git a/Perl_5/a-iife.pl b/Perl_5/a-iife.pl new file mode 100755 index 0000000..76e2f87 --- /dev/null +++ b/Perl_5/a-iife.pl @@ -0,0 +1,5 @@ +#!/usr/bin/perl + +use strict; + +(sub {print "hello world";})->(); From 325af91964307bb94df7a8daf724bd7f7205e234 Mon Sep 17 00:00:00 2001 From: fluffyhedgehog Date: Fri, 9 Jun 2017 19:18:30 +0300 Subject: [PATCH 5/7] Fixes style mistakes, switches to say, adds lvalue example --- Perl_5/1-simple.pl | 42 +++++++++++++++++++++-------------------- Perl_5/10-lvalue.pl | 17 +++++++++++++++++ Perl_5/2-scopes.pl | 32 +++++++++++++++---------------- Perl_5/3-abstraction.pl | 12 ++++++++---- Perl_5/5-default.pl | 2 +- Perl_5/6-manyargs.pl | 34 +++++++++++++++++++++++++++++++++ Perl_5/7-new.pl | 3 ++- Perl_5/8-method.pl | 11 +++++------ Perl_5/9-self.pl | 3 ++- 9 files changed, 107 insertions(+), 49 deletions(-) create mode 100755 Perl_5/10-lvalue.pl create mode 100755 Perl_5/6-manyargs.pl diff --git a/Perl_5/1-simple.pl b/Perl_5/1-simple.pl index e4c8d1b..b7ea54d 100755 --- a/Perl_5/1-simple.pl +++ b/Perl_5/1-simple.pl @@ -1,50 +1,52 @@ #!/usr/bin/perl use strict; +use v5.10; -sub inc1 { - return 1 + shift; -} # everytime shift is called inside a function without array passed to shift operator # it returns next argument # note that shift is an operator, so it thinks of things next to it as of its arguments - -sub inc2 { - return @_[0] + 1; +sub inc1 { + return 1 + shift; } + + # @_ is an array of arguments. Dont do $var = _@; # because context of this operation is scalar # and any array in scalar context represents its length. # Not talking about context here. # also note that @_ is immutable - -sub inc3 { - my $arg = shift; - $arg++; +sub inc2 { + return @_[0] + 1; } + # perl functions always return value. # Explicitly via return or implicitly last calculated value. # Thus, its better to use return. # note that $arg++ in perl is actually computed after return # so mind using prefix in such operations +sub inc3 { + my $arg = shift; + $arg++; +} +# we declare what do we wait on input, an then do +# fancy argument retrieval +# really the right way of doing this sub sum($$) { my ($a, $b) = (shift, shift); return $a + $b; } -# we declare what do we wait on input, an then do -# fancy argument retrieval -# really the right way of doing this +# A lambda function! Also, note how we get args my $max = sub { my ($a, $b) = @_; return $a > $b ? $a : $b; }; -# A lambda function! Also, note how we get args -print inc1(2); # returns 3 -print inc2(2); # returns 3 -print inc3(2); # returns 2 (look in comments near function definition) -print sum(1, 2); # returns 3 -#print sum(1, 2, 3); # produces an error. Uncomment and try it! -print $max->(8, 10); # returns 10, note how we make function call +say inc1(2); # returns 3 +say inc2(2); # returns 3 +say inc3(2); # returns 2 (look in comments near function definition) +say sum(1, 2); # returns 3 +# say sum(1, 2, 3); # produces an error. Uncomment and try it! +say $max->(8, 10); # returns 10, note how we make function call diff --git a/Perl_5/10-lvalue.pl b/Perl_5/10-lvalue.pl new file mode 100755 index 0000000..cc58518 --- /dev/null +++ b/Perl_5/10-lvalue.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl + +use strict; +use v5.10; + + +# We can do modifiable return! +my $val; + +sub lval: lvalue { + return $val; +}; + +lval() = 5; +say lval(); +lval() = 7; +say lval(); diff --git a/Perl_5/2-scopes.pl b/Perl_5/2-scopes.pl index 3a84435..c5f6819 100755 --- a/Perl_5/2-scopes.pl +++ b/Perl_5/2-scopes.pl @@ -1,6 +1,7 @@ #!/usr/bin/perl use strict; +use v5.10; # in original repo this file is named "context" # i take the responsibility to rename it to "scopes" # because of two reasons: @@ -10,34 +11,33 @@ my @cities = ("Athens", "Roma", "London", "Beijing", "Kiev", "Riga"); -my $f = sub {return scalar @_;}; -# we pass an array to function, @_ represents it, and shift starts getting values from array. -# if we need to pass multiple arrays, or array and variable - we need to use references -# (not talking about them here) +# we pass an array to function, @_ represents it, and shift +# starts getting values from array. +# if we need to pass multiple arrays, or array and variable - we +# need to use references (not talking about them here) # also, its nearly always better to use references, in terms of memory usage. +my $f = sub { return scalar @_; }; sub f2 { my @cities = ("Athens", "Roma"); my $f = sub {return uc shift}; # uc stands for uppercase - print @cities; - print map($f->($_), @cities); # here we map by passing each element to function via $_ - print "\n"; + say @cities; + # here we map by passing each element to function via $_ + say map($f->($_), @cities); { my $f = sub {return lc shift}; - print @cities; - print map $f->($_), @cities; # actually we can call any function with or without braces - print "\n"; + say @cities; + # actually we can call any function with or without braces + say map $f->($_), @cities; } { my @cities = ("London", "Beijing", "Kiev"); - print @cities; - print map $f->($_), @cities; - print "\n"; + say @cities; + say map $f->($_), @cities; } } f2(); -print @cities; -print $f->(@cities); -print "\n"; +say @cities; +say $f->(@cities); diff --git a/Perl_5/3-abstraction.pl b/Perl_5/3-abstraction.pl index 190b479..6590ec3 100755 --- a/Perl_5/3-abstraction.pl +++ b/Perl_5/3-abstraction.pl @@ -1,13 +1,17 @@ #!/usr/bin/perl +use strict; +use v5.10; + sub square { return shift() ** 2; + # braces for perl to know that shift is called without args } sub cube { return shift() ** 3; } -print 10 ** 2 . "\n"; -print square(10) . "\n"; +say 10 ** 2; +say square(10); -print 10 ** 3 . "\n"; -print cube 10 . "\n"; # demonstrating that feature again +say 10 ** 3; +say cube 10; # demonstrating that no-braces feature again diff --git a/Perl_5/5-default.pl b/Perl_5/5-default.pl index d1748e2..c63d155 100755 --- a/Perl_5/5-default.pl +++ b/Perl_5/5-default.pl @@ -2,6 +2,7 @@ use strict; use Data::Dumper; + # unless is equal to if not # also note that we can type two ways: # action if condition @@ -39,4 +40,3 @@ sub orDef { orDef(1, 2); orDef(1); orDef(1, 0); - diff --git a/Perl_5/6-manyargs.pl b/Perl_5/6-manyargs.pl new file mode 100755 index 0000000..bdcd643 --- /dev/null +++ b/Perl_5/6-manyargs.pl @@ -0,0 +1,34 @@ +#!/usr/bin/perl + +use strict; + +use Data::Dumper; +# spread operator is not needed in perl +# because only way for us to get our params is through array +# but i'll show how to iterate through params + +# ref gets us type of reference, \ creates reference + + +my $f1 = sub { print Dumper @_; }; + +$f1->(1, 2, 3); +print "\n"; + +sub f2 { + my $arg; + foreach $arg (@_) { + print ref(\$arg) . " : " . Dumper($arg); + } +} + +f2($f1, 1, 'Marcus', {field => "value"}); +print "\n"; + +# anonymous hash higner is automatically passed as a reference +# BUT unlike js arrays and hashes are defaultly passed not as arrays, +# if they arent anonymous +# here we try passing a real hash to a ref, just to show that not all hashes +# are references +my %hash; +print ref \%hash; diff --git a/Perl_5/7-new.pl b/Perl_5/7-new.pl index 2f00985..118e49a 100755 --- a/Perl_5/7-new.pl +++ b/Perl_5/7-new.pl @@ -1,6 +1,7 @@ #!/usr/bin/perl use strict; +use v5.10; # there is no new Function syntax in perl, but there is a fancy thing # that is worth looking at @@ -9,7 +10,7 @@ my $func = 'sub ttt($$) {($a, $b) = (shift, shift); return $a+$b;}'; my $res = (eval($func), ttt(1, 2)); -print $res . "\n"; +say $res; # fancy thing is that (, , , ) looks through all actions left to right and returns # result of last diff --git a/Perl_5/8-method.pl b/Perl_5/8-method.pl index 35df2bf..d984c90 100755 --- a/Perl_5/8-method.pl +++ b/Perl_5/8-method.pl @@ -1,7 +1,7 @@ #!/usr/bin/perl use strict; - +use v5.10; package obj1; sub new { @@ -26,12 +26,11 @@ ($$) return $a + $b; } -# declarement of package is between package statements. +# declaration of package is between package statements. # so we need to explicitly declare main code package main; my $obj = new obj1(); -print ref $obj; -print "\n"; -print $obj->sum(1, 2) . "\n"; -print $$obj{'max'}->(1, 2) . "\n"; +say ref $obj; +say $obj->sum(1, 2); +say $$obj{'max'}->(1, 2); diff --git a/Perl_5/9-self.pl b/Perl_5/9-self.pl index 402cee6..e4e2cb9 100755 --- a/Perl_5/9-self.pl +++ b/Perl_5/9-self.pl @@ -2,8 +2,9 @@ use strict; use Data::Dumper; -# i take the responsibility to rename this chapter too +# the chapter is renamed # because of naming conventions in perl +# actually, in perl6 there'll be an easier way of doing things package city; From c85e8b117807d548e384f96e550adda69fdd0758 Mon Sep 17 00:00:00 2001 From: fluffyhedgehog Date: Sat, 10 Jun 2017 08:19:38 +0300 Subject: [PATCH 6/7] Deletes unneeded files, corrects more mistakes --- Perl_5/1-simple.pl | 4 ++-- Perl_5/10-lvalue.pl | 1 - Perl_5/4-introspection.pl | 4 ---- Perl_5/6-spread.pl | 34 ---------------------------------- 4 files changed, 2 insertions(+), 41 deletions(-) delete mode 100755 Perl_5/4-introspection.pl delete mode 100755 Perl_5/6-spread.pl diff --git a/Perl_5/1-simple.pl b/Perl_5/1-simple.pl index b7ea54d..68a8630 100755 --- a/Perl_5/1-simple.pl +++ b/Perl_5/1-simple.pl @@ -3,9 +3,9 @@ use strict; use v5.10; -# everytime shift is called inside a function without array passed to shift operator +# everytime shift is called inside a function without array passed to it # it returns next argument -# note that shift is an operator, so it thinks of things next to it as of its arguments +# note that shift is a fucntion that awaits arguments sub inc1 { return 1 + shift; } diff --git a/Perl_5/10-lvalue.pl b/Perl_5/10-lvalue.pl index cc58518..cc74fbb 100755 --- a/Perl_5/10-lvalue.pl +++ b/Perl_5/10-lvalue.pl @@ -3,7 +3,6 @@ use strict; use v5.10; - # We can do modifiable return! my $val; diff --git a/Perl_5/4-introspection.pl b/Perl_5/4-introspection.pl deleted file mode 100755 index 2de6682..0000000 --- a/Perl_5/4-introspection.pl +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/perl - -print "Actually, perl cant introspect functions :(\n"; - diff --git a/Perl_5/6-spread.pl b/Perl_5/6-spread.pl deleted file mode 100755 index 257a5be..0000000 --- a/Perl_5/6-spread.pl +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/perl - -use strict; - -use Data::Dumper; -# spread operator is not needed in perl -# because only way for us to get our params is through array -# but i'll show how to iterate through params - -# ref gets us type of reference, \ creates reference - - -my $f1 = sub {print Dumper @_;}; - -$f1->(1, 2, 3); -print "\n"; - -sub f2 { - my $arg; - foreach $arg (@_) { - print ref(\$arg) . " : " . Dumper($arg); - } -} - -f2($f1, 1, 'Marcus', {field => "value"}); -print "\n"; - -# anonymous hash higner is automatically passed as a reference -# BUT unlike js arrays and hashes are defaultly passed not as arrays, -# if they arent anonymous -# here we try passing a real hash to a ref, just to show that not all hashes -# are references -my %hash; -print ref \%hash; From 7af4c2d0fb4fe9012c34c7c46b993cc44a3cebd1 Mon Sep 17 00:00:00 2001 From: fluffyhedgehog Date: Sun, 11 Jun 2017 01:52:32 +0300 Subject: [PATCH 7/7] Fixes typos, moves examples to fill the gap --- Perl_5/1-simple.pl | 2 +- Perl_5/{5-default.pl => 4-default.pl} | 0 Perl_5/{6-manyargs.pl => 5-manyargs.pl} | 0 Perl_5/{7-new.pl => 6-new.pl} | 0 Perl_5/{8-method.pl => 7-method.pl} | 0 Perl_5/{9-self.pl => 8-self.pl} | 0 Perl_5/{10-lvalue.pl => 9-lvalue.pl} | 0 7 files changed, 1 insertion(+), 1 deletion(-) rename Perl_5/{5-default.pl => 4-default.pl} (100%) rename Perl_5/{6-manyargs.pl => 5-manyargs.pl} (100%) rename Perl_5/{7-new.pl => 6-new.pl} (100%) rename Perl_5/{8-method.pl => 7-method.pl} (100%) rename Perl_5/{9-self.pl => 8-self.pl} (100%) rename Perl_5/{10-lvalue.pl => 9-lvalue.pl} (100%) diff --git a/Perl_5/1-simple.pl b/Perl_5/1-simple.pl index 68a8630..340bc24 100755 --- a/Perl_5/1-simple.pl +++ b/Perl_5/1-simple.pl @@ -5,7 +5,7 @@ # everytime shift is called inside a function without array passed to it # it returns next argument -# note that shift is a fucntion that awaits arguments +# note that shift is a function that awaits arguments sub inc1 { return 1 + shift; } diff --git a/Perl_5/5-default.pl b/Perl_5/4-default.pl similarity index 100% rename from Perl_5/5-default.pl rename to Perl_5/4-default.pl diff --git a/Perl_5/6-manyargs.pl b/Perl_5/5-manyargs.pl similarity index 100% rename from Perl_5/6-manyargs.pl rename to Perl_5/5-manyargs.pl diff --git a/Perl_5/7-new.pl b/Perl_5/6-new.pl similarity index 100% rename from Perl_5/7-new.pl rename to Perl_5/6-new.pl diff --git a/Perl_5/8-method.pl b/Perl_5/7-method.pl similarity index 100% rename from Perl_5/8-method.pl rename to Perl_5/7-method.pl diff --git a/Perl_5/9-self.pl b/Perl_5/8-self.pl similarity index 100% rename from Perl_5/9-self.pl rename to Perl_5/8-self.pl diff --git a/Perl_5/10-lvalue.pl b/Perl_5/9-lvalue.pl similarity index 100% rename from Perl_5/10-lvalue.pl rename to Perl_5/9-lvalue.pl