1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-06 23:06:49 +02:00

Merge pull request #1131 from mcanlas/master

[perl/en] Whitespace and for loop improvements
This commit is contained in:
Levi Bostian
2015-06-09 20:56:20 -05:00

View File

@@ -49,7 +49,7 @@ my %fruit_color = ("apple", "red", "banana", "yellow");
my %fruit_color = ( my %fruit_color = (
apple => "red", apple => "red",
banana => "yellow", banana => "yellow",
); );
# Scalars, arrays and hashes are documented more fully in perldata. # Scalars, arrays and hashes are documented more fully in perldata.
# (perldoc perldata). # (perldoc perldata).
@@ -60,17 +60,17 @@ my %fruit_color = (
# Perl has most of the usual conditional and looping constructs. # Perl has most of the usual conditional and looping constructs.
if ( $var ) { if ($var) {
... ...
} elsif ( $var eq 'bar' ) { } elsif ($var eq 'bar') {
... ...
} else { } else {
... ...
} }
unless ( condition ) { unless (condition) {
... ...
} }
# This is provided as a more readable version of "if (!condition)" # This is provided as a more readable version of "if (!condition)"
# the Perlish post-condition way # the Perlish post-condition way
@@ -78,19 +78,29 @@ print "Yow!" if $zippy;
print "We have no bananas" unless $bananas; print "We have no bananas" unless $bananas;
# while # while
while ( condition ) { while (condition) {
... ...
} }
# for and foreach # for loops and iteration
for ($i = 0; $i <= $max; $i++) { for (my $i = 0; $i < $max; $i++) {
... print "index is $i";
} }
foreach (@array) { for (my $i = 0; $i < @elements; $i++) {
print "This element is $_\n"; print "Current element is " . $elements[$i];
} }
for my $element (@elements) {
print $element;
}
# implicitly
for (@elements) {
print;
}
#### Regular expressions #### Regular expressions
@@ -130,7 +140,9 @@ my @lines = <$in>;
sub logger { sub logger {
my $logmessage = shift; my $logmessage = shift;
open my $logfile, ">>", "my.log" or die "Could not open my.log: $!"; open my $logfile, ">>", "my.log" or die "Could not open my.log: $!";
print $logfile $logmessage; print $logfile $logmessage;
} }