在 Perl 中寫入文件的問題 (Issues writing to a file in Perl)


問題描述

在 Perl 中寫入文件的問題 (Issues writing to a file in Perl)

I am running this script on solaris.  I am trying to write the real, user, and system times to a file, each on a separate line.  However, this code instead writes them out together on the same line with a bunch of unknown character boxes.  How can I get each of the times on their own separate line?

#!/usr/local/bin/perl ‑w

use strict;
use warnings;

my $command = "time echo 'hi' 2>&1 | tee ‑a runtimes.log";
system ($command);

‑‑‑‑‑

參考解法

方法 1:

Solaris standard line delimiter is \n, so your code seems right. Look also here.

Possibly your problem has to do with the text editor you are using to view the file...

方法 2:

Two things:

  • Check the output of both your open and system commands.
  • Print an extra blank line before you close the file.

Try this:

#!/usr/bin/env perl

use strict;
use warnings;   #No need for ‑w when using this

my $command = "time ls 2>&1 | tee ‑a runtimes.log";
open (FILE, '>>runtimes.log') 
    or die qq(Can't open "runtimes.log for writing\n);

my $error = system ($command);
if ($error) {
    die qq(Error in executing command "$command": $?\n);
}
print "\n";
close FILE;
exit 0;

方法 3:

You'll probably get better results as well as more maintainable code if you use perl code (either times or POSIX::times) for getting the system/user times.  

$command="ls ‑lR";
@before{real,user,system,chuser,chsystem}=(time(),times());
system $command;
@after{real,user,system,chuser,chsystem}=(time(),times());
print "real @{[ $after{real}‑$before{real} ]}\n";
print "user @{[ $after{chuser}‑$before{chuser} ]}\n";
print "sys  @{[ $after{chsystem}‑$before{chsystem} ]}\n";

or the POSIX version:

use POSIX qw(times sysconf);
$clocks_per_second=POSIX::sysconf( &POSIX::_SC_CLK_TCK );
$command="ls ‑lR";
@before{real,user,system,chuser,chsystem}=(POSIX::times());
system $command;
@after{real,user,system,chuser,chsystem}=(POSIX::times());
printf "real  %2.2f\n", ($after{real}‑$before{real})/$clocks_per_second;
printf "user  %2.2f\n", ($after{chuser}‑$before{chuser})/$clocks_per_second;
printf "sys   %2.2f\n", ($after{chsystem}‑$before{chsystem})/$clocks_per_second;

(by Stephen DsergioDavid W.evil otto)

參考文件

  1. Issues writing to a file in Perl (CC BY‑SA 3.0/4.0)

#newline #perl #solaris #file-io






相關問題

如何格式化電子郵件中的字符串以便 Outlook 打印換行符? (How do I format a String in an email so Outlook will print the line breaks?)

contentEditable поле для захавання новых радкоў пры ўваходзе ў базу дадзеных (contentEditable field to maintain newlines upon database entry)

換行符 (newline character(s))

grep 如何匹配一些字母或行尾 (grep how to match some letters OR end of line)

Trong Python, có thể thoát các ký tự dòng mới khi in một chuỗi không? (In Python, is it possible to escape newline characters when printing a string?)

在 SWIFT 中遇到逗號時將字符串換行 (Break string to newline when meeting a comma in SWIFT)

行尾的'^ M'字符 ('^M' character at end of lines)

從C#中的字符串末尾刪除回車符和換行符 (Removing carriage return and new-line from the end of a string in c#)

新線路和瀏覽器/操作系統兼容性 (New lines and browser/OS compatability)

如何使用 grep 查找單詞後兩個字符之間的所有內容,而不輸出整行? (How do I find everything between two characters after a word using grep, without outputting the entire line?)

在 Perl 中寫入文件的問題 (Issues writing to a file in Perl)

在 for 循環中每 7 行換行一次 (Newline every 7 lines within a for loop)







留言討論