例如:
open (PS , " tail -n 1 $file | grep win " );
Run Code Online (Sandbox Code Playgroud)
我想查找文件句柄是否为空.
在Windows服务器上使用perl 5.8.8我正在编写一个perl cgi脚本,使用Archive :: Zip来创建一个必须由用户下载的zip:这方面没有问题.zip在内存中管理,没有物理文件使用临时文件或其他任何内容写入磁盘.我想知道如何允许zip下载将流写入浏览器.我做的是这样的:
binmode (STDOUT);
$zip->writeToFileHandle(*STDOUT, 0);
Run Code Online (Sandbox Code Playgroud)
但我觉得这种方式不安全,将STDOUT作为文件句柄.它是否正确且稳健?有一个更好的方法?
非常感谢您的建议
而不是我习惯的典型文件句柄:
open INPUT, $input;
while ($line = <INPUT>) {
....
}
close INPUT;
Run Code Online (Sandbox Code Playgroud)
如何检索文件中指向行的指针,以便我可以在wiil处推进这些指针?我正在尝试创建指向其相应排序文件的两个指针,以便我可以根据一个文件中的行是否比另一个文件中的行"更少"或"更大"来推进指针.
注意:假设输入文件是BIG.
我想比较两个文件,所以我写了下面的代码:
while($line1 = <FH1>){
while($line2 = <FH2>){
next if $line1 > $line2;
last if $line1 < $line2;
}
next;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,当外部循环到达file1的下一行然后进入内部循环时,内部while语句将再次从file2的第一行读取,或者继续在上一次外部迭代中停止的位置环?
谢谢
我想将一个打开的文件句柄传递给一个方法,以便该方法可以写入该文件.
但是文件句柄似乎在对象内部关闭.
# open the file
open(MYOUTFILE, ">$dir_1/stories.txt") or die "Can't open file stories.txt"; #open for write, overwrite
print MYOUTFILE "outside"; #works
my $story = ObjectStory->new();
$story->appendToFile(\*MYOUTFILE);
close(MYOUTFILE);
Run Code Online (Sandbox Code Playgroud)
对象,应写入文件:
package ObjectStory;
# constructor
sub ObjectStory::new {
my ($class) = @_;
%hash = ();
bless \%hash, $class;
}
# addToFile method
sub ObjectStory::appendToFile {
my ($class, $MYOUTFILE) = @_;
# check if open
if (tell($MYOUTFILE) != -1) {
print $MYOUTFILE
"\n File Handle is not open!"; # File handle is always …Run Code Online (Sandbox Code Playgroud) 我想使用isinstance内置函数来判断的类型open(file)。
怎么做?
谢谢!:D
文件中有1910行,但是当我尝试打印行数时,我的值为0,为什么?已经打开了文件句柄,仅当我在count变量获得正确值后再次打开文件句柄时,这为什么
fhandle=open('C:\\Users\\Gopi\\Documents\\Exercise Files\\mbox-short.txt','r')
for i in fhandle:
print(i)
#counting lines in a file
count=0
#fhandle=open('C:\\Users\\Gopi\\Documents\\Exercise Files\\mbox-short.txt','r')
for j in fhandle:
count=count+1
print('Number of lines in the file is',count)
Run Code Online (Sandbox Code Playgroud)
实际结果0预期结果1910
我有一个哈希数组,我按以下方式填写:
# Array of hashes, for the files, regexps and more.
my @AoH;
push @AoH, { root => "msgFile", file => my $msgFile, filefh => my $msgFilefh, cleanregexp => s/.+Msg:/Msg:/g, storeregexp => '^Msg:' };
Run Code Online (Sandbox Code Playgroud)
这是其中一个条目,我有更多这样的。并且一直使用散列的每个键值对来创建文件、清理文本文件中的行等等。问题是,我通过以下方式创建了文件:
# Creating folder for containing module files.
my $modulesdir = "$dir/temp";
# Creating and opening files by module.
for my $i ( 0 .. $#AoH )
{
# Generating the name of the file, and storing it in hash.
$AoH[$i]{file} = "$modulesdir/$AoH[$i]{root}.csv";
# Creating and …Run Code Online (Sandbox Code Playgroud) 请参阅插入结尾处的更新
我需要从网站获得一些缩略图,但我尝试使用wget - 但这对我不起作用,因为我需要一些渲染功能needet:我有一个2,500个URL的列表,每行一个,保存在文件中.然后我想要一个脚本 - 见下面 - 打开文件,读取一行,然后检索网站并将图像保存为一个小缩略图.因为我有一堆网站(2500)我必须决定结果的命名.
http://www.unifr.ch/sfm
http://www.zug.phz.ch
http://www.schwyz.phz.ch
http://www.luzern.phz.ch
http://www.schwyz.phz.ch
http://www.phvs.ch
http://www.phtg.ch
http://www.phsg.ch
http://www.phsh.ch
http://www.phr.ch
http://www.hepfr.ch/
http://www.phbern.ch
Run Code Online (Sandbox Code Playgroud)
到目前为止这么好,我想我尝试这样的事情
#!/usr/bin/perl
use strict;
use warnings;
use WWW::Mechanize::Firefox;
my $mech = new WWW::Mechanize::Firefox();
open my $urls, '<', 'urls.txt' or die $!;
while (<$urls>) {
chomp;
next unless /^http/i;
print "$_\n";
$mech->get($_);
my $png = $mech->content_as_png;
my $name = $_;
$name =~ s#^http://##i;
$name =~ s#/##g;
$name =~ s/\s+\z//;
$name =~ s/\A\s+//;
$name =~ s/^www\.//;
$name …Run Code Online (Sandbox Code Playgroud) filehandle ×10
perl ×7
python ×2
isinstance ×1
javascript ×1
loops ×1
python-2.7 ×1
python-3.x ×1
while-loop ×1