Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl, warn not working after XML::Smart save

Tags:

stderr

perl

I realised there is a problem with warn after the use of XML::Smart save.

#!/usr/bin/perl

use strict;
use warnings;

use XML::Smart;

my $XML = XML::Smart->new() ;

print STDOUT "Before save: Print to STDOUT works\n";
print STDERR "Before save: Print to STDERR works\n";
warn "Before save: Warn works\n";

$XML->save('newfile.xml') ;

print STDOUT "After save: Print to STDOUT works\n";
print STDERR "After save: Print to STDERR works\n";
warn "After save: Warn does not work\n";

Test is done in OSX 10.8.2 perl version 5.12.4 xml-smart version 1.77

This is probably closely related to the internal workings of XML::Smart but is there a way to restore printing of warn (reset STDERR).

[EDIT 19/3/2013]: In HP-UX constructor is also problematic. Workaround provided below by ikegami can be used for both new and save to overcome the issue.

like image 901
Evangelos Valtos Avatar asked Mar 17 '13 16:03

Evangelos Valtos


1 Answers

As TLP pointed out, the problem is related to XML::Smart's fiddling with $SIG{__WARN__}.

The following is a workaround to the bug:

{
    local $SIG{__WARN__} = $SIG{__WARN__};
    local $SIG{__DIE__}  = $SIG{__DIE__};
    $XML->save('newfile.xml') ;
}

As Borodin explains, this workaround localises the two elements of %SIG that XML::Smart modifies so that the damage is restricted to the enclosing block. The values are automatically restored at the end of the block, immediately after the call to $XML->save.

like image 82
ikegami Avatar answered Oct 20 '22 23:10

ikegami