File Coverage

lib/XML/Namespace.pm
Criterion Covered Total %
statement 39 39 100.0
branch 3 4 75.0
condition 5 6 83.3
subroutine 10 10 100.0
pod 2 2 100.0
total 59 61 96.7


line stmt bran cond sub pod time code
1             #============================================================= -*-perl-*-
2             #
3             # XML::Namespace
4             #
5             # Simple support for XML Namespaces.
6             #
7             # Written by Andy Wardley
8             #
9             # This is free software; you can redistribute it and/or
10             # modify it under the same terms as Perl itself.
11             #
12             # $Id: Namespace.pm,v 1.2 2005/08/22 14:04:04 abw Exp $
13             #
14             #========================================================================
15              
16             package XML::Namespace;
17              
18 1     1   1991 use base 'Exporter';
  1         2  
  1         104  
19 1     1   4 use strict;
  1         1  
  1         25  
20 1     1   5 use warnings;
  1         5  
  1         67  
21              
22             our $VERSION = 0.02;
23             our $AUTOLOAD;
24             our @EXPORT_OK;
25              
26             use overload
27 1         8 '""' => \&uri,
28 1     1   1734 fallback => 1;
  1         1163  
29              
30              
31             #------------------------------------------------------------------------
32             # import(@symbols)
33             #
34             # Method called by Exporter base class when the module is loaded via
35             # a C statement. Any arguments provided are passed
36             # to the import() method as @symbols. These should be pairs of
37             # (xml_namespace => uri) arguments. The method constructs an XML::Namespace
38             # object for each pair, and a closure subroutine with the same name as
39             # the XML namespace, which simply returns the object. This is then exported
40             # to the caller's package namespace.
41             #------------------------------------------------------------------------
42              
43             sub import {
44 4     4   22145 my $class = shift;
45 4         12 my @symbols = @_;
46 4         7 my (@imports, $planned);
47              
48 4         16 while (@symbols) {
49 1     1   118 no strict 'refs';
  1         2  
  1         394  
50 3         8 my $ns = shift @symbols;
51 3   50     11 my $uri = shift @symbols
52             || die "no URI provided for namespace $ns in 'use $class' statement";
53 3         13 my $obj = $class->new($uri);
54 3     4   16 *{"$class\::$ns"} = sub { return $obj };
  3         22  
  4         28  
55 3         8 push(@imports, $ns);
56 3         14 push(@EXPORT_OK, $ns);
57             }
58 4 100       2365 $class->export_to_level(1, $class, @imports)
59             if @imports
60             }
61              
62              
63             #------------------------------------------------------------------------
64             # new($uri)
65             #
66             # A simple object constructed as a reference to the URI passed as an
67             # argument.
68             #------------------------------------------------------------------------
69              
70             sub new {
71 7     7 1 258 my $class = shift;
72 7   100     26 my $uri = shift
73             || die "no URI parameter provided for $class new() method";
74 6         22 bless \$uri, $class;
75             }
76              
77              
78             #------------------------------------------------------------------------
79             # uri()
80             # uri($path)
81             #
82             # Returns the URI for the namespace object, with an optional path
83             # argument added to the end of it.
84             #------------------------------------------------------------------------
85              
86             sub uri {
87 10     10 1 19 my $self = shift;
88 10   100     40 my $path = shift || '';
89 10         161 return "$$self$path";
90             }
91              
92              
93             #------------------------------------------------------------------------
94             # AUTOLOAD
95             #
96             # Catches all method calls (expect import(), new() and uri(), obviously)
97             # and delegates them to $self->uri() to resolve.
98             #------------------------------------------------------------------------
99              
100             sub AUTOLOAD {
101 4     4   6 my $self = shift;
102 4         7 my $path = $AUTOLOAD;
103 4         16 $path =~ s/^.*:://;
104 4 50       12 return if $path eq 'DESTROY';
105 4         9 return $self->uri($path);
106             }
107              
108              
109              
110             1;
111             __END__