簡體   English   中英

Perl子例程如何區分文件名,文件handes,* DATA和* STDIN?

[英]How can a Perl subroutine distinguish between file names, file handes, *DATA, and *STDIN?

如果我有一個可能傳遞文件名或各種文件句柄或typeglobs的函數,那么函數如何區分這些參數 - 包括告訴區別,例如*DATA*STDIN

根據目前為止收到的答案更新了代碼謝謝大家。

use strict;
use warnings;
use FileHandle;

sub file_thing_type {
    my ($f) = shift;
    my $type;
    my $r = ref $f;
    if ($r eq 'GLOB' or ref(\$f) eq 'GLOB'){
        # Regular and built-in file handles.
        my $fn = fileno $f;
        if (defined $fn){
            my %built_in = (
                'STDIN'  => fileno(*STDIN),
                'STDOUT' => fileno(*STDOUT),
                'STDERR' => fileno(*STDERR),
                'DATA'   => fileno(*DATA),
            );
            for my $k (keys %built_in){
                if (defined $built_in{$k} and $built_in{$k} == $fn){
                    $type = $k;
                    last;
                }
            }
            $type = 'regular file handle' unless defined $type;
        }
        else {
            $type = 'non-IO glob';
        }
    }
    elsif ($r){
        # A reference of some kind.
        $type = $r;
        # Might be an IO object. Has it been opened?
        {
            no warnings 'unopened';
            $type .= ' opened' if -f $f;
        }
    }
    else {
        # File name or just some other value?
        $type = -f $f ? 'file name' : 'other';
    }
    return $type;
}

open(my $h, '<', $0) or die $!;

printf "%12s => %s\n",
       $_->[0],
       file_thing_type($_->[1])
for (
    [ 'handle',     $h                  ], # regular file handle
    [ 'DATA',       *DATA               ], # DATA if source has DATA section; else non-IO glob
    [ 'STDIN',      *STDIN              ], # STDIN
    [ 'STDOUT',     *STDOUT             ], # STDOUT
    [ 'STDERR',     *STDERR             ], # STDERR
    [ 'FOO',        *FOO, *FOO          ], # non-IO glob
    [ 'FileHandle', FileHandle->new     ], # FileHandle
    [ 'FileHandle', FileHandle->new($0) ], # FileHandle opened
    [ 'file name',  $0                  ], # file name
    [ 'not file',   ''                  ], # other
    [ 'misc',       {bar=>1}            ], # HASH
);

__END__

更新:區分可能分配給*DATA*STDIN globs的變量的問題是fileno的作業:

sub data_or_stdin {
  my $x = shift;
  if (fileno($x) == fileno(DATA)) {
    return "DATA";
  } elsif (fileno($x) == fileno(STDIN)) {
    return "STDIN";
  } else {
    return "NEITHER";
  }
}

print "DATA:  ", data_or_stdin(*DATA), "\n";
print "STDIN: ", data_or_stdin(*STDIN), "\n";
open(ZZZ, ">>", "zzz"); close ZZZ;
open(ZZZ, "<", "zzz"); print "ZZZ: ", data_or_stdin(*ZZZ), "\n"; close ZZZ;
open($fh, "<", "zzz"); print "\$fh=ZZZ: ", data_or_stdin($fh), "\n"; close $fh;
$fh = *DATA; print "\$fh=DATA: ", data_or_stdin($fh), "\n";
$fh = *STDIN; print "\$fh=STDIN: ", data_or_stdin($fh), "\n";

__END__
stuff;
$ perl data_or_stdin.pl
DATA:  DATA
STDIN: DATA
ZZZ: NEITHER
$fh=ZZZ: NEITHER
$fh=DATA: DATA
$fh=STDIN: DATA

如果$f是文件句柄,則ref $fref \\$f將為"GLOB"如果$f是標量,則ref \\$f將為"SCALAR"

sub filehandle_or_scalar {
  my $x = shift;
  if (ref $x eq "GLOB" || ref \$x eq "GLOB") {
      return "filehandle";
  } elsif (ref \$x eq "SCALAR") {
      return "scalar";
  } else {
      return "not filehandle or scalar";
  }
}

print "STDIN: ", filehandle_or_scalar(*STDIN), "\n";
print "\$_: ", filehandle_or_scalar($_), "\n";
open($fh, ">", "zzz");
print "\$fh: ", filehandle_or_scalar($fh), "\n";
print "string: ", filehandle_or_scalar('file.txt'), "\n";
print "ref: ", filehandle_or_scalar(\$x), "\n"

###########################################

$ perl filehandle_or_scalar.pl
STDIN: filehandle
$_: scalar
$fh: filehandle
string: scalar
ref: not filehandle or scalar

您可以在stringafied文件句柄上使用模式匹配* STDIN,* DATA等...

if ($f =~ /\bSTDIN$/) {
    return "STDIN";
} elsif ($f =~ /\bDATA$/) {
    return "DATA";
}

哈基,但可能就夠了......

mobrule的方法看起來很有希望:

perl -E 'open $fh, "<", "/dev/null"; say ref $fh;'

將輸出GLOB 但是,也會這樣

perl -E 'say ref \*FOO;'

“真正的”文件句柄也會有一個與之關聯的文件描述符,您可以使用fileno來確定:

perl -MData::Dumper -E 'open $fh, "<", "/dev/null"; say Data::Dumper::Dumper([fileno $fh, fileno \*STDIN, fileno \*FOO])'

將輸出如下內容:

$VAR1 = [
          3,
          0,
          undef
        ];

您可以使用它來告訴正在用於文件I / O的GLOB。 在UNIX系統上,標准輸入流按照約定與文件描述符0相關聯。

想到的另一件事是與文件句柄綁定的類。 這些需要實現一個特定的接口,您可以使用它來測試can 有關此接口的詳細信息請參閱perlfunc中tie VARIABLE,CLASSNAME,LIST條目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM