Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: XML::Twig parsefile_inplace misunderstanding

by mirod (Canon)
on Jul 19, 2013 at 09:26 UTC ( #1045309=note: print w/ replies, xml ) Need Help??


in reply to XML::Twig parsefile_inplace misunderstanding

It looks like a bug to me. I am not sure whether it's a bug in the code or in the docs.

The file is replaced when the parse method returns, so any print after this is not sent to the proper filehanlde (the temp file that will then replace the original file). I think it could be done when the twig is destroyed, when it goes out of scope or when the progam exits, but I am a bit worried about causing problems in existing code if I do this.

I usually use parsefile_inplace with flush, this way the data is flushed at the end of the parse. This requires flush to be called during the parse though so if you don't have handlers, your code should look like this (ugly!):

my $twig = XML::Twig->new( pretty_print => 'indented', twig_handlers + =. { 'level(1) => { sub { $_->flush} }); $twig->parsefile_inplace ( $file, $ext) ; # no ->print after this

Alternatively, if you don't need handlers, you could also use perl -i to do this, using parsefile and print.


Comment on Re: XML::Twig parsefile_inplace misunderstanding
Download Code
Re^2: XML::Twig parsefile_inplace misunderstanding
by Discipulus (Curate) on Jul 19, 2013 at 09:51 UTC
    Thanks mirod

    will be a good think update the docs to specify the scope of parsefile_inplace.

    Your useful module have a lot of method available.. maybe you can add a parsefile_inplace_global one to extend the redirection of handlers till the existence of the twig.

    thanks again
    L*


    there are no rules, there are no thumbs..

      lol!

      At the very least I'll update the docs, explaining the exact scope of parsefile_inplace and how to use perl -i. But that's a good suggestion though. I'll see if it can be done. Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1045309]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (13)
As of 2014-09-19 15:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (140 votes), past polls