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

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.

Replies are listed 'Best First'.
Re^2: XML::Twig parsefile_inplace misunderstanding
by Discipulus (Prior) 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

    there are no rules, there are no thumbs..


      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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1045309]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2017-01-19 09:02 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (169 votes). Check out past polls.