-
-
Notifications
You must be signed in to change notification settings - Fork 185
Perl 5 implementation #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fedirsmilianets
wants to merge
7
commits into
HowProgrammingWorks:master
Choose a base branch
from
fedirsmilianets:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3c17571
Adds perl 5 implementation of first 3 examples
fedirsmilianets 79930ea
Completes 1
fedirsmilianets 2950c6e
Adds some more examples and modifies 1 to show correct argument syntax
fedirsmilianets d41560c
Adds lacking chapters
fedirsmilianets 325af91
Fixes style mistakes, switches to say, adds lvalue example
fedirsmilianets c85e8b1
Deletes unneeded files, corrects more mistakes
fedirsmilianets 7af4c2d
Fixes typos, moves examples to fill the gap
fedirsmilianets File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #!/usr/bin/perl | ||
|
|
||
| use strict; | ||
| use v5.10; | ||
|
|
||
| # everytime shift is called inside a function without array passed to it | ||
| # it returns next argument | ||
| # note that shift is a function that awaits arguments | ||
| 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 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; | ||
| } | ||
|
|
||
| # A lambda function! Also, note how we get args | ||
| my $max = sub { | ||
| my ($a, $b) = @_; | ||
| return $a > $b ? $a : $b; | ||
| }; | ||
|
|
||
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #!/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: | ||
| # 1. context is name of entirely different principle | ||
| # 2. scope is just more logical :) | ||
|
|
||
|
|
||
| my @cities = ("Athens", "Roma", "London", "Beijing", "Kiev", "Riga"); | ||
|
|
||
| # 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 | ||
| say @cities; | ||
| # here we map by passing each element to function via $_ | ||
| say map($f->($_), @cities); | ||
| { | ||
| my $f = sub {return lc shift}; | ||
| say @cities; | ||
| # actually we can call any function with or without braces | ||
| say map $f->($_), @cities; | ||
| } | ||
| { | ||
| my @cities = ("London", "Beijing", "Kiev"); | ||
| say @cities; | ||
| say map $f->($_), @cities; | ||
| } | ||
| } | ||
|
|
||
| f2(); | ||
|
|
||
| say @cities; | ||
| say $f->(@cities); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +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; | ||
| } | ||
| say 10 ** 2; | ||
| say square(10); | ||
|
|
||
| say 10 ** 3; | ||
| say cube 10; # demonstrating that no-braces feature again |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #!/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 | ||
| # KIDS DO NOT TRY THIS AT HOME | ||
|
|
||
| my $func = 'sub ttt($$) {($a, $b) = (shift, shift); return $a+$b;}'; | ||
|
|
||
| my $res = (eval($func), ttt(1, 2)); | ||
| say $res; | ||
|
|
||
| # fancy thing is that (, , , ) looks through all actions left to right and returns | ||
| # result of last | ||
| # Without braces it will go right to left |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #!/usr/bin/perl | ||
|
|
||
| use strict; | ||
| use v5.10; | ||
|
|
||
| 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; | ||
| } | ||
|
|
||
| # declaration of package is between package statements. | ||
| # so we need to explicitly declare main code | ||
| package main; | ||
|
|
||
| my $obj = new obj1(); | ||
| say ref $obj; | ||
| say $obj->sum(1, 2); | ||
| say $$obj{'max'}->(1, 2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| #!/usr/bin/perl | ||
|
|
||
| use strict; | ||
| use Data::Dumper; | ||
| # the chapter is renamed | ||
| # because of naming conventions in perl | ||
| # actually, in perl6 there'll be an easier way of doing things | ||
|
|
||
|
|
||
| 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"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #!/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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #!/usr/bin/perl | ||
|
|
||
| use strict; | ||
|
|
||
| (sub {print "hello world";})->(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on this. Mind sending a PR to change it throughout the whole repo? I'd be in support for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, it's a good idea.