diff options
author | Jonathan Scott Duff <duff@pobox.com> | 2015-06-25 22:58:56 -0500 |
---|---|---|
committer | Jonathan Scott Duff <duff@pobox.com> | 2015-06-25 22:58:56 -0500 |
commit | 2162639cd901a81a24eb4a566ba5108da87634cf (patch) | |
tree | 3bef6c762bcc04609e0a6cb219c3c169e5aa5748 /perl.html.markdown | |
parent | 041064416115985ef336babe6ef7dbac726327fa (diff) | |
parent | ef771384ae672e341ec309cf71cf372143607892 (diff) |
Merge remote-tracking branch 'upstream/master'
Conflicts:
perl6.html.markdown
Diffstat (limited to 'perl.html.markdown')
-rw-r--r-- | perl.html.markdown | 60 |
1 files changed, 36 insertions, 24 deletions
diff --git a/perl.html.markdown b/perl.html.markdown index aac95939..3c0699ad 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,29 @@ 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; +} #### Regular expressions @@ -129,9 +139,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: |