Changeset 1870

Show
Ignore:
Timestamp:
07/10/06 14:12:10
Author:
miyagawa
Message:

add globally_unoverride and Destructor object

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • XML-Liberal/trunk/Changes

    r1839 r1870  
    11Revision history for Perl extension XML::Liberal 
     2 
     30.13  Sun Jul  9 22:03:35 JST 2006 
     4        - Added globally_unoverride method and Destructor object to hold in a lecical scope 
     5 
     60.12  2006-06-28 06:50:00 CDT 
     7        - Fixed HTMLEntity handler to handle entities other than just nbsp. 
    28 
    390.11  2006-05-30 20:19:23 JST 
  • XML-Liberal/trunk/MANIFEST

    r1838 r1870  
    1616t/01_bad.t 
    1717t/02_global.t 
     18t/03_global_destroy.t 
    1819t/bad/BAD-ampersand.xml 
    1920t/bad/BAD-atttibute.xml 
     
    5354t/bad/BAD-euc.xml 
    5455t/bad/BAD-html.xml 
     56t/bad/BAD-html2.xml 
    5557t/bad/BAD-lowascii.xml 
    5658t/bad/BAD-undeclared-ns-element.xml 
  • XML-Liberal/trunk/MANIFEST.bak

    r1838 r1870  
    5353t/bad/BAD-euc.xml 
    5454t/bad/BAD-html.xml 
     55t/bad/BAD-html2.xml 
    5556t/bad/BAD-lowascii.xml 
    5657t/bad/BAD-undeclared-ns-element.xml 
  • XML-Liberal/trunk/lib/XML/Liberal.pm

    r1839 r1870  
    22 
    33use strict; 
    4 our $VERSION = '0.11'; 
     4our $VERSION = '0.13'; 
    55 
    66use base qw( Class::Accessor ); 
     
    4444 
    4545    $subclass->globally_override; 
     46 
     47    if (defined wantarray) { 
     48        return XML::Liberal::Destructor->new( 
     49            sub { $subclass->globally_unoverride }, 
     50        ); 
     51    } 
     52 
     53    return; 
    4654} 
    4755 
     
    99107} 
    100108 
     109package XML::Liberal::Destructor; 
     110 
     111sub new { 
     112    my($class, $callback) = @_; 
     113    bless { cb => $callback }, $class; 
     114} 
     115 
     116sub DESTROY { 
     117    my $self = shift; 
     118    $self->{cb}->(); 
     119} 
     120 
     121package XML::Liberal; 
     122 
    1011231; 
    102124__END__ 
     
    118140 
    119141  XML::Liberal->globally_override('LibXML'); 
    120  
    121142  my $parser = XML::LibXML->new; # isa XML::Liberal 
     143 
     144  # revert the global overrides back 
     145  XML::Liberal->globally_unoverride('LibXML'); 
     146 
     147  # override XML::LibXML->new globally in a lexical scope 
     148  { 
     149     my $destructor = XML::LibXML->globally_override('LibXML'); 
     150     my $parser = XML::LibXML->new; # isa XML::Liberal 
     151  } 
     152 
     153  # $destructor goes out of scope and global override doesn't take effect 
     154  my $parser = XML::LibXML->new; # isa XML::LibXML 
    122155 
    123156=head1 DESCRIPTION 
     
    163196  my $feed = XML::Atom::Feed->new(URI->new('http://example.com/atom.xml')); 
    164197 
     198If you want the original XML::LibXML->new back in business, you can 
     199call I<globally_unoverride> method. 
     200 
     201  XML::Liberal->globally_override('LibXML'); 
     202  # ... do something 
     203  XML::Liberal->globally_unoverride('LibXML'); 
     204 
     205Or, you can hold the destructor object in a scalar variable and make 
     206the global override take effect only in a lexical scope: 
     207 
     208  { 
     209    my $destructor = XML::Liberal->globally_override('LibXML'); 
     210    # ... do something 
     211  } 
     212 
     213  # now XML::LibXML::new is back as normal 
     214 
    165215=back 
    166216 
  • XML-Liberal/trunk/lib/XML/Liberal/LibXML.pm

    r1838 r1870  
    1919 
    2020    1; 
     21} 
     22 
     23sub globally_unoverride { 
     24    my $class = shift; 
     25 
     26    no warnings 'redefine'; 
     27    if ($XML_LibXML_new) { 
     28        *XML::LibXML::new = $XML_LibXML_new; 
     29        undef $XML_LibXML_new; 
     30    } 
     31 
     32    return 1; 
    2133} 
    2234 
     
    6476        return $remedy; 
    6577    } 
    66     elsif ($errors[0] =~ /^:(\d+): parser error : Entity 'nbsp' not defined/) { 
     78    elsif ($errors[0] =~ /^:(\d+): parser error : Entity '(.*)' not defined/) { 
    6779        my $line = $1; 
    6880        my $pos = $self->get_pos($errors[2]);