diff options
| author | Elena Bolshakova <lena-san@yandex-team.ru> | 2015-06-10 11:34:14 +0300 | 
|---|---|---|
| committer | Elena Bolshakova <lena-san@yandex-team.ru> | 2015-06-10 11:34:14 +0300 | 
| commit | 193f66553fc114e83e7c4cfb4607e4a1b57c4f09 (patch) | |
| tree | 30988e25d31ed6dff83cf409ad093c3c7ec9322c /perl.html.markdown | |
| parent | 676568cca8731d0dbb2d2bdeff08cc092d283177 (diff) | |
| parent | 5086480a04d27cff2380f04609210082000538d4 (diff) | |
Merge branch 'master' of https://github.com/adambard/learnxinyminutes-docs
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: | 
