简体   繁体   English

将输出打印到perl中的文件中

[英]Printing the output to a file in perl

I have been trying to find examples related to file handling, but I haven't found anything that will solve my problem. 我一直在尝试查找与文件处理有关的示例,但没有找到任何可以解决我的问题的方法。

    use strict;
    use warnings;

    my ($m, $b)    = @ARGV;
    my $count_args = $#ARGV + 1;
    my $times      = 2**$m;

    my $output = "main fucntion to be called???\n";

    open(OUTFILE, "> output1.txt") || die "could not open output file";

    print OUTFILE "$output\n"; ## Notice, there is not a comma after the file handle and the string to be output

    close(OUTFILE);

    main();

    sub main {
      if ($m =~ /^\d+$/) {
        if ($b =~ /^and$/i) {
          func_and();
        }
        else {
          print " not supported\n";
        }
      }
      else {
        print " enter valid number of pins\n";
      }
    }

    sub func_and {
      my $bit;
      for (my $i = 0 ; $i < $times ; $i++) {
        my $p = sprintf("%0${m}b", $i);
        print "$p\t";
        my @c = split('', $p);
        my $result = 100;
        foreach $bit (@c) {
          if ($result < 100) {
            $result = $result && $bit;
          }
          else {
            $result = $bit;
          }
        }
        print "result is $result \n";
      }
    }

This program prints an output if I provide the input as 2 and the output is printed on the screen. 如果我将输入设置为2,并且输出显示在屏幕上,则此程序将打印输出。

I want to change the file handle STDOUT of this program that is i want to print the output to output1.txt file 我想更改该程序的文件句柄STDOUT ,即我想将输出打印到output1.txt文件

Can you please point out the mistake I am making? 你能指出我犯的错误吗? -----> ----->

copy STDOUT to another filehandle 将STDOUT复制到另一个文件句柄

open (my $STDOLD, '>&', STDOUT);

# redirect STDOUT to log.txt

open (STDOUT, '>>', "$ARGV[1]".".log");

print main();

# restore STDOUT
open (STDOUT, '>&', $STDOLD);

print " check .log file\n This should show in the terminal\n"; 

This works for me.. but in the end of the log file i have digit "1" printed i don't know why..I believe it is the timestamp being printed. 这对我有用..但是在日志文件的末尾我打印了数字“ 1”,我不知道为什么。.我相信这是正在打印的时间戳。 I need to remove it.. do u know why is it happening? 我需要将其删除。.您知道为什么会这样吗?

It sounds like you want to redirect the output from the program to a file? 听起来您想将程序的输出重定向到文件?

To do that from the command line, enter 要从命令行执行此操作,请输入

perl myprogram.pl > myoutput.txt


If you want everything printed to STDOUT to go to a file then just open it as a file handle at the start of your program. 如果您希望将打印到STDOUT所有内容都STDOUT一个文件,则只需在程序开始时将其作为文件句柄打开即可。

Like this 像这样

open STDOUT, '>', 'myoutput.txt' or die $!;

Any file handle you assign to the typeglob *::STDOUT will become STDOUT. 您分配给typeglob *::STDOUT任何文件句柄都将成为STDOUT。

if ( my $fh = FileHandle->new( ">>$stdout_path" )) {
    *::STDOUT = $fh;
}

Although I normally usually only redirect STDERR to STDOUT or a file. 尽管我通常通常只将STDERR重定向到STDOUT或文件。

I would try the following (avoiding using double quotes plus using a "clear then append" approach: 我将尝试以下操作(避免使用双引号以及“先清除后追加”方法:

:
:
my $output = "main fucntion to be called???\n"; 

open(OUTFILE, '> output1.txt') || die "could not clear down output file"; 
close OUTFILE;
open(OUTFILE, '>> output1.txt') || die "could not open append output file"; 

print OUTFILE "$output\n"; ## Notice, there is not a comma after the file handle and the string to be output 

close OUTFILE;
:
:

No need for braces in the close statement. 在结束声明中不需要大括号。 The first open effectively emptys the file. 第一次打开有效地清空了文件。 The second is an open-extend (or append). 第二个是开放扩展(或附加)。 I assume you will then use print OUTFILE ... throughout and move the second close Output; 我假设您随后将使用print OUTFILE ...并移动第二个close Output; to the end of your script. 到脚本的末尾。

OR... 要么...

:
:
my $output = "main fucntion to be called???\n"; 

open(STDOUT, '> output1.txt') || die "could not clear down output file"; 
close STDOUT;
open(STDOUT, '>> output1.txt') || die "could not open append output file"; 

print "$output\n"; ## Notice, there is not a comma after the file handle and the string to be output 
:
:
close STDOUT;
:
:

Pretty sure this works - without need for STDOUT on every print command. 相当确定这是可行的-无需在每个打印命令上使用STDOUT。

From what I understand, You are trying to send everything you "print" to a file. 据我了解,您正在尝试将“打印”的所有内容发送到文件。 If that is what you want to do, it is fairly simple. 如果那是您想要做的,那将非常简单。

open (OUTPUT, '>output1.txt');
#whatever logic you want to do
print OUTPUT "whatever you want to print","temporary outputs";
#any other logic you have for computing the output
print OUTPUT "output1","output2";
#after completing all print statements, close the file handle
close(OUTPUT);

Basically, you need to give your File Handle (in my example OUTPUT) to "print" and then print like you would do normally. 基本上,您需要给文件句柄(在我的示例中为OUTPUT)进行“打印”,然后像平常一样进行打印。 For example, if your original line is 例如,如果您的原始行是

print "not supported\n";

It will become 它将成为

print OUTPUT "not supported\n";

You can use select to set the default filehandle for print: 您可以使用select设置默认的打印文件句柄:

open(my $OUTFILE, '>', 'output1.txt') || die "could not open output file: $!";
select $OUTFILE;
print $output;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM