Perl

Perl

[vagrant@localhost ~]$ perl -v

This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi

Copyright 1987-2009, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
use strict;
use warnings;

print "hello world\n";

スカラー変数

my $x = 123_234_456

文字列

my $a = "he\tllo\n" #特殊文字、変数展開
my $a = 'hello'

配列

my @sales = (150, 200, 300);
print @sales;

my @sales = (150, 200, 300);
print $sales[1];

ハッシュ変数

my %sales = ("tanaka"=>150, "suzuki"=>200, "yamada"=>300);
print $sales{"tanaka"}

ループ

my $i = 0;

while ($i < 10){
  print "i = $i\n";
  $i++;
}&#91;/perl&#93;

配列ループ
&#91;prel&#93;
my @colors = qw(red green blue orange pink);

foreach my $color(@colors){
  print "color = $color\n";
}&#91;/perl&#93;

ハッシュループ処理
&#91;perl&#93;my %sales = ("tanaka"=>150, "suzuki"=>300, "ohira"=>200);

foreach my $key(keys(%sales)){
  print "sales for $key is $sales{$key}\n";
}

ファイルの読み込み

open(my $in, "<", "test.dat") or die("could not open file.");

while(<$in>){
    print $_;
}

close($in);

置換

$_ =~ s/abc/ABC;

正規表現

while(<$in>){
    $_ =~ s/abc/ABC/;
    if ($_ =~ /[a-z]/){
        print $_;
    }
}

サブルーチン

sub max{
  my $max = $_[0];
  if ($_[1]> $max){
    $max = $_[1];
  }
  return $max;
}


print max(2, 8);