Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Sending output to an xml file

by pjf (Curate)
on Sep 28, 2005 at 07:25 UTC ( #495657=note: print w/replies, xml ) Need Help??

in reply to Sending output to an xml file

G'day Tito1981,

Under both Windows and Unix-flavoured you can redirect the output of any command to a file using the > redirection operator, like this:

perl > 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:

my $out_fh; open($out_fh, ">", "myfile.xml") or die "Cannot open myfile.xml - $!";

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.

print {$out_fh} "<greeting>Hello World!</greeting>\n";

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:

open(STDOUT,">","myfile.xml") or die "Cannot redirect STDOUT to myfile +.xml - $!";

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,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://495657]
[Discipulus]: practical use for?
[marto]: finding bugs, either in the CPU layer or hypervisor, finding undocumented instructions
[zentara]: are you talikng about perf?
[Discipulus]: i miss what an undocumented instructions can be, and how can be used

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2017-07-28 12:00 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (428 votes). Check out past polls.