在Windows中,只有文件的句柄,是否有一种简单的方法来获取文件的完整路径?
我不能使用GetFinalPathNameByHandle(),因为这只是Vista +,我们的产品必须在XP上运行.然而,简单或接近它的东西最好.
请先查看以下代码.
#! /usr/bin/perl
package foo;
sub new {
my $pkg = shift;
my $self = {};
my $self->{_fd} = undef;
bless $self, $pkg;
return $self;
}
sub Setfd {
my $self = shift;
my $fd = shift;
$self_->{_fd} = $fd;
}
sub write {
my $self = shift;
print $self->{_fd} "hello word";
}
my $foo = new foo;
Run Code Online (Sandbox Code Playgroud)
我的目的是使用hash在类中存储文件句柄.文件句柄最初是未定义的,但之后可以通过调用Setfd函数来启动.然后可以调用write来实际将字符串"hello word"写入文件句柄指示的文件,假设文件句柄是成功"写入"打开的结果.
但是,perl编译器只是抱怨"print"行中存在语法错误.谁能告诉我这里有什么问题?提前致谢.
在perldoc perlvar,我读到这个:
请注意,目前"ARGV"仅在"<>"运算符中具有神奇效果; 在其他地方,它只是一个普通的文件句柄,对应于"<>"打开的最后一个文件.特别是,将"*ARGV"作为参数传递给需要文件句柄的函数可能不会导致您的函数自动读取@ARGV中所有文件的内容.
那么,如何可以我通过*ARGV(或一些类似于它)作为一个参数设置为期望一个文件句柄的功能,并有功能读取所有的文件@ARGV?
我试图找出如何让Perl模块顺从并打开文件句柄的引用.当你看到主程序时,你会理解我的意思:
#!/usr/bin/perl
use strict;
use warnings;
use lib '/usr/local/share/custom_pm';
use Read_FQ;
# open the STDIN filehandle and make a copy of it for (safe handling)
open(FILECOPY, "<&STDIN") or die "Couldn't duplicate STDIN: $!";
# filehandle ref
my $FH_ref = \*FILECOPY;
# pass a reference of the filehandle copy to the module's subroutine
# the value the perl module returns gets stored in $value
my $value = {Read_FQ::read_fq($FH_ref)};
# do something with $value
Run Code Online (Sandbox Code Playgroud)
基本上,我希望主程序通过STDIN接收输入,复制STDIN文件句柄(用于安全处理),然后将对该副本的引用传递给Read_FQ.pm文件(perl模块)中的read_fq()子例程.然后,子例程将从该文件句柄读取输入,处理它并返回一个值.这里是Read_FQ.pm文件:
package Read_FQ;
sub read_fq{
my ($filehandle) = …Run Code Online (Sandbox Code Playgroud) 在下面的小代码中,我没有收到行 [09] 和 [18] 的错误或警告。我得到的唯一警告是第 [21] 行:
use strict; # [01]
use warnings FATAL => 'unopened'; # [02]
# [03]
open(my $outHandleA, ">outputA.txt") or die ("A: $!\n"); # [04] Opened $outHandleA
print $outHandleA "FILE A\n"; # [05]
close $outHandleA; # [06] Closed $outHandleA
# [07]
print $outHandleA; # [08]
print $outHandleA "ABC\n"; # [09] <---
print $outHandleA; # [10]
print "-----"; # [11]
# [12]
open(OUT, ">outputB.txt") or die ("B: $!\n"); # [13] Opened OUT
print OUT "FILE B\n"; # …Run Code Online (Sandbox Code Playgroud) 我正在一个用户可以传递-o file选项的程序中工作,然后应该将输出定向到该文件.否则,它应该去stdout.
要检索选项,我正在使用模块getopt long,这不是问题.问题是我想用该文件创建一个文件句柄,或者如果未设置该选项则为其分配标准输出.
if ($opt) {
open OUTPUT, ">", $file;
} else {
open OUTPUT, # ???
}
Run Code Online (Sandbox Code Playgroud)
那是因为这样,以后在我的代码中我可以:
print OUTPUT "...";
Run Code Online (Sandbox Code Playgroud)
不用担心OUTPUT是stdout还是用户指定的文件.这可能吗?如果我在这里设计不好,请告诉我.
我从STDIN读了几行.如何将剩余的STDIN传递给从标准输入读取的命令(例如md5sum或wc)?
我可以做一个:
read_a_few_lines_from_diamond_operator();
open (C, "|cmd");
while(<>) { print C }
close C;
cleanup_after_C();
Run Code Online (Sandbox Code Playgroud)
但出于效率原因,我不想触摸输入,而是传递STDIN的文件句柄.有点像:
seq 10 | (read A; wc)
Run Code Online (Sandbox Code Playgroud)
在read将其余部分传递给之前,尽可能多地读取wc.我不能使用这个解决方案,因为我需要从我的perl程序中启动命令,我需要在cmd完成后完成工作.
我从'foo'文件中读了几行.如何将剩余部分传递给从标准输入读取的命令(例如md5sum或wc)?
我可以做一个:
open (F, "<foo");
read_a_few_lines_from_F();
open (C, "|cmd");
while(<F>) { print C }
close C;
cleanup_after_C();
Run Code Online (Sandbox Code Playgroud)
但出于效率原因,我不想触摸输入,而是传递文件'foo'的其余部分.
我有一种感觉,它可以使用欺骗等做select,open(FOO,">&STDOUT),exec 6<&0,fork,pipe.
在Perl 5中,我可以在字符串上打开文件句柄,如下所示:
open my $kfh, "<", \$message->payload;
Run Code Online (Sandbox Code Playgroud)
我有一个场景,使用字符串作为文件句柄并将其传递给open方法:
my $fh = new IO::Zlib;
open my $kfh, "<", \$message->payload;
if($fh->open($kfh, 'rb')){
print <$fh>;
$fh->close;
}
Run Code Online (Sandbox Code Playgroud)
其中$message->payload从读取卡夫卡,并且内容是一个字节数组.raiph有一个类似的问题,但它没有回答我的问题.
所以我想知道如何在Perl 6中的字符串上打开文件句柄,就像Perl 5一样?这些文档页面没有关于此的信息:
我有一个包含三列显示学生姓名、科目和成绩的grades.tsv 文件:
Liam Mathematics 5
Liam History 6
Liam Geography 8
Liam English 8
Aria Mathematics 8
Aria History 7
Aria Geography 6
Isabella Mathematics 9
Isabella History 4
Isabella Geography 7
Isabella English 5
Isabella Music 8
Run Code Online (Sandbox Code Playgroud)
我想计算每个学生的平均成绩并将其添加到单独的列中。为此,我使用了两个文件句柄 DATA 和 OUT 打开同一个文件:
use strict;
use warnings;
# Open file with grades for calculation of average grade for each student
open (DATA,"grades.tsv") or die "Cannot open file\n";
my %grade_sums;
my %num_of_subjects;
# Calculate sum of grades and number of subjects …Run Code Online (Sandbox Code Playgroud) 我正在考虑做
open(*STDIN, "<", "/dev/null" );
open(my $fh, "-|", "/bin/bash", "/tmp/foo");
print for <$fh>;'
Run Code Online (Sandbox Code Playgroud)
但是,我想*STDIN恢复,之后,所以我尝试了。
{
open(local *STDIN, "<", "/dev/null" );
open(my $fh, "-|", "/bin/bash", "/tmp/foo");
print for <$fh>;
}
Run Code Online (Sandbox Code Playgroud)
您可以尝试cat使用和不使用local这样的关键字,
{
# remove local and it works,
open(local *STDIN, "<", "/dev/null" );
open(my $fh, "-|", "/bin/cat");
print for <$fh>;
}
Run Code Online (Sandbox Code Playgroud)
只有没有 local才会cat从/dev/null. 那么local在一个裸字文件句柄上实际上做了什么呢?