Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Sending output to an xml file

by tito1981 (Initiate)
on Sep 28, 2005 at 06:56 UTC ( [id://495651]=perlquestion: print w/replies, xml ) Need Help??

tito1981 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, Iam very new to Perl. I need some help for a small script that i have written. What I want is to send the output of the perl script to a xml file. Currently the output is displayed on the command prompt window. Just a brief idea abt the script. It is a script which generates contents of a xml file recursively. basically prints the output onto the Command prompt window. Now what i want is to send the output to be written to a xml file. Any help will be very appreciated Regards Tito1981

Replies are listed 'Best First'.
Re: Sending output to an xml file
by pjf (Curate) on Sep 28, 2005 at 07:25 UTC

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

    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,

Re: Sending output to an xml file
by Corion (Patriarch) on Sep 28, 2005 at 07:13 UTC

    There are three approaches to this problem:

    • The invocation hack - you take the console output and redirect it into a file:
      # Instead of >perl -w myscript.pl -some -command -line # you invoke it as >perl -w myscript.pl -some -command -line >output.xml
    • The ugly hack - you take the old console output and stuff it into a new file:
      BEGIN { my $outfile = 'output.xml'; close STDOUT; open STDOUT, ">", $outfile or die "Couldn't create '$outfile': $!"; };
    • The proper implementation: You rewrite all the code that generates output to either take a filehandle as parameter or to return a string. Usually, just returning a string is far nicer (but a bit slower), because it makes for easy, convenient testing:
      # Instead of sub frobnitz { my ($input) = @_; if ($input =~ /foo/) { $input =~ s!foo!frobnitz!g; print $input; }; }; # write: sub frobnitz { my ($input) = @_; if ($input =~ /foo/) { $input =~ s!foo!frobnitz!g; return "<foo>$input</foo>" } else { return $input; }; }; # ... print frobnitz($in);
      Sorry, but I must nit-pick. What you refer to as the "invocation hack" is really not a "hack" at all. Output redirection is a perfectly normal, reasonable and "proper" way of storing output to a file.

      It has the advantage of keeping things simple in the perl script, while also keeping the script very flexible (e.g. output can be piped to some other process, in addition to being redirected to a file). No worries about hard-coding an output file name, and no need to handle an extra element in @ARGV or whatever.

      On many systems, if there is an "open failed for output" type of error, redirection will trap that before the script is even read, let alone waiting for the perl interpreter to load. (That is, given a command line with piping or redirection, a proper shell will open the file or process at the end of the command first, and stop with an error report if that fails.)

      On the whole, for scripts whose purpose is to generate any sort of output data stream, printing to STDOUT within the script and using redirection as needed can usually be considered the preferred approach.

Re: Sending output to an xml file
by ambrus (Abbot) on Sep 28, 2005 at 21:37 UTC

    If you are using the XML::Twig module, you have to open the file to writing, and pass the filehandle as an argument to the flush or flush_up_to method of XML::Twig.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://495651]
Approved by monkfan
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-24 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found