English 中文(简体)
• 如何在Perl关闭并重新开放STDOUT?
原标题:How can close and reopen STDOUT in Perl?

我愿结束科技促发委的工作,以防止我守则产生我需要进一步计算,但我不想在网页上进行计算的具体形象。

因此,我想结束科技促发委的工作,我必须做些什么,然后将科技促发委重新投入到网页上。 <>strong>(无)

我尝试的是:

    close STDOUT;
    # my code here
    open STDOUT;

......

增 编

最佳回答
问题回答

由于很多人认为STDOUT总是开放,因此它不难结束。 最好将其转至<代码>dev/null(unix)或nul(Windows)。

如果你想要改写,

use Sub::ScopeFinalizer qw( scope_finalizer );

{
    open(my $backup_fh,  >& , *STDOUT) or die $!;
    my $guard = scope_finalizer { open(STDOUT,  >& , $backup_fh) or die $!; };
    open(STDOUT,  > ,  /dev/null ) or die $!;

    ...
}

如果你只是想把科技创新的方向转向,

{
    local *STDOUT;
    open(STDOUT,  > ,  /dev/null ) or die $!;

    ...
}

如果你只是想调整拖欠的产出处理,

use Sub::ScopeFinalizer qw( scope_finalizer );

{
    open(my $null_fh,  > ,  /dev/null ) or die $!;
    my $backup_fh = select($null_fh);
    my $guard = scope_finalizer { select($backup_fh); };

    ...
}

你们可以做些什么,以达到科技创新目标:

sub stdout_of (&) {
    my $code = shift;

    local *STDOUT;
    open STDOUT,  > , (my $stdout_string =   )
        or die "reopen STDOUT: $!";

    $code->();

    return $stdout_string;
}

然后使用:

my $stdout = stdout_of { print "hello world" };

将文件存放在(a)版面,使你能够避免关闭和重新开放STDOUT的陷阱。

开放

寻找“他是一种用各种方法挽救、转口和恢复性传染病和性传染病的文字”。

你们想要做的是不是结束性病,而是将其转用于临时的疾病。

(re) 开放STDOUT或STDERR,作为中间文件,首先关闭:

 close STDOUT;
    open STDOUT,  > , $variable or die "Can t open STDOUT: $!";

From the perl doc: http://perldoc.perl.org/functions/open.html You have a : after your close, don t do that. The open above should also work with jus

open STDOUT;

http://www.perlmonks.org/?node_id=635010“rel=“nofollow”http://www.perlmonks.org/?node_id=635010

我检查了两种方式:

  1. via select
  2. via *OLD_STDOUT = * STDOUT, and see they are not usable in common case.

The reason is these 2 approachs redirect STDOUT only if "print" or something else is used in a Perl Script. But if you use "system()" call or call subscript, their output got to standard STDOUT anyway =((.

My point of view, the indeed solution is to be:

#!/usr/bin/perl -w
my $file1 = "/tmp/out.txt";
my $file2 = "/tmp/err.txt";
open my $oldSTDOUT, ">&STDOUT";
open OLDERR, ">&",*STDERR; 
open(STDOUT, ">$file1")  or print("Can t redirect stdout: to $file1 ");
open(STDERR, ">$file2")  or print("Can t redirect stderr: to $file2 ");
print "THIS OUTPUT ISN T GOT TO STANDARD OUTPUT
";
system("pwd"); # this output isn;t got to standard output too, that is right!
close(STDOUT);
close(STDERR);
open STDOUT, ">>&", $oldSTDOUT;
open STDERR, ">>&OLDERR"; 
print "BUT THIS OUTPUT IS SEEN IN A STANDARD OUTPUT
";

我检查了这一解决办法,并为我工作。





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签