summaryrefslogtreecommitdiffhomepage
path: root/perl.html.markdown
diff options
context:
space:
mode:
authorC. Bess <cbess@company.com>2015-11-09 17:55:53 -0600
committerC. Bess <cbess@company.com>2015-11-09 17:55:53 -0600
commitdf0992d72c2a28f140e6ff9681c505f36e19249a (patch)
tree508aa3abe4c25b957dca442560d9c95c9b1fc97a /perl.html.markdown
parentafc5ea14654e0e9cd11c7ef1b672639d12418bad (diff)
parentc460e1fafa0e9b4edc6a5cb35b970bb5cc030a81 (diff)
Merge remote-tracking branch 'adambard/master'
Conflicts: swift.html.markdown
Diffstat (limited to 'perl.html.markdown')
-rw-r--r--perl.html.markdown68
1 files changed, 41 insertions, 27 deletions
diff --git a/perl.html.markdown b/perl.html.markdown
index aac95939..1b86f410 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:
@@ -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,20 +78,32 @@ print "Yow!" if $zippy;
print "We have no bananas" unless $bananas;
# while
- while ( condition ) {
- ...
- }
+while (condition) {
+ ...
+}
+
+# for loops and iteration
+for (my $i = 0; $i < $max; $i++) {
+ print "index is $i";
+}
-# for and foreach
-for ($i = 0; $i <= $max; $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;
+}
+
+# the Perlish post-condition way again
+print for @elements;
#### Regular expressions
@@ -129,9 +141,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: