How to redirect console output to a text file

后端 未结 4 2070
眼角桃花
眼角桃花 2020-12-31 17:15

I am executing a Perl program. Whatever is being printed on my console, I want to redirect that to a text file.

4条回答
  •  盖世英雄少女心
    2020-12-31 17:29

    In the CLI you can use >, like this:

    perl  script_name.pl > path_to_your_file
    

    If you want to do this inside the perl script, add this code before you print anything:

    open(FH, '>', 'path_to_your_file') or die "cannot open file";
    select FH;
    # ...
    # ... everything you print should be redirected to your file
    # ...
    close FH;  # in the end
    

提交回复
热议问题