Skip to content

Instantly share code, notes, and snippets.

@mix3
Created August 23, 2011 15:01
Show Gist options
  • Save mix3/1165357 to your computer and use it in GitHub Desktop.
Save mix3/1165357 to your computer and use it in GitHub Desktop.
perlのファイルオープンの例
#!/usr/bin/env perl
use strict;
use warnings;
# <> でopen無しに引数のファイルを読み取れる
while (<>) { print; }
#!/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);
#!/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);
#!/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;
#!/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);
#!/usr/bin/env perl
use strict;
use warnings;
# or die でファイルが開けなかったら die とするのが良くある書き方
open my $fh, $ARGV[0] or die $!;
while (<$fh>) {
print; # $_ は省略可能
}
# closeするのが紳士
close($fh);
@mix3
Copy link
Author

mix3 commented Aug 23, 2011

$ perl fileopen_array.pl fileopen_array.pl

#!/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);

$ perl fileopen_array_abbr.pl fileopen_array_abbr.pl

#!/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);

$ perl fileopen_object.pl fileopen_object.pl

#!/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;

$ perl fileopen_while.pl fileopen_while.pl

#!/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);

$ perl fileopen_while_abbr.pl fileopen_while_abbr.pl

#!/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