|Just another Perl shrine|
Re: Sending output to an xml fileby pjf (Curate)
|on Sep 28, 2005 at 07:25 UTC||Need Help??|
Under both Windows and Unix-flavoured you can redirect the output of any command to a file using the > redirection operator, like this:
perl myprogram.pl > myfile.xml
If you wish to have Perl create and print to a file without using a shell redirect, you can first open a file:
Once your file is open, you can just use regular print to direct output to it. Note that the filehandle. Note that there is no comma in between the filehandle (which we've placed in $out_fh) and the data to be printed. This is known as an indirect argument.
The above code is pretty much the same for opening and writing to any file.
If your program is already large, and you don't wish to modify all your existing print statements, then you can re-open STDOUT to a file:
You should be very careful in redirecting STDOUT. Any future maintainer (that's you in six months time) may not expect this to occur, and have a much harder time debugging as a result.
All the very best,
Perl Training Australia