LoginSignup
8
6

More than 5 years have passed since last update.

Perl でアンダースコアのみの変数をみた

Posted at

たまたま http://cpansearch.perl.org/src/LBROCARD/Net-Amazon-S3-0.53/lib/Net/Amazon/S3/Bucket.pm のソースを読んでいたら、_ のみの変数名がでてきた。

sub _content_sub {
    my $filename  = shift;
    my $stat      = stat($filename);
    my $remaining = $stat->size;
    my $blksize   = $stat->blksize || 4096;

    croak "$filename not a readable file with fixed size"
        unless -r $filename and ( -f _ || $remaining ); # ココ!
    my $fh = IO::File->new( $filename, 'r' )
        or croak "Could not open $filename: $!";
    $fh->binmode;

はて、こんな文法あったっけ?と思って調べると、例えばここ http://www.tutorialspoint.com/perl/perl_special_variables.htm には、

The special filehandle used to cache the information from the last stat, lstat, or file test operator.

という説明がある。直前のファイルテスト演算子でテストしたファイルを _ で呼び出せるとのこと。

if -f $filename && -r $filename && -s $filename

なんかは、

if -f $filename && -r _ && -s _

と書けちゃうってことですね。

知らんかった・・・。

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6