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 From a8dce45150fb6938188d7ec895ade58ff264d670 Mon Sep 17 00:00:00 2001 From: Alan Berndt Date: Sat, 10 Oct 2015 11:44:50 -0700 Subject: Add example of post-condition for loop. --- perl.html.markdown | 2 ++ 1 file changed, 2 insertions(+) (limited to 'perl.html.markdown') diff --git a/perl.html.markdown b/perl.html.markdown index 4e172406..1b86f410 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -102,6 +102,8 @@ for (@elements) { print; } +# the Perlish post-condition way again +print for @elements; #### Regular expressions -- cgit v1.2.3