Redirect output to file perl




















Read on. Before you launch your favourite text editor and start hacking Perl code, you may just need to redirect the program output in the terminal. If a terminal redirect is not specific enough for your needs, you can use one of the following Perl solutions. This makes it easy to globally redirect and restore standard output. The local function creates a lexically-scoped copy of any variable passed to it. This copy can then be restored when required. If you want to read from a file, follow the reading from a file tutorial.

In order to write to a file, first you need to open the file for writing as follows:. Notice that if you write to a file that contains content, Perl will truncate its content.

The following example demonstrates how to read the content of one file and write it another file. Printing the output to a file in perl Ask Question. Asked 7 years, 10 months ago. Active 7 years, 4 months ago. Viewed 4k times. Improve this question. There is no need to declare a main subroutine. You aren't writing in C — Borodin. There is no reason to declare a main and then call it. Borodin okay, I will make that correction — user No, please don't change things as it will confuse the problem.

Show 3 more comments. Active Oldest Votes. 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. Improve this answer.

Borodin Borodin k 9 9 gold badges 67 67 silver badges bronze badges. Quick Links. Search Forums. Show Threads. Show Posts.

Shell Programming and Scripting. Top Forums Shell Programming and Scripting perl redirect output to file.. Registered User. Join Date: Feb Code :. Join Date: May I took this subset of your script and ran it, it writes successfully to ping. Join Date: Jan Its not apparent why nothing is being written to the file, but this looks a little dangerous: Code :. Annihilannic yes it works no problems The problem is that output is buffered, and since you are in an endless while loop, with small amounts of output, it will take a while before the log data is written from buffer to disk.

Test this by moving your close after your print to the logfile.



0コメント

  • 1000 / 1000