Created
August 23, 2011 15:01
-
-
Save mix3/1165357 to your computer and use it in GitHub Desktop.
perlのファイルオープンの例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
# <> でopen無しに引数のファイルを読み取れる | |
while (<>) { print; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
# or die でファイルが開けなかったら die とするのが良くある書き方 | |
open my $fh, "<".$ARGV[0] or die $!; | |
# オープンの種類 | |
# | |
# 省略 読み込み | |
# < 読み込み | |
# > 書き込み | |
# >> 追記 | |
# +< 読み書き & 新規作成しない | |
# +> 読み書き & 新規作成あり | |
# +>> 読み書き & 新規作成あり & 追記 | |
my @lines = <$fh>; | |
for my $line (@lines) { | |
chomp($line); | |
print $line."\n"; | |
} | |
# closeするのが紳士 | |
close($fh); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
# or die でファイルが開けなかったら die とするのが良くある書き方 | |
open my $fh, "<", $ARGV[0] or die $!; | |
# joinで文字列にしてしまうのもよくある書き方 | |
my $_ = join('', <$fh>); | |
print; | |
# closeするのが紳士 | |
close($fh); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Path::Class; | |
my $file = Path::Class::File->new($ARGV[0]); | |
# open('r')で読み取り | |
my $fh = $file->open('r') or die $!; | |
while (<$fh>) { print; } | |
# closeするのが紳士 | |
$fh->close; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
# or die でファイルが開けなかったら die とするのが良くある書き方 | |
my $fh; | |
open ($fh, $ARGV[0]) or die $!; | |
while (my $line = <$fh>) { | |
chomp($line); # 改行を取り除く場合はchompとかする | |
print $line."\n"; | |
} | |
# closeするのが紳士 | |
close($fh); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
# or die でファイルが開けなかったら die とするのが良くある書き方 | |
open my $fh, $ARGV[0] or die $!; | |
while (<$fh>) { | |
print; # $_ は省略可能 | |
} | |
# closeするのが紳士 | |
close($fh); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ perl fileopen_array.pl fileopen_array.pl
$ perl fileopen_array_abbr.pl fileopen_array_abbr.pl
$ perl fileopen_object.pl fileopen_object.pl
$ perl fileopen_while.pl fileopen_while.pl
$ perl fileopen_while_abbr.pl fileopen_while_abbr.pl