From b61eba73fbb5ab8283e4cff337affe15bda35a2d Mon Sep 17 00:00:00 2001 From: Korjavin Ivan Date: Tue, 13 Aug 2013 20:37:52 +0600 Subject: beta perl --- perl.html.markdown | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 perl.html.markdown (limited to 'perl.html.markdown') diff --git a/perl.html.markdown b/perl.html.markdown new file mode 100644 index 00000000..7e0b1183 --- /dev/null +++ b/perl.html.markdown @@ -0,0 +1,122 @@ +--- +name: perl +category: language +language: perl +filename: learnperl.pl +contributors: + - ["Korjavin Ivan", "http://github.com/korjavin"] +--- + +Perl 5 is a highly capable, feature-rich programming language with over 25 years of development. + +Perl 5 runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large scale development projects. + +```perl +# Single line comments start with a hash. + +/* +Multi-line comments look like this. +*/ + + +#### Perl variable types + +# Variables begin with the $ symbol. +# A valid variable name starts with a letter or underscore, +# followed by any number of letters, numbers, or underscores. + +### Perl has three main variable types: scalars, arrays, and hashes. + +## Scalars +# A scalar represents a single value: +my $animal = "camel"; +my $answer = 42; + +# Scalar values can be strings, integers or floating point numbers, and Perl will automatically convert between them as required. + +## Arrays +# An array represents a list of values: +my @animals = ("camel", "llama", "owl"); +my @numbers = (23, 42, 69); +my @mixed = ("camel", 42, 1.23); + + + +## Hashes +# A hash represents a set of key/value pairs: + +my %fruit_color = ("apple", "red", "banana", "yellow"); + +# You can use whitespace and the "=>" operator to lay them out more nicely: + +my %fruit_color = ( + apple => "red", + banana => "yellow", + ); +# Scalars, arrays and hashes are documented more fully in perldata. (perldoc perldata). + +# More complex data types can be constructed using references, which allow you to build lists and hashes within lists and hashes. + +#### Conditional and looping constructs + +# Perl has most of the usual conditional and looping constructs. + +if ( $var ) { + ... +} elsif ( $var eq 'bar' ) { + ... +} else { + ... +} + +unless ( condition ) { + ... + } +# This is provided as a more readable version of "if (!condition)" + +# the Perlish post-condition way +print "Yow!" if $zippy; +print "We have no bananas" unless $bananas; + +# while + while ( condition ) { + ... + } + + +# for and foreach +for ($i = 0; $i <= $max; $i++) { + ... + } + +foreach (@array) { + print "This element is $_\n"; + } + + +#### Regular expressions + +# Perl's regular expression support is both broad and deep, and is the subject of lengthy documentation in perlrequick, perlretut, and elsewhere. However, in short: + +# Simple matching +if (/foo/) { ... } # true if $_ contains "foo" +if ($a =~ /foo/) { ... } # true if $a contains "foo" + +# Simple substitution + +$a =~ s/foo/bar/; # replaces foo with bar in $a +$a =~ s/foo/bar/g; # replaces ALL INSTANCES of foo with bar in $a + + +``` + +#### Using Perl modules + +# Perl modules provide a range of features to help you avoid reinventing the wheel, and can be downloaded from CPAN ( http://www.cpan.org/ ). A number of popular modules are included with the Perl distribution itself. + +# perlfaq contains questions and answers related to many common tasks, and often provides suggestions for good CPAN modules to use. + +#### Further Reading + +[Learn at www.perl.com](http://www.perl.org/learn.html) + and perldoc perlintro -- cgit v1.2.3 From 90b79ea04a242f6b5bbb6dae3b41c0d5b2b4aea9 Mon Sep 17 00:00:00 2001 From: Korjavin Ivan Date: Tue, 13 Aug 2013 20:39:25 +0600 Subject: md fix --- perl.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'perl.html.markdown') diff --git a/perl.html.markdown b/perl.html.markdown index 7e0b1183..fec4ce05 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -112,9 +112,9 @@ $a =~ s/foo/bar/g; # replaces ALL INSTANCES of foo with bar in $a #### Using Perl modules -# Perl modules provide a range of features to help you avoid reinventing the wheel, and can be downloaded from CPAN ( http://www.cpan.org/ ). A number of popular modules are included with the Perl distribution itself. +Perl modules provide a range of features to help you avoid reinventing the wheel, and can be downloaded from CPAN ( http://www.cpan.org/ ). A number of popular modules are included with the Perl distribution itself. -# perlfaq contains questions and answers related to many common tasks, and often provides suggestions for good CPAN modules to use. +perlfaq contains questions and answers related to many common tasks, and often provides suggestions for good CPAN modules to use. #### Further Reading -- cgit v1.2.3 From 3f92d3345f31246e5d547ca47e9c725be2a193b4 Mon Sep 17 00:00:00 2001 From: Korjavin Ivan Date: Tue, 13 Aug 2013 20:42:22 +0600 Subject: mistake fix --- perl.html.markdown | 4 ---- 1 file changed, 4 deletions(-) (limited to 'perl.html.markdown') diff --git a/perl.html.markdown b/perl.html.markdown index fec4ce05..024bd851 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -14,10 +14,6 @@ Perl 5 runs on over 100 platforms from portables to mainframes and is suitable f ```perl # Single line comments start with a hash. -/* -Multi-line comments look like this. -*/ - #### Perl variable types -- cgit v1.2.3 From c8c0808657417bc40150bb5f98994a98f4def1fd Mon Sep 17 00:00:00 2001 From: Korjavin Ivan Date: Thu, 22 Aug 2013 08:11:07 +0600 Subject: More tutorial links --- perl.html.markdown | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'perl.html.markdown') diff --git a/perl.html.markdown b/perl.html.markdown index 024bd851..f03e7244 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -114,5 +114,7 @@ perlfaq contains questions and answers related to many common tasks, and often p #### Further Reading -[Learn at www.perl.com](http://www.perl.org/learn.html) - and perldoc perlintro + - [perl-tutorial](http://perl-tutorial.org/) + - [Learn at www.perl.com](http://www.perl.org/learn.html) + - [perldoc](http://perldoc.perl.org/) + - and perl built-in : `perldoc perlintro` -- cgit v1.2.3 From 9a36a51ccc1cb7d64baac17a067be71a7923afdd Mon Sep 17 00:00:00 2001 From: Korjavin Ivan Date: Thu, 22 Aug 2013 08:16:54 +0600 Subject: Few words about subs and file i/o --- perl.html.markdown | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'perl.html.markdown') diff --git a/perl.html.markdown b/perl.html.markdown index f03e7244..18339dde 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -104,6 +104,35 @@ $a =~ s/foo/bar/; # replaces foo with bar in $a $a =~ s/foo/bar/g; # replaces ALL INSTANCES of foo with bar in $a +#### Files and I/O + +# You can open a file for input or output using the "open()" function. + +open(my $in, "<", "input.txt") or die "Can't open input.txt: $!"; +open(my $out, ">", "output.txt") or die "Can't open output.txt: $!"; +open(my $log, ">>", "my.log") or die "Can't open my.log: $!"; + +# You can read from an open filehandle using the "<>" operator. In scalar context it reads a single line from +# the filehandle, and in list context it reads the whole file in, assigning each line to an element of the list: + +my $line = <$in>; +my @lines = <$in>; + +#### Writing subroutines + +# Writing subroutines is easy: + +sub logger { + my $logmessage = shift; + open my $logfile, ">>", "my.log" or die "Could not open my.log: $!"; + print $logfile $logmessage; +} + +# Now we can use the subroutine just as any other built-in function: + +logger("We have a logger subroutine!"); + + ``` #### Using Perl modules -- cgit v1.2.3 From 2c7fa291a34eb88380e9cf8c8e35a49e70114251 Mon Sep 17 00:00:00 2001 From: Christos Kontas Date: Fri, 4 Oct 2013 12:44:13 +0300 Subject: [Perl] Remove some redundant spaces --- perl.html.markdown | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'perl.html.markdown') diff --git a/perl.html.markdown b/perl.html.markdown index 18339dde..b04b76cd 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -131,13 +131,11 @@ sub logger { # Now we can use the subroutine just as any other built-in function: logger("We have a logger subroutine!"); - - ``` #### Using Perl modules -Perl modules provide a range of features to help you avoid reinventing the wheel, and can be downloaded from CPAN ( http://www.cpan.org/ ). A number of popular modules are included with the Perl distribution itself. +Perl modules provide a range of features to help you avoid reinventing the wheel, and can be downloaded from CPAN (http://www.cpan.org/). A number of popular modules are included with the Perl distribution itself. perlfaq contains questions and answers related to many common tasks, and often provides suggestions for good CPAN modules to use. -- cgit v1.2.3 From fba9728e46d46ab8d9c8b2ae02778d6bd1213837 Mon Sep 17 00:00:00 2001 From: Christos Kontas Date: Fri, 4 Oct 2013 12:46:28 +0300 Subject: [perl] Fix list with links at "Further Reading" --- perl.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'perl.html.markdown') diff --git a/perl.html.markdown b/perl.html.markdown index b04b76cd..ad9155e4 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -141,7 +141,7 @@ perlfaq contains questions and answers related to many common tasks, and often p #### Further Reading - - [perl-tutorial](http://perl-tutorial.org/) - - [Learn at www.perl.com](http://www.perl.org/learn.html) - - [perldoc](http://perldoc.perl.org/) - - and perl built-in : `perldoc perlintro` + - [perl-tutorial](http://perl-tutorial.org/) + - [Learn at www.perl.com](http://www.perl.org/learn.html) + - [perldoc](http://perldoc.perl.org/) + - and perl built-in : `perldoc perlintro` -- cgit v1.2.3 From 777c333ff078a7095f9b7ac303829b249f499b21 Mon Sep 17 00:00:00 2001 From: Sam Dodrill Date: Mon, 14 Apr 2014 11:04:44 -0700 Subject: Remove references to hash and hashtag in favor of number symbol --- perl.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'perl.html.markdown') diff --git a/perl.html.markdown b/perl.html.markdown index ad9155e4..da2e0cdf 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -12,7 +12,7 @@ Perl 5 is a highly capable, feature-rich programming language with over 25 years Perl 5 runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large scale development projects. ```perl -# Single line comments start with a hash. +# Single line comments start with a number symbol. #### Perl variable types -- cgit v1.2.3 From fac959a398c7242086c23a9c565f2f56150e8e0c Mon Sep 17 00:00:00 2001 From: Timothy Malcham Date: Tue, 10 Jun 2014 10:06:08 -0700 Subject: Shortens line lengths of comments in Perl guide --- perl.html.markdown | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'perl.html.markdown') diff --git a/perl.html.markdown b/perl.html.markdown index da2e0cdf..aac95939 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -28,7 +28,8 @@ Perl 5 runs on over 100 platforms from portables to mainframes and is suitable f my $animal = "camel"; my $answer = 42; -# Scalar values can be strings, integers or floating point numbers, and Perl will automatically convert between them as required. +# Scalar values can be strings, integers or floating point numbers, and +# Perl will automatically convert between them as required. ## Arrays # An array represents a list of values: @@ -49,9 +50,11 @@ my %fruit_color = ( apple => "red", banana => "yellow", ); -# Scalars, arrays and hashes are documented more fully in perldata. (perldoc perldata). +# Scalars, arrays and hashes are documented more fully in perldata. +# (perldoc perldata). -# More complex data types can be constructed using references, which allow you to build lists and hashes within lists and hashes. +# More complex data types can be constructed using references, which allow you +# to build lists and hashes within lists and hashes. #### Conditional and looping constructs @@ -92,7 +95,9 @@ foreach (@array) { #### Regular expressions -# Perl's regular expression support is both broad and deep, and is the subject of lengthy documentation in perlrequick, perlretut, and elsewhere. However, in short: +# Perl's regular expression support is both broad and deep, and is the subject +# of lengthy documentation in perlrequick, perlretut, and elsewhere. +# However, in short: # Simple matching if (/foo/) { ... } # true if $_ contains "foo" @@ -112,8 +117,9 @@ open(my $in, "<", "input.txt") or die "Can't open input.txt: $!"; open(my $out, ">", "output.txt") or die "Can't open output.txt: $!"; open(my $log, ">>", "my.log") or die "Can't open my.log: $!"; -# You can read from an open filehandle using the "<>" operator. In scalar context it reads a single line from -# the filehandle, and in list context it reads the whole file in, assigning each line to an element of the list: +# You can read from an open filehandle using the "<>" operator. In scalar +# context it reads a single line from the filehandle, and in list context it +# reads the whole file in, assigning each line to an element of the list: my $line = <$in>; my @lines = <$in>; -- cgit v1.2.3 From 75ecb5aa8133325f14d97d00675c3b039da06530 Mon Sep 17 00:00:00 2001 From: Mark Canlas Date: Wed, 10 Jun 2015 00:00:12 +0200 Subject: tamed indentation/whitespace --- perl.html.markdown | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) (limited to 'perl.html.markdown') diff --git a/perl.html.markdown b/perl.html.markdown index aac95939..ab8c7a32 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -47,9 +47,9 @@ my %fruit_color = ("apple", "red", "banana", "yellow"); # You can use whitespace and the "=>" operator to lay them out more nicely: my %fruit_color = ( - apple => "red", - banana => "yellow", - ); + apple => "red", + banana => "yellow", +); # Scalars, arrays and hashes are documented more fully in perldata. # (perldoc perldata). @@ -60,17 +60,17 @@ my %fruit_color = ( # Perl has most of the usual conditional and looping constructs. -if ( $var ) { - ... -} elsif ( $var eq 'bar' ) { - ... +if ($var) { + ... +} elsif ($var eq 'bar') { + ... } else { - ... + ... } -unless ( condition ) { - ... - } +unless (condition) { + ... +} # This is provided as a more readable version of "if (!condition)" # the Perlish post-condition way @@ -78,19 +78,19 @@ print "Yow!" if $zippy; print "We have no bananas" unless $bananas; # while - while ( condition ) { - ... - } +while (condition) { + ... +} # for and foreach for ($i = 0; $i <= $max; $i++) { - ... - } + ... +} foreach (@array) { - print "This element is $_\n"; - } + print "This element is $_\n"; +} #### Regular expressions @@ -129,9 +129,11 @@ my @lines = <$in>; # Writing subroutines is easy: sub logger { - my $logmessage = shift; - open my $logfile, ">>", "my.log" or die "Could not open my.log: $!"; - print $logfile $logmessage; + my $logmessage = shift; + + open my $logfile, ">>", "my.log" or die "Could not open my.log: $!"; + + print $logfile $logmessage; } # Now we can use the subroutine just as any other built-in function: -- cgit v1.2.3 From 66379e80cdc805612327e71a6cfa79d4699a8186 Mon Sep 17 00:00:00 2001 From: Mark Canlas Date: Wed, 10 Jun 2015 00:05:36 +0200 Subject: revamped for loops --- perl.html.markdown | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'perl.html.markdown') diff --git a/perl.html.markdown b/perl.html.markdown index ab8c7a32..3c0699ad 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -83,13 +83,23 @@ while (condition) { } -# for and foreach -for ($i = 0; $i <= $max; $i++) { - ... +# for loops and iteration +for (my $i = 0; $i < $max; $i++) { + print "index is $i"; +} + +for (my $i = 0; $i < @elements; $i++) { + print "Current element is " . $elements[$i]; } -foreach (@array) { - print "This element is $_\n"; +for my $element (@elements) { + print $element; +} + +# implicitly + +for (@elements) { + print; } -- cgit v1.2.3 From e4931ee126a23a851c6a2ab474e84996e1b28f3e Mon Sep 17 00:00:00 2001 From: Ahmad Fatoum Date: Fri, 19 Jun 2015 23:11:38 +0200 Subject: Fixed misinfo regarding sigils also # is more commonly called number sign than number symbol. --- perl.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'perl.html.markdown') diff --git a/perl.html.markdown b/perl.html.markdown index 3c0699ad..4e172406 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -12,16 +12,16 @@ Perl 5 is a highly capable, feature-rich programming language with over 25 years Perl 5 runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large scale development projects. ```perl -# Single line comments start with a number symbol. +# Single line comments start with a number sign. #### Perl variable types -# Variables begin with the $ symbol. +# Variables begin with a sigil, which is a symbol showing the type. # A valid variable name starts with a letter or underscore, # followed by any number of letters, numbers, or underscores. -### Perl has three main variable types: scalars, arrays, and hashes. +### Perl has three main variable types: $scalar, @array, and %hash. ## Scalars # A scalar represents a single value: -- cgit v1.2.3