Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: XML::Twig parsefile_inplace misunderstanding

by mirod (Canon)
on Jul 19, 2013 at 09:26 UTC ( [id://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.

Replies are listed 'Best First'.
Re^2: XML::Twig parsefile_inplace misunderstanding
by Discipulus (Canon) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1045309]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-23 20:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found