英文:
want to print the in console as an output
问题
这是我的示例代码,我想将报告打印在控制台上作为输出,我尝试了很多次,但无法做到,我是新学习者。
#!/usr/local/bin/perl
# for循环执行
$b=0;
for( $a = 0; $a < 5; $a = $a + 1 ) {
print "请输入您的姓名:" ;
my $name = <STDIN>;
chomp $name;
open(my $report, '>', 'report.txt');
close $report;
}
英文:
This is my sample code I want to print the report in the console as an output, I tried too many times but I unable to do this, new learner.
#!/usr/local/bin/perl
# for loop execution
$b=0;
for( $a = 0; $a < 5; $a = $a + 1 ) {
print "Enter your name please" ;
my $name = <STDIN>;
chomp $name;
open(my $name, '>', 'report.txt');
close $name;
}
答案1
得分: 2
以下是您要翻译的内容:
my $qfn = 'report.txt';
open(my $fh, '<', $qfn)
or die("无法打开 \"$qfn\": $!\n");
while (<$fh>) {
print;
}
最后三行等同于
while ($_ = <$fh>) {
print($_);
}
所以您也可以使用
while (my $line = <$fh>) {
print($line);
}
英文:
my $qfn = 'report.txt';
open(my $fh, '<', $qfn)
or die("Can't open \"$qfn\": $!\n");
while (<$fh>) {
print;
}
The last three lines are equivalent to
while ($_ = <$fh>) {
print($_);
}
So you could also use
while (my $line = <$fh>) {
print($line);
}
答案2
得分: 1
我认为OP正在要求将控制台输出打印到报告中。
#!/usr/local/bin/perl
# for循环执行
$b = 0; 现在这是不需要的标量变量声明
for ($a = 0; $a < 5; $a = $a + 1) { # 这里可以使用$a++而不是$a = $a + 1;
print "请输入您的姓名:";
my $name = <STDIN>;
chomp $name; # 你将检查文件处理I/O。
open($filename, '>>', 'report.txt'); # 你需要将值追加到报告中
print $filename "$name\n";
close $filename;
}
只需尝试这个。
英文:
I think the OP is asking to print the console output into the report.
#!/usr/local/bin/perl
# for loop execution
$b=0; Now this is unwanted scalar variable declaration
for( $a = 0; $a < 5; $a = $a + 1 ) { # You can use $a++ here instead of $a = $a + 1 ;
print "Enter your name please" ;
my $name = <STDIN>;
chomp $name; #You will check the filehandling I/O.
open($filename, '>>', 'report.txt'); #You need to append the values into the report
print $filename "$name\n";
close $filename;
}
Just try this.
答案3
得分: 1
根据提供的代码,您从标准输入读取并输出到 report.txt 文件中。请注意,您的脚本包含了一个shebang,表示您在使用Linux/UNIX操作系统。
#!/usr/local/bin/perl
use strict;
use warnings;
my $report = 'report.txt';
my $a = 5;
open(my $fh, '>', $report)
or die "Couldn't open $report";
while( $a-- ){
print "Enter your name please: " ;
my $name = <STDIN>;
chomp $name;
say $fh $name;
}
close $fh;
希望对您有所帮助。
英文:
Judging by provided piece of code you read from STDIN
and output to report.txt file
#!/usr/local/bin/perl
use strict;
use warnings;
my $report = 'report.txt';
my $a = 5;
open(my $fh, '>', $report)
or die "Couldn't open $report";
while( $a-- ){
print "Enter your name please: " ;
my $name = <STDIN>;
chomp $name;
say $fh $name;
}
close $fh;
NOTE: your script includes shebang
what indicates that you use Linux/UNIX
答案4
得分: 1
这个回答适用于如果 OP 想要在 Perl 中实现 tee
Linux/UNIX 命令提供的功能。Man 页面 tee。
在使用该脚本之前,应将其设置为可执行文件 chmod +x script.pl
。
该命令将继续将内容打印到终端,并将相同的输出保存到提供的 filename 或默认文件 report.txt。
用法:command | script.pl [filename]
注意:旧文件会被覆盖
#!/usr/local/bin/perl
use strict;
use warnings;
use feature 'say';
my $report = shift || 'report.txt';
open(my $fh, '>', $report)
or die "Couldn't open $report";
while( <> ){
say;
say $fh $_;
}
close $fh;
英文:
This answer applies to the case if OP wants to implement in perl feature provided by tee
Linux/UNIX command. Man page tee.
Prior usage of the script it should be made executable chmod +x script.pl
.
The command will continue to print into the terminal and save same output into file with provided filename or otherwise default file report.txt.
USAGE: command | script.pl [filename]
NOTE: old file gets overwritten
#!/usr/local/bin/perl
use strict;
use warnings;
use feature 'say';
my $report = shift || 'report.txt';
open(my $fh, '>', $report)
or die "Couldn't open $report";
while( <> ){
say;
say $fh $_;
}
close $fh;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论