ÿØÿà JFIF    ÿÛ „ ( %!1!%*+...983,7(-.- usr/local/share/perl5/XML/Simple.pm000044400000305420152347135100013032 0ustar00package XML::Simple; $XML::Simple::VERSION = '2.25'; =head1 NAME XML::Simple - An API for simple XML files =head1 SYNOPSIS PLEASE DO NOT USE THIS MODULE IN NEW CODE. If you ignore this warning and use it anyway, the C mode will save you a little pain. use XML::Simple qw(:strict); my $ref = XMLin([] [, ]); my $xml = XMLout($hashref [, ]); Or the object oriented way: require XML::Simple qw(:strict); my $xs = XML::Simple->new([]); my $ref = $xs->XMLin([] [, ]); my $xml = $xs->XMLout($hashref [, ]); (or see L<"SAX SUPPORT"> for 'the SAX way'). Note, in these examples, the square brackets are used to denote optional items not to imply items should be supplied in arrayrefs. =cut # See after __END__ for more POD documentation # Load essentials here, other modules loaded on demand later use strict; use warnings; use warnings::register; use Carp; use Scalar::Util qw(); require Exporter; ############################################################################## # Define some constants # use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $PREFERRED_PARSER); @ISA = qw(Exporter); @EXPORT = qw(XMLin XMLout); @EXPORT_OK = qw(xml_in xml_out); my %StrictMode = (); my @KnownOptIn = qw(keyattr keeproot forcecontent contentkey noattr searchpath forcearray cache suppressempty parseropts grouptags nsexpand datahandler varattr variables normalisespace normalizespace valueattr strictmode); my @KnownOptOut = qw(keyattr keeproot contentkey noattr rootname xmldecl outputfile noescape suppressempty grouptags nsexpand handler noindent attrindent nosort valueattr numericescape strictmode); my @DefKeyAttr = qw(name key id); my $DefRootName = qq(opt); my $DefContentKey = qq(content); my $DefXmlDecl = qq(); my $xmlns_ns = 'http://www.w3.org/2000/xmlns/'; my $bad_def_ns_jcn = '{' . $xmlns_ns . '}'; # LibXML::SAX workaround ############################################################################## # Globals for use by caching routines # my %MemShareCache = (); my %MemCopyCache = (); ############################################################################## # Wrapper for Exporter - handles ':strict' # sub import { # Handle the :strict tag my($calling_package) = caller(); _strict_mode_for_caller(1) if grep(/^:strict$/, @_); # Pass everything else to Exporter.pm @_ = grep(!/^:strict$/, @_); goto &Exporter::import; } ############################################################################## # Constructor for optional object interface. # sub new { my $class = shift; if(@_ % 2) { croak "Default options must be name=>value pairs (odd number supplied)"; } my %known_opt; @known_opt{@KnownOptIn, @KnownOptOut} = (); my %raw_opt = @_; $raw_opt{strictmode} = _strict_mode_for_caller() unless exists $raw_opt{strictmode}; my %def_opt; while(my($key, $val) = each %raw_opt) { my $lkey = lc($key); $lkey =~ s/_//g; croak "Unrecognised option: $key" unless(exists($known_opt{$lkey})); $def_opt{$lkey} = $val; } my $self = { def_opt => \%def_opt }; return(bless($self, $class)); } ############################################################################## # Sub: _strict_mode_for_caller() # # Gets or sets the XML::Simple :strict mode flag for the calling namespace. # Walks back through call stack to find the calling namespace and sets the # :strict mode flag for that namespace if an argument was supplied and returns # the flag value if not. # sub _strict_mode_for_caller { my $set_mode = @_; my $frame = 1; while(my($package) = caller($frame++)) { next if $package eq 'XML::Simple'; $StrictMode{$package} = 1 if $set_mode; return $StrictMode{$package}; } return(0); } ############################################################################## # Sub: _get_object() # # Helper routine called from XMLin() and XMLout() to create an object if none # was provided. Note, this routine does mess with the caller's @_ array. # sub _get_object { my $self; if($_[0] and UNIVERSAL::isa($_[0], 'XML::Simple')) { $self = shift; } else { $self = XML::Simple->new(); } return $self; } ############################################################################## # Sub/Method: XMLin() # # Exported routine for slurping XML into a hashref - see pod for info. # # May be called as object method or as a plain function. # # Expects one arg for the source XML, optionally followed by a number of # name => value option pairs. # sub XMLin { my $self = &_get_object; # note, @_ is passed implicitly my $target = shift; # Work out whether to parse a string, a file or a filehandle if(not defined $target) { return $self->parse_file(undef, @_); } elsif($target eq '-') { local($/) = undef; $target = ; return $self->parse_string(\$target, @_); } elsif(my $type = ref($target)) { if($type eq 'SCALAR') { return $self->parse_string($target, @_); } else { return $self->parse_fh($target, @_); } } elsif($target =~ m{<.*?>}s) { return $self->parse_string(\$target, @_); } else { return $self->parse_file($target, @_); } } ############################################################################## # Sub/Method: parse_file() # # Same as XMLin, but only parses from a named file. # sub parse_file { my $self = &_get_object; # note, @_ is passed implicitly my $filename = shift; $self->handle_options('in', @_); $filename = $self->default_config_file if not defined $filename; $filename = $self->find_xml_file($filename, @{$self->{opt}->{searchpath}}); # Check cache for previous parse if($self->{opt}->{cache}) { foreach my $scheme (@{$self->{opt}->{cache}}) { my $method = 'cache_read_' . $scheme; my $opt = $self->$method($filename); return($opt) if($opt); } } my $ref = $self->build_simple_tree($filename, undef); if($self->{opt}->{cache}) { my $method = 'cache_write_' . $self->{opt}->{cache}->[0]; $self->$method($ref, $filename); } return $ref; } ############################################################################## # Sub/Method: parse_fh() # # Same as XMLin, but only parses from a filehandle. # sub parse_fh { my $self = &_get_object; # note, @_ is passed implicitly my $fh = shift; croak "Can't use " . (defined $fh ? qq{string ("$fh")} : 'undef') . " as a filehandle" unless ref $fh; $self->handle_options('in', @_); return $self->build_simple_tree(undef, $fh); } ############################################################################## # Sub/Method: parse_string() # # Same as XMLin, but only parses from a string or a reference to a string. # sub parse_string { my $self = &_get_object; # note, @_ is passed implicitly my $string = shift; $self->handle_options('in', @_); return $self->build_simple_tree(undef, ref $string ? $string : \$string); } ############################################################################## # Method: default_config_file() # # Returns the name of the XML file to parse if no filename (or XML string) # was provided. # sub default_config_file { my $self = shift; require File::Basename; my($basename, $script_dir, $ext) = File::Basename::fileparse($0, '\.[^\.]+'); # Add script directory to searchpath if($script_dir) { unshift(@{$self->{opt}->{searchpath}}, $script_dir); } return $basename . '.xml'; } ############################################################################## # Method: build_simple_tree() # # Builds a 'tree' data structure as provided by XML::Parser and then # 'simplifies' it as specified by the various options in effect. # sub build_simple_tree { my $self = shift; my $tree = eval { $self->build_tree(@_); }; Carp::croak("$@XML::Simple called") if $@; return $self->{opt}->{keeproot} ? $self->collapse({}, @$tree) : $self->collapse(@{$tree->[1]}); } ############################################################################## # Method: build_tree() # # This routine will be called if there is no suitable pre-parsed tree in a # cache. It parses the XML and returns an XML::Parser 'Tree' style data # structure (summarised in the comments for the collapse() routine below). # # XML::Simple requires the services of another module that knows how to parse # XML. If XML::SAX is installed, the default SAX parser will be used, # otherwise XML::Parser will be used. # # This routine expects to be passed a filename as argument 1 or a 'string' as # argument 2. The 'string' might be a string of XML (passed by reference to # save memory) or it might be a reference to an IO::Handle. (This # non-intuitive mess results in part from the way XML::Parser works but that's # really no excuse). # sub build_tree { my $self = shift; my $filename = shift; my $string = shift; my $preferred_parser = $PREFERRED_PARSER; unless(defined($preferred_parser)) { $preferred_parser = $ENV{XML_SIMPLE_PREFERRED_PARSER} || ''; } if($preferred_parser eq 'XML::Parser') { return($self->build_tree_xml_parser($filename, $string)); } eval { require XML::SAX; }; # We didn't need it until now if($@) { # No XML::SAX - fall back to XML::Parser if($preferred_parser) { # unless a SAX parser was expressly requested croak "XMLin() could not load XML::SAX"; } return($self->build_tree_xml_parser($filename, $string)); } $XML::SAX::ParserPackage = $preferred_parser if($preferred_parser); my $sp = XML::SAX::ParserFactory->parser(Handler => $self); $self->{nocollapse} = 1; my($tree); if($filename) { $tree = $sp->parse_uri($filename); } else { if(ref($string) && ref($string) ne 'SCALAR') { $tree = $sp->parse_file($string); } else { $tree = $sp->parse_string($$string); } } return($tree); } ############################################################################## # Method: build_tree_xml_parser() # # This routine will be called if XML::SAX is not installed, or if XML::Parser # was specifically requested. It takes the same arguments as build_tree() and # returns the same data structure (XML::Parser 'Tree' style). # sub build_tree_xml_parser { my $self = shift; my $filename = shift; my $string = shift; eval { local($^W) = 0; # Suppress warning from Expat.pm re File::Spec::load() require XML::Parser; # We didn't need it until now }; if($@) { croak "XMLin() requires either XML::SAX or XML::Parser"; } if($self->{opt}->{nsexpand}) { carp "'nsexpand' option requires XML::SAX"; } my $xp = $self->new_xml_parser(); my($tree); if($filename) { # $tree = $xp->parsefile($filename); # Changed due to prob w/mod_perl open(my $xfh, '<', $filename) || croak qq($filename - $!); $tree = $xp->parse($xfh); } else { $tree = $xp->parse($$string); } return($tree); } ############################################################################## # Method: new_xml_parser() # # Simply calls the XML::Parser constructor. Override this method to customise # the behaviour of the parser. # sub new_xml_parser { my($self) = @_; my $xp = XML::Parser->new(Style => 'Tree', @{$self->{opt}->{parseropts}}); $xp->setHandlers(ExternEnt => sub {return $_[2]}); return $xp; } ############################################################################## # Method: cache_write_storable() # # Wrapper routine for invoking Storable::nstore() to cache a parsed data # structure. # sub cache_write_storable { my($self, $data, $filename) = @_; my $cachefile = $self->storable_filename($filename); require Storable; # We didn't need it until now if ('VMS' eq $^O) { Storable::nstore($data, $cachefile); } else { # If the following line fails for you, your Storable.pm is old - upgrade Storable::lock_nstore($data, $cachefile); } } ############################################################################## # Method: cache_read_storable() # # Wrapper routine for invoking Storable::retrieve() to read a cached parsed # data structure. Only returns cached data if the cache file exists and is # newer than the source XML file. # sub cache_read_storable { my($self, $filename) = @_; my $cachefile = $self->storable_filename($filename); return unless(-r $cachefile); return unless((stat($cachefile))[9] > (stat($filename))[9]); require Storable; # We didn't need it until now if ('VMS' eq $^O) { return(Storable::retrieve($cachefile)); } else { return(Storable::lock_retrieve($cachefile)); } } ############################################################################## # Method: storable_filename() # # Translates the supplied source XML filename into a filename for the storable # cached data. A '.stor' suffix is added after stripping an optional '.xml' # suffix. # sub storable_filename { my($self, $cachefile) = @_; $cachefile =~ s{(\.xml)?$}{.stor}; return $cachefile; } ############################################################################## # Method: cache_write_memshare() # # Takes the supplied data structure reference and stores it away in a global # hash structure. # sub cache_write_memshare { my($self, $data, $filename) = @_; $MemShareCache{$filename} = [time(), $data]; } ############################################################################## # Method: cache_read_memshare() # # Takes a filename and looks in a global hash for a cached parsed version. # sub cache_read_memshare { my($self, $filename) = @_; return unless($MemShareCache{$filename}); return unless($MemShareCache{$filename}->[0] > (stat($filename))[9]); return($MemShareCache{$filename}->[1]); } ############################################################################## # Method: cache_write_memcopy() # # Takes the supplied data structure and stores a copy of it in a global hash # structure. # sub cache_write_memcopy { my($self, $data, $filename) = @_; require Storable; # We didn't need it until now $MemCopyCache{$filename} = [time(), Storable::dclone($data)]; } ############################################################################## # Method: cache_read_memcopy() # # Takes a filename and looks in a global hash for a cached parsed version. # Returns a reference to a copy of that data structure. # sub cache_read_memcopy { my($self, $filename) = @_; return unless($MemCopyCache{$filename}); return unless($MemCopyCache{$filename}->[0] > (stat($filename))[9]); return(Storable::dclone($MemCopyCache{$filename}->[1])); } ############################################################################## # Sub/Method: XMLout() # # Exported routine for 'unslurping' a data structure out to XML. # # Expects a reference to a data structure and an optional list of option # name => value pairs. # sub XMLout { my $self = &_get_object; # note, @_ is passed implicitly croak "XMLout() requires at least one argument" unless(@_); my $ref = shift; $self->handle_options('out', @_); # If namespace expansion is set, XML::NamespaceSupport is required if($self->{opt}->{nsexpand}) { require XML::NamespaceSupport; $self->{nsup} = XML::NamespaceSupport->new(); $self->{ns_prefix} = 'aaa'; } # Wrap top level arrayref in a hash if(UNIVERSAL::isa($ref, 'ARRAY')) { $ref = { anon => $ref }; } # Extract rootname from top level hash if keeproot enabled if($self->{opt}->{keeproot}) { my(@keys) = keys(%$ref); if(@keys == 1) { $ref = $ref->{$keys[0]}; $self->{opt}->{rootname} = $keys[0]; } } # Ensure there are no top level attributes if we're not adding root elements elsif($self->{opt}->{rootname} eq '') { if(UNIVERSAL::isa($ref, 'HASH')) { my $refsave = $ref; $ref = {}; foreach (keys(%$refsave)) { if(ref($refsave->{$_})) { $ref->{$_} = $refsave->{$_}; } else { $ref->{$_} = [ $refsave->{$_} ]; } } } } # Encode the hashref and write to file if necessary $self->{_ancestors} = {}; my $xml = $self->value_to_xml($ref, $self->{opt}->{rootname}, ''); delete $self->{_ancestors}; if($self->{opt}->{xmldecl}) { $xml = $self->{opt}->{xmldecl} . "\n" . $xml; } if($self->{opt}->{outputfile}) { if(ref($self->{opt}->{outputfile})) { my $fh = $self->{opt}->{outputfile}; if(UNIVERSAL::isa($fh, 'GLOB') and !UNIVERSAL::can($fh, 'print')) { eval { require IO::Handle; }; croak $@ if $@; } return($fh->print($xml)); } else { open(my $out, '>', "$self->{opt}->{outputfile}") || croak "open($self->{opt}->{outputfile}): $!"; binmode($out, ':utf8') if($] >= 5.008); print $out $xml or croak "print: $!"; close $out or croak "close: $!"; } } elsif($self->{opt}->{handler}) { require XML::SAX; my $sp = XML::SAX::ParserFactory->parser( Handler => $self->{opt}->{handler} ); return($sp->parse_string($xml)); } else { return($xml); } } ############################################################################## # Method: handle_options() # # Helper routine for both XMLin() and XMLout(). Both routines handle their # first argument and assume all other args are options handled by this routine. # Saves a hash of options in $self->{opt}. # # If default options were passed to the constructor, they will be retrieved # here and merged with options supplied to the method call. # # First argument should be the string 'in' or the string 'out'. # # Remaining arguments should be name=>value pairs. Sets up default values # for options not supplied. Unrecognised options are a fatal error. # sub handle_options { my $self = shift; my $dirn = shift; # Determine valid options based on context my %known_opt; if($dirn eq 'in') { @known_opt{@KnownOptIn} = @KnownOptIn; } else { @known_opt{@KnownOptOut} = @KnownOptOut; } # Store supplied options in hashref and weed out invalid ones if(@_ % 2) { croak "Options must be name=>value pairs (odd number supplied)"; } my %raw_opt = @_; my $opt = {}; $self->{opt} = $opt; while(my($key, $val) = each %raw_opt) { my $lkey = lc($key); $lkey =~ s/_//g; croak "Unrecognised option: $key" unless($known_opt{$lkey}); $opt->{$lkey} = $val; } # Merge in options passed to constructor foreach (keys(%known_opt)) { unless(exists($opt->{$_})) { if(exists($self->{def_opt}->{$_})) { $opt->{$_} = $self->{def_opt}->{$_}; } } } # Set sensible defaults if not supplied if(exists($opt->{rootname})) { unless(defined($opt->{rootname})) { $opt->{rootname} = ''; } } else { $opt->{rootname} = $DefRootName; } if($opt->{xmldecl} and $opt->{xmldecl} eq '1') { $opt->{xmldecl} = $DefXmlDecl; } if(exists($opt->{contentkey})) { if($opt->{contentkey} =~ m{^-(.*)$}) { $opt->{contentkey} = $1; $opt->{collapseagain} = 1; } } else { $opt->{contentkey} = $DefContentKey; } unless(exists($opt->{normalisespace})) { $opt->{normalisespace} = $opt->{normalizespace}; } $opt->{normalisespace} = 0 unless(defined($opt->{normalisespace})); # Cleanups for values assumed to be arrays later if($opt->{searchpath}) { unless(ref($opt->{searchpath})) { $opt->{searchpath} = [ $opt->{searchpath} ]; } } else { $opt->{searchpath} = [ ]; } if($opt->{cache} and !ref($opt->{cache})) { $opt->{cache} = [ $opt->{cache} ]; } if($opt->{cache}) { $_ = lc($_) foreach (@{$opt->{cache}}); foreach my $scheme (@{$opt->{cache}}) { my $method = 'cache_read_' . $scheme; croak "Unsupported caching scheme: $scheme" unless($self->can($method)); } } if(exists($opt->{parseropts})) { if(warnings::enabled()) { carp "Warning: " . "'ParserOpts' is deprecated, contact the author if you need it"; } } else { $opt->{parseropts} = [ ]; } # Special cleanup for {forcearray} which could be regex, arrayref or boolean # or left to default to 0 if(exists($opt->{forcearray})) { if(ref($opt->{forcearray}) eq 'Regexp') { $opt->{forcearray} = [ $opt->{forcearray} ]; } if(ref($opt->{forcearray}) eq 'ARRAY') { my @force_list = @{$opt->{forcearray}}; if(@force_list) { $opt->{forcearray} = {}; foreach my $tag (@force_list) { if(ref($tag) eq 'Regexp') { push @{$opt->{forcearray}->{_regex}}, $tag; } else { $opt->{forcearray}->{$tag} = 1; } } } else { $opt->{forcearray} = 0; } } else { $opt->{forcearray} = ( $opt->{forcearray} ? 1 : 0 ); } } else { if($opt->{strictmode} and $dirn eq 'in') { croak "No value specified for 'ForceArray' option in call to XML$dirn()"; } $opt->{forcearray} = 0; } # Special cleanup for {keyattr} which could be arrayref or hashref or left # to default to arrayref if(exists($opt->{keyattr})) { if(ref($opt->{keyattr})) { if(ref($opt->{keyattr}) eq 'HASH') { # Make a copy so we can mess with it $opt->{keyattr} = { %{$opt->{keyattr}} }; # Convert keyattr => { elem => '+attr' } # to keyattr => { elem => [ 'attr', '+' ] } foreach my $el (keys(%{$opt->{keyattr}})) { if($opt->{keyattr}->{$el} =~ /^(\+|-)?(.*)$/) { $opt->{keyattr}->{$el} = [ $2, ($1 ? $1 : '') ]; if($opt->{strictmode} and $dirn eq 'in') { next if($opt->{forcearray} == 1); next if(ref($opt->{forcearray}) eq 'HASH' and $opt->{forcearray}->{$el}); croak "<$el> set in KeyAttr but not in ForceArray"; } } else { delete($opt->{keyattr}->{$el}); # Never reached (famous last words?) } } } else { if(@{$opt->{keyattr}} == 0) { delete($opt->{keyattr}); } } } else { $opt->{keyattr} = [ $opt->{keyattr} ]; } } else { if($opt->{strictmode}) { croak "No value specified for 'KeyAttr' option in call to XML$dirn()"; } $opt->{keyattr} = [ @DefKeyAttr ]; } # Special cleanup for {valueattr} which could be arrayref or hashref if(exists($opt->{valueattr})) { if(ref($opt->{valueattr}) eq 'ARRAY') { $opt->{valueattrlist} = {}; $opt->{valueattrlist}->{$_} = 1 foreach(@{ delete $opt->{valueattr} }); } } # make sure there's nothing weird in {grouptags} if($opt->{grouptags}) { croak "Illegal value for 'GroupTags' option - expected a hashref" unless UNIVERSAL::isa($opt->{grouptags}, 'HASH'); while(my($key, $val) = each %{$opt->{grouptags}}) { next if $key ne $val; croak "Bad value in GroupTags: '$key' => '$val'"; } } # Check the {variables} option is valid and initialise variables hash if($opt->{variables} and !UNIVERSAL::isa($opt->{variables}, 'HASH')) { croak "Illegal value for 'Variables' option - expected a hashref"; } if($opt->{variables}) { $self->{_var_values} = { %{$opt->{variables}} }; } elsif($opt->{varattr}) { $self->{_var_values} = {}; } } ############################################################################## # Method: find_xml_file() # # Helper routine for XMLin(). # Takes a filename, and a list of directories, attempts to locate the file in # the directories listed. # Returns a full pathname on success; croaks on failure. # sub find_xml_file { my $self = shift; my $file = shift; my @search_path = @_; require File::Basename; require File::Spec; my($filename, $filedir) = File::Basename::fileparse($file); if($filename ne $file) { # Ignore searchpath if dir component return($file) if(-e $file); } else { my($path); foreach $path (@search_path) { my $fullpath = File::Spec->catfile($path, $file); return($fullpath) if(-e $fullpath); } } # If user did not supply a search path, default to current directory if(!@search_path) { return($file) if(-e $file); croak "File does not exist: $file"; } croak "Could not find $file in ", join(':', @search_path); } ############################################################################## # Method: collapse() # # Helper routine for XMLin(). This routine really comprises the 'smarts' (or # value add) of this module. # # Takes the parse tree that XML::Parser produced from the supplied XML and # recurses through it 'collapsing' unnecessary levels of indirection (nested # arrays etc) to produce a data structure that is easier to work with. # # Elements in the original parser tree are represented as an element name # followed by an arrayref. The first element of the array is a hashref # containing the attributes. The rest of the array contains a list of any # nested elements as name+arrayref pairs: # # , [ { }, , [ ... ], ... ] # # The special element name '0' (zero) flags text content. # # This routine cuts down the noise by discarding any text content consisting of # only whitespace and then moves the nested elements into the attribute hash # using the name of the nested element as the hash key and the collapsed # version of the nested element as the value. Multiple nested elements with # the same name will initially be represented as an arrayref, but this may be # 'folded' into a hashref depending on the value of the keyattr option. # sub collapse { my $self = shift; # Start with the hash of attributes my $attr = shift; if($self->{opt}->{noattr}) { # Discard if 'noattr' set $attr = $self->new_hashref; } elsif($self->{opt}->{normalisespace} == 2) { while(my($key, $value) = each %$attr) { $attr->{$key} = $self->normalise_space($value) } } # Do variable substitutions if(my $var = $self->{_var_values}) { while(my($key, $val) = each(%$attr)) { $val =~ s^\$\{([\w.]+)\}^ $self->get_var($1) ^ge; $attr->{$key} = $val; } } # Roll up 'value' attributes (but only if no nested elements) if(!@_ and keys %$attr == 1) { my($k) = keys %$attr; if($self->{opt}->{valueattrlist} and $self->{opt}->{valueattrlist}->{$k}) { return $attr->{$k}; } } # Add any nested elements my($key, $val); while(@_) { $key = shift; $val = shift; $val = '' if not defined $val; if(ref($val)) { $val = $self->collapse(@$val); next if(!defined($val) and $self->{opt}->{suppressempty}); } elsif($key eq '0') { next if($val =~ m{^\s*$}s); # Skip all whitespace content $val = $self->normalise_space($val) if($self->{opt}->{normalisespace} == 2); # do variable substitutions if(my $var = $self->{_var_values}) { $val =~ s^\$\{(\w+)\}^ $self->get_var($1) ^ge; } # look for variable definitions if(my $var = $self->{opt}->{varattr}) { if(exists $attr->{$var}) { $self->set_var($attr->{$var}, $val); } } # Collapse text content in element with no attributes to a string if(!%$attr and !@_) { return($self->{opt}->{forcecontent} ? { $self->{opt}->{contentkey} => $val } : $val ); } $key = $self->{opt}->{contentkey}; } # Combine duplicate attributes into arrayref if required if(exists($attr->{$key})) { if(UNIVERSAL::isa($attr->{$key}, 'ARRAY')) { push(@{$attr->{$key}}, $val); } else { $attr->{$key} = [ $attr->{$key}, $val ]; } } elsif(defined($val) and UNIVERSAL::isa($val, 'ARRAY')) { $attr->{$key} = [ $val ]; } else { if( $key ne $self->{opt}->{contentkey} and ( ($self->{opt}->{forcearray} == 1) or ( (ref($self->{opt}->{forcearray}) eq 'HASH') and ( $self->{opt}->{forcearray}->{$key} or (grep $key =~ $_, @{$self->{opt}->{forcearray}->{_regex}}) ) ) ) ) { $attr->{$key} = [ $val ]; } else { $attr->{$key} = $val; } } } # Turn arrayrefs into hashrefs if key fields present if($self->{opt}->{keyattr}) { while(($key,$val) = each %$attr) { if(defined($val) and UNIVERSAL::isa($val, 'ARRAY')) { $attr->{$key} = $self->array_to_hash($key, $val); } } } # disintermediate grouped tags if($self->{opt}->{grouptags}) { while(my($key, $val) = each(%$attr)) { next unless(UNIVERSAL::isa($val, 'HASH') and (keys %$val == 1)); next unless(exists($self->{opt}->{grouptags}->{$key})); my($child_key, $child_val) = %$val; if($self->{opt}->{grouptags}->{$key} eq $child_key) { $attr->{$key}= $child_val; } } } # Fold hashes containing a single anonymous array up into just the array my $count = scalar keys %$attr; if($count == 1 and exists $attr->{anon} and UNIVERSAL::isa($attr->{anon}, 'ARRAY') ) { return($attr->{anon}); } # Do the right thing if hash is empty, otherwise just return it if(!%$attr and exists($self->{opt}->{suppressempty})) { if(defined($self->{opt}->{suppressempty}) and $self->{opt}->{suppressempty} eq '') { return(''); } return(undef); } # Roll up named elements with named nested 'value' attributes if($self->{opt}->{valueattr}) { while(my($key, $val) = each(%$attr)) { next unless($self->{opt}->{valueattr}->{$key}); next unless(UNIVERSAL::isa($val, 'HASH') and (keys %$val == 1)); my($k) = keys %$val; next unless($k eq $self->{opt}->{valueattr}->{$key}); $attr->{$key} = $val->{$k}; } } return($attr) } ############################################################################## # Method: set_var() # # Called when a variable definition is encountered in the XML. (A variable # definition looks like value where attrname # matches the varattr setting). # sub set_var { my($self, $name, $value) = @_; $self->{_var_values}->{$name} = $value; } ############################################################################## # Method: get_var() # # Called during variable substitution to get the value for the named variable. # sub get_var { my($self, $name) = @_; my $value = $self->{_var_values}->{$name}; return $value if(defined($value)); return '${' . $name . '}'; } ############################################################################## # Method: normalise_space() # # Strips leading and trailing whitespace and collapses sequences of whitespace # characters to a single space. # sub normalise_space { my($self, $text) = @_; $text =~ s/^\s+//s; $text =~ s/\s+$//s; $text =~ s/\s\s+/ /sg; return $text; } ############################################################################## # Method: array_to_hash() # # Helper routine for collapse(). # Attempts to 'fold' an array of hashes into an hash of hashes. Returns a # reference to the hash on success or the original array if folding is # not possible. Behaviour is controlled by 'keyattr' option. # sub array_to_hash { my $self = shift; my $name = shift; my $arrayref = shift; my $hashref = $self->new_hashref; my($i, $key, $val, $flag); # Handle keyattr => { .... } if(ref($self->{opt}->{keyattr}) eq 'HASH') { return($arrayref) unless(exists($self->{opt}->{keyattr}->{$name})); ($key, $flag) = @{$self->{opt}->{keyattr}->{$name}}; for($i = 0; $i < @$arrayref; $i++) { if(UNIVERSAL::isa($arrayref->[$i], 'HASH') and exists($arrayref->[$i]->{$key}) ) { $val = $arrayref->[$i]->{$key}; if(ref($val)) { $self->die_or_warn("<$name> element has non-scalar '$key' key attribute"); return($arrayref); } $val = $self->normalise_space($val) if($self->{opt}->{normalisespace} == 1); $self->die_or_warn("<$name> element has non-unique value in '$key' key attribute: $val") if(exists($hashref->{$val})); $hashref->{$val} = $self->new_hashref( %{$arrayref->[$i]} ); $hashref->{$val}->{"-$key"} = $hashref->{$val}->{$key} if($flag eq '-'); delete $hashref->{$val}->{$key} unless($flag eq '+'); } else { $self->die_or_warn("<$name> element has no '$key' key attribute"); return($arrayref); } } } # Or assume keyattr => [ .... ] else { my $default_keys = join(',', @DefKeyAttr) eq join(',', @{$self->{opt}->{keyattr}}); ELEMENT: for($i = 0; $i < @$arrayref; $i++) { return($arrayref) unless(UNIVERSAL::isa($arrayref->[$i], 'HASH')); foreach $key (@{$self->{opt}->{keyattr}}) { if(defined($arrayref->[$i]->{$key})) { $val = $arrayref->[$i]->{$key}; if(ref($val)) { $self->die_or_warn("<$name> element has non-scalar '$key' key attribute") if not $default_keys; return($arrayref); } $val = $self->normalise_space($val) if($self->{opt}->{normalisespace} == 1); $self->die_or_warn("<$name> element has non-unique value in '$key' key attribute: $val") if(exists($hashref->{$val})); $hashref->{$val} = $self->new_hashref( %{$arrayref->[$i]} ); delete $hashref->{$val}->{$key}; next ELEMENT; } } return($arrayref); # No keyfield matched } } # collapse any hashes which now only have a 'content' key if($self->{opt}->{collapseagain}) { $hashref = $self->collapse_content($hashref); } return($hashref); } ############################################################################## # Method: die_or_warn() # # Takes a diagnostic message and does one of three things: # 1. dies if strict mode is enabled # 2. warns if warnings are enabled but strict mode is not # 3. ignores message and returns silently if neither strict mode nor warnings # are enabled # sub die_or_warn { my $self = shift; my $msg = shift; croak $msg if($self->{opt}->{strictmode}); if(warnings::enabled()) { carp "Warning: $msg"; } } ############################################################################## # Method: new_hashref() # # This is a hook routine for overriding in a sub-class. Some people believe # that using Tie::IxHash here will solve order-loss problems. # sub new_hashref { my $self = shift; return { @_ }; } ############################################################################## # Method: collapse_content() # # Helper routine for array_to_hash # # Arguments expected are: # - an XML::Simple object # - a hashref # the hashref is a former array, turned into a hash by array_to_hash because # of the presence of key attributes # at this point collapse_content avoids over-complicated structures like # dir => { libexecdir => { content => '$exec_prefix/libexec' }, # localstatedir => { content => '$prefix' }, # } # into # dir => { libexecdir => '$exec_prefix/libexec', # localstatedir => '$prefix', # } sub collapse_content { my $self = shift; my $hashref = shift; my $contentkey = $self->{opt}->{contentkey}; # first go through the values,checking that they are fit to collapse foreach my $val (values %$hashref) { return $hashref unless ( (ref($val) eq 'HASH') and (keys %$val == 1) and (exists $val->{$contentkey}) ); } # now collapse them foreach my $key (keys %$hashref) { $hashref->{$key}= $hashref->{$key}->{$contentkey}; } return $hashref; } ############################################################################## # Method: value_to_xml() # # Helper routine for XMLout() - recurses through a data structure building up # and returning an XML representation of that structure as a string. # # Arguments expected are: # - the data structure to be encoded (usually a reference) # - the XML tag name to use for this item # - a string of spaces for use as the current indent level # sub value_to_xml { my $self = shift;; # Grab the other arguments my($ref, $name, $indent) = @_; my $named = (defined($name) and $name ne '' ? 1 : 0); my $nl = "\n"; my $is_root = $indent eq '' ? 1 : 0; # Warning, dirty hack! if($self->{opt}->{noindent}) { $indent = ''; $nl = ''; } # Convert to XML my $refaddr = Scalar::Util::refaddr($ref); if($refaddr) { croak "circular data structures not supported" if $self->{_ancestors}->{$refaddr}; $self->{_ancestors}->{$refaddr} = $ref; # keep ref alive until we delete it } else { if($named) { return(join('', $indent, '<', $name, '>', ($self->{opt}->{noescape} ? $ref : $self->escape_value($ref)), '", $nl )); } else { return("$ref$nl"); } } # Unfold hash to array if possible if(UNIVERSAL::isa($ref, 'HASH') # It is a hash and keys %$ref # and it's not empty and $self->{opt}->{keyattr} # and folding is enabled and !$is_root # and its not the root element ) { $ref = $self->hash_to_array($name, $ref); } my @result = (); my($key, $value); # Handle hashrefs if(UNIVERSAL::isa($ref, 'HASH')) { # Reintermediate grouped values if applicable if($self->{opt}->{grouptags}) { $ref = $self->copy_hash($ref); while(my($key, $val) = each %$ref) { if($self->{opt}->{grouptags}->{$key}) { $ref->{$key} = $self->new_hashref( $self->{opt}->{grouptags}->{$key} => $val ); } } } # Scan for namespace declaration attributes my $nsdecls = ''; my $default_ns_uri; if($self->{nsup}) { $ref = $self->copy_hash($ref); $self->{nsup}->push_context(); # Look for default namespace declaration first if(exists($ref->{xmlns})) { $self->{nsup}->declare_prefix('', $ref->{xmlns}); $nsdecls .= qq( xmlns="$ref->{xmlns}"); delete($ref->{xmlns}); } $default_ns_uri = $self->{nsup}->get_uri(''); # Then check all the other keys foreach my $qname (keys(%$ref)) { my($uri, $lname) = $self->{nsup}->parse_jclark_notation($qname); if($uri) { if($uri eq $xmlns_ns) { $self->{nsup}->declare_prefix($lname, $ref->{$qname}); $nsdecls .= qq( xmlns:$lname="$ref->{$qname}"); delete($ref->{$qname}); } } } # Translate any remaining Clarkian names foreach my $qname (keys(%$ref)) { my($uri, $lname) = $self->{nsup}->parse_jclark_notation($qname); if($uri) { if($default_ns_uri and $uri eq $default_ns_uri) { $ref->{$lname} = $ref->{$qname}; delete($ref->{$qname}); } else { my $prefix = $self->{nsup}->get_prefix($uri); unless($prefix) { # $self->{nsup}->declare_prefix(undef, $uri); # $prefix = $self->{nsup}->get_prefix($uri); $prefix = $self->{ns_prefix}++; $self->{nsup}->declare_prefix($prefix, $uri); $nsdecls .= qq( xmlns:$prefix="$uri"); } $ref->{"$prefix:$lname"} = $ref->{$qname}; delete($ref->{$qname}); } } } } my @nested = (); my $text_content = undef; if($named) { push @result, $indent, '<', $name, $nsdecls; } if(keys %$ref) { my $first_arg = 1; foreach my $key ($self->sorted_keys($name, $ref)) { my $value = $ref->{$key}; next if(substr($key, 0, 1) eq '-'); if(!defined($value)) { next if $self->{opt}->{suppressempty}; unless(exists($self->{opt}->{suppressempty}) and !defined($self->{opt}->{suppressempty}) ) { carp 'Use of uninitialized value' if warnings::enabled(); } if($key eq $self->{opt}->{contentkey}) { $text_content = ''; } else { $value = exists($self->{opt}->{suppressempty}) ? {} : ''; } } if(!ref($value) and $self->{opt}->{valueattr} and $self->{opt}->{valueattr}->{$key} ) { $value = $self->new_hashref( $self->{opt}->{valueattr}->{$key} => $value ); } if(ref($value) or $self->{opt}->{noattr}) { push @nested, $self->value_to_xml($value, $key, "$indent "); } else { if($key eq $self->{opt}->{contentkey}) { $value = $self->escape_value($value) unless($self->{opt}->{noescape}); $text_content = $value; } else { $value = $self->escape_attr($value) unless($self->{opt}->{noescape}); push @result, "\n$indent " . ' ' x length($name) if($self->{opt}->{attrindent} and !$first_arg); push @result, ' ', $key, '="', $value , '"'; $first_arg = 0; } } } } else { $text_content = ''; } if(@nested or defined($text_content)) { if($named) { push @result, ">"; if(defined($text_content)) { push @result, $text_content; $nested[0] =~ s/^\s+// if(@nested); } else { push @result, $nl; } if(@nested) { push @result, @nested, $indent; } push @result, '", $nl; } else { push @result, @nested; # Special case if no root elements } } else { push @result, " />", $nl; } $self->{nsup}->pop_context() if($self->{nsup}); } # Handle arrayrefs elsif(UNIVERSAL::isa($ref, 'ARRAY')) { foreach $value (@$ref) { next if !defined($value) and $self->{opt}->{suppressempty}; if(!ref($value)) { push @result, $indent, '<', $name, '>', ($self->{opt}->{noescape} ? $value : $self->escape_value($value)), '$nl"; } elsif(UNIVERSAL::isa($value, 'HASH')) { push @result, $self->value_to_xml($value, $name, $indent); } else { push @result, $indent, '<', $name, ">$nl", $self->value_to_xml($value, 'anon', "$indent "), $indent, '$nl"; } } } else { croak "Can't encode a value of type: " . ref($ref); } delete $self->{_ancestors}->{$refaddr}; return(join('', @result)); } ############################################################################## # Method: sorted_keys() # # Returns the keys of the referenced hash sorted into alphabetical order, but # with the 'key' key (as in KeyAttr) first, if there is one. # sub sorted_keys { my($self, $name, $ref) = @_; return keys %$ref if $self->{opt}->{nosort}; my %hash = %$ref; my $keyattr = $self->{opt}->{keyattr}; my @key; if(ref $keyattr eq 'HASH') { if(exists $keyattr->{$name} and exists $hash{$keyattr->{$name}->[0]}) { push @key, $keyattr->{$name}->[0]; delete $hash{$keyattr->{$name}->[0]}; } } elsif(ref $keyattr eq 'ARRAY') { foreach (@{$keyattr}) { if(exists $hash{$_}) { push @key, $_; delete $hash{$_}; last; } } } return(@key, sort keys %hash); } ############################################################################## # Method: escape_value() # # Helper routine for automatically escaping values for XMLout(). # Expects a scalar data value. Returns escaped version. # sub escape_value { my($self, $data) = @_; return '' unless(defined($data)); $data =~ s/&/&/sg; $data =~ s//>/sg; $data =~ s/"/"/sg; my $level = $self->{opt}->{numericescape} or return $data; return $self->numeric_escape($data, $level); } sub numeric_escape { my($self, $data, $level) = @_; if($self->{opt}->{numericescape} eq '2') { $data =~ s/([^\x00-\x7F])/'&#' . ord($1) . ';'/gse; } else { $data =~ s/([^\x00-\xFF])/'&#' . ord($1) . ';'/gse; } return $data; } ############################################################################## # Method: escape_attr() # # Helper routine for escaping attribute values. Defaults to escape_value(), # but may be overridden by a subclass to customise behaviour. # sub escape_attr { my $self = shift; return $self->escape_value(@_); } ############################################################################## # Method: hash_to_array() # # Helper routine for value_to_xml(). # Attempts to 'unfold' a hash of hashes into an array of hashes. Returns a # reference to the array on success or the original hash if unfolding is # not possible. # sub hash_to_array { my $self = shift; my $parent = shift; my $hashref = shift; my $arrayref = []; my($key, $value); my @keys = $self->{opt}->{nosort} ? keys %$hashref : sort keys %$hashref; foreach $key (@keys) { $value = $hashref->{$key}; return($hashref) unless(UNIVERSAL::isa($value, 'HASH')); if(ref($self->{opt}->{keyattr}) eq 'HASH') { return($hashref) unless(defined($self->{opt}->{keyattr}->{$parent})); push @$arrayref, $self->copy_hash( $value, $self->{opt}->{keyattr}->{$parent}->[0] => $key ); } else { push(@$arrayref, { $self->{opt}->{keyattr}->[0] => $key, %$value }); } } return($arrayref); } ############################################################################## # Method: copy_hash() # # Helper routine for hash_to_array(). When unfolding a hash of hashes into # an array of hashes, we need to copy the key from the outer hash into the # inner hash. This routine makes a copy of the original hash so we don't # destroy the original data structure. You might wish to override this # method if you're using tied hashes and don't want them to get untied. # sub copy_hash { my($self, $orig, @extra) = @_; return { @extra, %$orig }; } ############################################################################## # Methods required for building trees from SAX events ############################################################################## sub start_document { my $self = shift; $self->handle_options('in') unless($self->{opt}); $self->{lists} = []; $self->{curlist} = $self->{tree} = []; } sub start_element { my $self = shift; my $element = shift; my $name = $element->{Name}; if($self->{opt}->{nsexpand}) { $name = $element->{LocalName} || ''; if($element->{NamespaceURI}) { $name = '{' . $element->{NamespaceURI} . '}' . $name; } } my $attributes = {}; if($element->{Attributes}) { # Might be undef foreach my $attr (values %{$element->{Attributes}}) { if($self->{opt}->{nsexpand}) { my $name = $attr->{LocalName} || ''; if($attr->{NamespaceURI}) { $name = '{' . $attr->{NamespaceURI} . '}' . $name } $name = 'xmlns' if($name eq $bad_def_ns_jcn); $attributes->{$name} = $attr->{Value}; } else { $attributes->{$attr->{Name}} = $attr->{Value}; } } } my $newlist = [ $attributes ]; push @{ $self->{lists} }, $self->{curlist}; push @{ $self->{curlist} }, $name => $newlist; $self->{curlist} = $newlist; } sub characters { my $self = shift; my $chars = shift; my $text = $chars->{Data}; my $clist = $self->{curlist}; my $pos = $#$clist; if ($pos > 0 and $clist->[$pos - 1] eq '0') { $clist->[$pos] .= $text; } else { push @$clist, 0 => $text; } } sub end_element { my $self = shift; $self->{curlist} = pop @{ $self->{lists} }; } sub end_document { my $self = shift; delete($self->{curlist}); delete($self->{lists}); my $tree = $self->{tree}; delete($self->{tree}); # Return tree as-is to XMLin() return($tree) if($self->{nocollapse}); # Or collapse it before returning it to SAX parser class if($self->{opt}->{keeproot}) { $tree = $self->collapse({}, @$tree); } else { $tree = $self->collapse(@{$tree->[1]}); } if($self->{opt}->{datahandler}) { return($self->{opt}->{datahandler}->($self, $tree)); } return($tree); } *xml_in = \&XMLin; *xml_out = \&XMLout; 1; __END__ =head1 STATUS OF THIS MODULE The use of this module in new code is B. Other modules are available which provide more straightforward and consistent interfaces. In particular, L is highly recommended and you can refer to L for a tutorial introduction. L is another excellent alternative. The major problems with this module are the large number of options (some of which have unfortunate defaults) and the arbitrary ways in which these options interact - often producing unexpected results. Patches with bug fixes and documentation fixes are welcome, but new features are unlikely to be added. =head1 QUICK START Say you have a script called B and a file of configuration options called B containing the following:
10.0.0.101
10.0.1.101
10.0.0.102
10.0.0.103
10.0.1.103
The following lines of code in B: use XML::Simple qw(:strict); my $config = XMLin(undef, KeyAttr => { server => 'name' }, ForceArray => [ 'server', 'address' ]); will 'slurp' the configuration options into the hashref $config (because no filename or XML string was passed as the first argument to C the name and location of the XML file will be inferred from name and location of the script). You can dump out the contents of the hashref using Data::Dumper: use Data::Dumper; print Dumper($config); which will produce something like this (formatting has been adjusted for brevity): { 'logdir' => '/var/log/foo/', 'debugfile' => '/tmp/foo.debug', 'server' => { 'sahara' => { 'osversion' => '2.6', 'osname' => 'solaris', 'address' => [ '10.0.0.101', '10.0.1.101' ] }, 'gobi' => { 'osversion' => '6.5', 'osname' => 'irix', 'address' => [ '10.0.0.102' ] }, 'kalahari' => { 'osversion' => '2.0.34', 'osname' => 'linux', 'address' => [ '10.0.0.103', '10.0.1.103' ] } } } Your script could then access the name of the log directory like this: print $config->{logdir}; similarly, the second address on the server 'kalahari' could be referenced as: print $config->{server}->{kalahari}->{address}->[1]; Note: If the mapping between the output of Data::Dumper and the print statements above is not obvious to you, then please refer to the 'references' tutorial (AKA: "Mark's very short tutorial about references") at L. In this example, the C<< ForceArray >> option was used to list elements that might occur multiple times and should therefore be represented as arrayrefs (even when only one element is present). The C<< KeyAttr >> option was used to indicate that each C<< >> element has a unique identifier in the C<< name >> attribute. This allows you to index directly to a particular server record using the name as a hash key (as shown above). For simple requirements, that's really all there is to it. If you want to store your XML in a different directory or file, or pass it in as a string or even pass it in via some derivative of an IO::Handle, you'll need to check out L<"OPTIONS">. If you want to turn off or tweak the array folding feature (that neat little transformation that produced $config->{server}) you'll find options for that as well. If you want to generate XML (for example to write a modified version of $config back out as XML), check out C. If your needs are not so simple, this may not be the module for you. In that case, you might want to read L<"WHERE TO FROM HERE?">. =head1 DESCRIPTION The XML::Simple module provides a simple API layer on top of an underlying XML parsing module (either XML::Parser or one of the SAX2 parser modules). Two functions are exported: C and C. Note: you can explicitly request the lower case versions of the function names: C and C. The simplest approach is to call these two functions directly, but an optional object oriented interface (see L<"OPTIONAL OO INTERFACE"> below) allows them to be called as methods of an B object. The object interface can also be used at either end of a SAX pipeline. =head2 XMLin() Parses XML formatted data and returns a reference to a data structure which contains the same information in a more readily accessible form. (Skip down to L<"EXAMPLES"> below, for more sample code). C accepts an optional XML specifier followed by zero or more 'name => value' option pairs. The XML specifier can be one of the following: =over 4 =item A filename If the filename contains no directory components C will look for the file in each directory in the SearchPath (see L<"OPTIONS"> below) or in the current directory if the SearchPath option is not defined. eg: $ref = XMLin('/etc/params.xml'); Note, the filename '-' can be used to parse from STDIN. =item undef If there is no XML specifier, C will check the script directory and each of the SearchPath directories for a file with the same name as the script but with the extension '.xml'. Note: if you wish to specify options, you must specify the value 'undef'. eg: $ref = XMLin(undef, ForceArray => 1); =item A string of XML A string containing XML (recognised by the presence of '<' and '>' characters) will be parsed directly. eg: $ref = XMLin(''); =item An IO::Handle object An IO::Handle object will be read to EOF and its contents parsed. eg: $fh = IO::File->new('/etc/params.xml'); $ref = XMLin($fh); =back =head2 XMLout() Takes a data structure (generally a hashref) and returns an XML encoding of that structure. If the resulting XML is parsed using C, it should return a data structure equivalent to the original (see caveats below). The C function can also be used to output the XML as SAX events see the C option and L<"SAX SUPPORT"> for more details). When translating hashes to XML, hash keys which have a leading '-' will be silently skipped. This is the approved method for marking elements of a data structure which should be ignored by C. (Note: If these items were not skipped the key names would be emitted as element or attribute names with a leading '-' which would not be valid XML). =head2 Caveats Some care is required in creating data structures which will be passed to C. Hash keys from the data structure will be encoded as either XML element names or attribute names. Therefore, you should use hash key names which conform to the relatively strict XML naming rules: Names in XML must begin with a letter. The remaining characters may be letters, digits, hyphens (-), underscores (_) or full stops (.). It is also allowable to include one colon (:) in an element name but this should only be used when working with namespaces (B can only usefully work with namespaces when teamed with a SAX Parser). You can use other punctuation characters in hash values (just not in hash keys) however B does not support dumping binary data. If you break these rules, the current implementation of C will simply emit non-compliant XML which will be rejected if you try to read it back in. (A later version of B might take a more proactive approach). Note also that although you can nest hashes and arrays to arbitrary levels, circular data structures are not supported and will cause C to die. If you wish to 'round-trip' arbitrary data structures from Perl to XML and back to Perl, then you should probably disable array folding (using the KeyAttr option) both with C and with C. If you still don't get the expected results, you may prefer to use L which is designed for exactly that purpose. Refer to L<"WHERE TO FROM HERE?"> if C is too simple for your needs. =head1 OPTIONS B supports a number of options (in fact as each release of B adds more options, the module's claim to the name 'Simple' becomes increasingly tenuous). If you find yourself repeatedly having to specify the same options, you might like to investigate L<"OPTIONAL OO INTERFACE"> below. If you can't be bothered reading the documentation, refer to L<"STRICT MODE"> to automatically catch common mistakes. Because there are so many options, it's hard for new users to know which ones are important, so here are the two you really need to know about: =over 4 =item * check out C because you'll almost certainly want to turn it on =item * make sure you know what the C option does and what its default value is because it may surprise you otherwise (note in particular that 'KeyAttr' affects both C and C) =back The option name headings below have a trailing 'comment' - a hash followed by two pieces of metadata: =over 4 =item * Options are marked with 'I' if they are recognised by C and 'I' if they are recognised by C. =item * Each option is also flagged to indicate whether it is: 'important' - don't use the module until you understand this one 'handy' - you can skip this on the first time through 'advanced' - you can skip this on the second time through 'SAX only' - don't worry about this unless you're using SAX (or alternatively if you need this, you also need SAX) 'seldom used' - you'll probably never use this unless you were the person that requested the feature =back The options are listed alphabetically: Note: option names are no longer case sensitive so you can use the mixed case versions shown here; all lower case as required by versions 2.03 and earlier; or you can add underscores between the words (eg: key_attr). =head2 AttrIndent => 1 I<# out - handy> When you are using C, enable this option to have attributes printed one-per-line with sensible indentation rather than all on one line. =head2 Cache => [ cache schemes ] I<# in - advanced> Because loading the B module and parsing an XML file can consume a significant number of CPU cycles, it is often desirable to cache the output of C for later reuse. When parsing from a named file, B supports a number of caching schemes. The 'Cache' option may be used to specify one or more schemes (using an anonymous array). Each scheme will be tried in turn in the hope of finding a cached pre-parsed representation of the XML file. If no cached copy is found, the file will be parsed and the first cache scheme in the list will be used to save a copy of the results. The following cache schemes have been implemented: =over 4 =item storable Utilises B to read/write a cache file with the same name as the XML file but with the extension .stor =item memshare When a file is first parsed, a copy of the resulting data structure is retained in memory in the B module's namespace. Subsequent calls to parse the same file will return a reference to this structure. This cached version will persist only for the life of the Perl interpreter (which in the case of mod_perl for example, may be some significant time). Because each caller receives a reference to the same data structure, a change made by one caller will be visible to all. For this reason, the reference returned should be treated as read-only. =item memcopy This scheme works identically to 'memshare' (above) except that each caller receives a reference to a new data structure which is a copy of the cached version. Copying the data structure will add a little processing overhead, therefore this scheme should only be used where the caller intends to modify the data structure (or wishes to protect itself from others who might). This scheme uses B to perform the copy. =back Warning! The memory-based caching schemes compare the timestamp on the file to the time when it was last parsed. If the file is stored on an NFS filesystem (or other network share) and the clock on the file server is not exactly synchronised with the clock where your script is run, updates to the source XML file may appear to be ignored. =head2 ContentKey => 'keyname' I<# in+out - seldom used> When text content is parsed to a hash value, this option lets you specify a name for the hash key to override the default 'content'. So for example: XMLin('Text', ContentKey => 'text') will parse to: { 'one' => 1, 'text' => 'Text' } instead of: { 'one' => 1, 'content' => 'Text' } C will also honour the value of this option when converting a hashref to XML. You can also prefix your selected key name with a '-' character to have C try a little harder to eliminate unnecessary 'content' keys after array folding. For example: XMLin( 'FirstSecond', KeyAttr => {item => 'name'}, ForceArray => [ 'item' ], ContentKey => '-content' ) will parse to: { 'item' => { 'one' => 'First' 'two' => 'Second' } } rather than this (without the '-'): { 'item' => { 'one' => { 'content' => 'First' } 'two' => { 'content' => 'Second' } } } =head2 DataHandler => code_ref I<# in - SAX only> When you use an B object as a SAX handler, it will return a 'simple tree' data structure in the same format as C would return. If this option is set (to a subroutine reference), then when the tree is built the subroutine will be called and passed two arguments: a reference to the B object and a reference to the data tree. The return value from the subroutine will be returned to the SAX driver. (See L<"SAX SUPPORT"> for more details). =head2 ForceArray => 1 I<# in - important> This option should be set to '1' to force nested elements to be represented as arrays even when there is only one. Eg, with ForceArray enabled, this XML: value would parse to this: { 'name' => [ 'value' ] } instead of this (the default): { 'name' => 'value' } This option is especially useful if the data structure is likely to be written back out as XML and the default behaviour of rolling single nested elements up into attributes is not desirable. If you are using the array folding feature, you should almost certainly enable this option. If you do not, single nested elements will not be parsed to arrays and therefore will not be candidates for folding to a hash. (Given that the default value of 'KeyAttr' enables array folding, the default value of this option should probably also have been enabled too - sorry). =head2 ForceArray => [ names ] I<# in - important> This alternative (and preferred) form of the 'ForceArray' option allows you to specify a list of element names which should always be forced into an array representation, rather than the 'all or nothing' approach above. It is also possible (since version 2.05) to include compiled regular expressions in the list - any element names which match the pattern will be forced to arrays. If the list contains only a single regex, then it is not necessary to enclose it in an arrayref. Eg: ForceArray => qr/_list$/ =head2 ForceContent => 1 I<# in - seldom used> When C parses elements which have text content as well as attributes, the text content must be represented as a hash value rather than a simple scalar. This option allows you to force text content to always parse to a hash value even when there are no attributes. So for example: XMLin('text1text2', ForceContent => 1) will parse to: { 'x' => { 'content' => 'text1' }, 'y' => { 'a' => 2, 'content' => 'text2' } } instead of: { 'x' => 'text1', 'y' => { 'a' => 2, 'content' => 'text2' } } =head2 GroupTags => { grouping tag => grouped tag } I<# in+out - handy> You can use this option to eliminate extra levels of indirection in your Perl data structure. For example this XML: /usr/bin /usr/local/bin /usr/X11/bin Would normally be read into a structure like this: { searchpath => { dir => [ '/usr/bin', '/usr/local/bin', '/usr/X11/bin' ] } } But when read in with the appropriate value for 'GroupTags': my $opt = XMLin($xml, GroupTags => { searchpath => 'dir' }); It will return this simpler structure: { searchpath => [ '/usr/bin', '/usr/local/bin', '/usr/X11/bin' ] } The grouping element (C<< >> in the example) must not contain any attributes or elements other than the grouped element. You can specify multiple 'grouping element' to 'grouped element' mappings in the same hashref. If this option is combined with C, the array folding will occur first and then the grouped element names will be eliminated. C will also use the grouptag mappings to re-introduce the tags around the grouped elements. Beware though that this will occur in all places that the 'grouping tag' name occurs - you probably don't want to use the same name for elements as well as attributes. =head2 Handler => object_ref I<# out - SAX only> Use the 'Handler' option to have C generate SAX events rather than returning a string of XML. For more details see L<"SAX SUPPORT"> below. Note: the current implementation of this option generates a string of XML and uses a SAX parser to translate it into SAX events. The normal encoding rules apply here - your data must be UTF8 encoded unless you specify an alternative encoding via the 'XMLDecl' option; and by the time the data reaches the handler object, it will be in UTF8 form regardless of the encoding you supply. A future implementation of this option may generate the events directly. =head2 KeepRoot => 1 I<# in+out - handy> In its attempt to return a data structure free of superfluous detail and unnecessary levels of indirection, C normally discards the root element name. Setting the 'KeepRoot' option to '1' will cause the root element name to be retained. So after executing this code: $config = XMLin('', KeepRoot => 1) You'll be able to reference the tempdir as C<$config-E{config}-E{tempdir}> instead of the default C<$config-E{tempdir}>. Similarly, setting the 'KeepRoot' option to '1' will tell C that the data structure already contains a root element name and it is not necessary to add another. =head2 KeyAttr => [ list ] I<# in+out - important> This option controls the 'array folding' feature which translates nested elements from an array to a hash. It also controls the 'unfolding' of hashes to arrays. For example, this XML: would, by default, parse to this: { 'user' => [ { 'login' => 'grep', 'fullname' => 'Gary R Epstein' }, { 'login' => 'stty', 'fullname' => 'Simon T Tyson' } ] } If the option 'KeyAttr => "login"' were used to specify that the 'login' attribute is a key, the same XML would parse to: { 'user' => { 'stty' => { 'fullname' => 'Simon T Tyson' }, 'grep' => { 'fullname' => 'Gary R Epstein' } } } The key attribute names should be supplied in an arrayref if there is more than one. C will attempt to match attribute names in the order supplied. C will use the first attribute name supplied when 'unfolding' a hash into an array. Note 1: The default value for 'KeyAttr' is ['name', 'key', 'id']. If you do not want folding on input or unfolding on output you must set this option to an empty list to disable the feature. Note 2: If you wish to use this option, you should also enable the C option. Without 'ForceArray', a single nested element will be rolled up into a scalar rather than an array and therefore will not be folded (since only arrays get folded). =head2 KeyAttr => { list } I<# in+out - important> This alternative (and preferred) method of specifying the key attributes allows more fine grained control over which elements are folded and on which attributes. For example the option 'KeyAttr => { package => 'id' } will cause any package elements to be folded on the 'id' attribute. No other elements which have an 'id' attribute will be folded at all. Note: C will generate a warning (or a fatal error in L<"STRICT MODE">) if this syntax is used and an element which does not have the specified key attribute is encountered (eg: a 'package' element without an 'id' attribute, to use the example above). Warnings can be suppressed with the lexical C pragma or C. Two further variations are made possible by prefixing a '+' or a '-' character to the attribute name: The option 'KeyAttr => { user => "+login" }' will cause this XML: to parse to this data structure: { 'user' => { 'stty' => { 'fullname' => 'Simon T Tyson', 'login' => 'stty' }, 'grep' => { 'fullname' => 'Gary R Epstein', 'login' => 'grep' } } } The '+' indicates that the value of the key attribute should be copied rather than moved to the folded hash key. A '-' prefix would produce this result: { 'user' => { 'stty' => { 'fullname' => 'Simon T Tyson', '-login' => 'stty' }, 'grep' => { 'fullname' => 'Gary R Epstein', '-login' => 'grep' } } } As described earlier, C will ignore hash keys starting with a '-'. =head2 NoAttr => 1 I<# in+out - handy> When used with C, the generated XML will contain no attributes. All hash key/values will be represented as nested elements instead. When used with C, any attributes in the XML will be ignored. =head2 NoEscape => 1 I<# out - seldom used> By default, C will translate the characters 'E', 'E', '&' and '"' to '<', '>', '&' and '"' respectively. Use this option to suppress escaping (presumably because you've already escaped the data in some more sophisticated manner). =head2 NoIndent => 1 I<# out - seldom used> Set this option to 1 to disable C's default 'pretty printing' mode. With this option enabled, the XML output will all be on one line (unless there are newlines in the data) - this may be easier for downstream processing. =head2 NoSort => 1 I<# out - seldom used> Newer versions of XML::Simple sort elements and attributes alphabetically (*), by default. Enable this option to suppress the sorting - possibly for backwards compatibility. * Actually, sorting is alphabetical but 'key' attribute or element names (as in 'KeyAttr') sort first. Also, when a hash of hashes is 'unfolded', the elements are sorted alphabetically by the value of the key field. =head2 NormaliseSpace => 0 | 1 | 2 I<# in - handy> This option controls how whitespace in text content is handled. Recognised values for the option are: =over 4 =item * 0 = (default) whitespace is passed through unaltered (except of course for the normalisation of whitespace in attribute values which is mandated by the XML recommendation) =item * 1 = whitespace is normalised in any value used as a hash key (normalising means removing leading and trailing whitespace and collapsing sequences of whitespace characters to a single space) =item * 2 = whitespace is normalised in all text content =back Note: you can spell this option with a 'z' if that is more natural for you. =head2 NSExpand => 1 I<# in+out handy - SAX only> This option controls namespace expansion - the translation of element and attribute names of the form 'prefix:name' to '{uri}name'. For example the element name 'xsl:template' might be expanded to: '{http://www.w3.org/1999/XSL/Transform}template'. By default, C will return element names and attribute names exactly as they appear in the XML. Setting this option to 1 will cause all element and attribute names to be expanded to include their namespace prefix. I. This option also controls whether C performs the reverse translation from '{uri}name' back to 'prefix:name'. The default is no translation. If your data contains expanded names, you should set this option to 1 otherwise C will emit XML which is not well formed. I to translate URIs back to prefixes>. =head2 NumericEscape => 0 | 1 | 2 I<# out - handy> Use this option to have 'high' (non-ASCII) characters in your Perl data structure converted to numeric entities (eg: €) in the XML output. Three levels are possible: 0 - default: no numeric escaping (OK if you're writing out UTF8) 1 - only characters above 0xFF are escaped (ie: characters in the 0x80-FF range are not escaped), possibly useful with ISO8859-1 output 2 - all characters above 0x7F are escaped (good for plain ASCII output) =head2 OutputFile => I<# out - handy> The default behaviour of C is to return the XML as a string. If you wish to write the XML to a file, simply supply the filename using the 'OutputFile' option. This option also accepts an IO handle object - especially useful in Perl 5.8.0 and later for output using an encoding other than UTF-8, eg: open my $fh, '>:encoding(iso-8859-1)', $path or die "open($path): $!"; XMLout($ref, OutputFile => $fh); Note, XML::Simple does not require that the object you pass in to the OutputFile option inherits from L - it simply assumes the object supports a C method. =head2 ParserOpts => [ XML::Parser Options ] I<# in - don't use this> I. This option allows you to pass parameters to the constructor of the underlying XML::Parser object (which of course assumes you're not using SAX). =head2 RootName => 'string' I<# out - handy> By default, when C generates XML, the root element will be named 'opt'. This option allows you to specify an alternative name. Specifying either undef or the empty string for the RootName option will produce XML with no root elements. In most cases the resulting XML fragment will not be 'well formed' and therefore could not be read back in by C. Nevertheless, the option has been found to be useful in certain circumstances. =head2 SearchPath => [ list ] I<# in - handy> If you pass C a filename, but the filename include no directory component, you can use this option to specify which directories should be searched to locate the file. You might use this option to search first in the user's home directory, then in a global directory such as /etc. If a filename is provided to C but SearchPath is not defined, the file is assumed to be in the current directory. If the first parameter to C is undefined, the default SearchPath will contain only the directory in which the script itself is located. Otherwise the default SearchPath will be empty. =head2 StrictMode => 1 | 0 I<# in+out seldom used> This option allows you to turn L on or off for a particular call, regardless of whether it was enabled at the time XML::Simple was loaded. =head2 SuppressEmpty => 1 | '' | undef I<# in+out - handy> This option controls what C should do with empty elements (no attributes and no content). The default behaviour is to represent them as empty hashes. Setting this option to a true value (eg: 1) will cause empty elements to be skipped altogether. Setting the option to 'undef' or the empty string will cause empty elements to be represented as the undefined value or the empty string respectively. The latter two alternatives are a little easier to test for in your code than a hash with no keys. The option also controls what C does with undefined values. Setting the option to undef causes undefined values to be output as empty elements (rather than empty attributes), it also suppresses the generation of warnings about undefined values. Setting the option to a true value (eg: 1) causes undefined values to be skipped altogether on output. =head2 ValueAttr => [ names ] I<# in - handy> Use this option to deal elements which always have a single attribute and no content. Eg: Setting C<< ValueAttr => [ 'value' ] >> will cause the above XML to parse to: { colour => 'red', size => 'XXL' } instead of this (the default): { colour => { value => 'red' }, size => { value => 'XXL' } } Note: This form of the ValueAttr option is not compatible with C - since the attribute name is discarded at parse time, the original XML cannot be reconstructed. =head2 ValueAttr => { element => attribute, ... } I<# in+out - handy> This (preferred) form of the ValueAttr option requires you to specify both the element and the attribute names. This is not only safer, it also allows the original XML to be reconstructed by C. Note: You probably don't want to use this option and the NoAttr option at the same time. =head2 Variables => { name => value } I<# in - handy> This option allows variables in the XML to be expanded when the file is read. (there is no facility for putting the variable names back if you regenerate XML using C). A 'variable' is any text of the form C<${name}> which occurs in an attribute value or in the text content of an element. If 'name' matches a key in the supplied hashref, C<${name}> will be replaced with the corresponding value from the hashref. If no matching key is found, the variable will not be replaced. Names must match the regex: C<[\w.]+> (ie: only 'word' characters and dots are allowed). =head2 VarAttr => 'attr_name' I<# in - handy> In addition to the variables defined using C, this option allows variables to be defined in the XML. A variable definition consists of an element with an attribute called 'attr_name' (the value of the C option). The value of the attribute will be used as the variable name and the text content of the element will be used as the value. A variable defined in this way will override a variable defined using the C option. For example: XMLin( ' /usr/local/apache ${prefix} ${exec_prefix}/bin ', VarAttr => 'name', ContentKey => '-content' ); produces the following data structure: { dir => { prefix => '/usr/local/apache', exec_prefix => '/usr/local/apache', bindir => '/usr/local/apache/bin', } } =head2 XMLDecl => 1 or XMLDecl => 'string' I<# out - handy> If you want the output from C to start with the optional XML declaration, simply set the option to '1'. The default XML declaration is: If you want some other string (for example to declare an encoding value), set the value of this option to the complete string you require. =head1 OPTIONAL OO INTERFACE The procedural interface is both simple and convenient however there are a couple of reasons why you might prefer to use the object oriented (OO) interface: =over 4 =item * to define a set of default values which should be used on all subsequent calls to C or C =item * to override methods in B to provide customised behaviour =back The default values for the options described above are unlikely to suit everyone. The OO interface allows you to effectively override B's defaults with your preferred values. It works like this: First create an XML::Simple parser object with your preferred defaults: my $xs = XML::Simple->new(ForceArray => 1, KeepRoot => 1); then call C or C as a method of that object: my $ref = $xs->XMLin($xml); my $xml = $xs->XMLout($ref); You can also specify options when you make the method calls and these values will be merged with the values specified when the object was created. Values specified in a method call take precedence. Note: when called as methods, the C and C routines may be called as C or C. The method names are aliased so the only difference is the aesthetics. =head2 Parsing Methods You can explicitly call one of the following methods rather than rely on the C method automatically determining whether the target to be parsed is a string, a file or a filehandle: =over 4 =item parse_string(text) Works exactly like the C method but assumes the first argument is a string of XML (or a reference to a scalar containing a string of XML). =item parse_file(filename) Works exactly like the C method but assumes the first argument is the name of a file containing XML. =item parse_fh(file_handle) Works exactly like the C method but assumes the first argument is a filehandle which can be read to get XML. =back =head2 Hook Methods You can make your own class which inherits from XML::Simple and overrides certain behaviours. The following methods may provide useful 'hooks' upon which to hang your modified behaviour. You may find other undocumented methods by examining the source, but those may be subject to change in future releases. =over 4 =item new_xml_parser() This method will be called when a new XML::Parser object must be constructed (either because XML::SAX is not installed or XML::Parser is preferred). =item handle_options(direction, name => value ...) This method will be called when one of the parsing methods or the C method is called. The initial argument will be a string (either 'in' or 'out') and the remaining arguments will be name value pairs. =item default_config_file() Calculates and returns the name of the file which should be parsed if no filename is passed to C (default: C<$0.xml>). =item build_simple_tree(filename, string) Called from C or any of the parsing methods. Takes either a file name as the first argument or C followed by a 'string' as the second argument. Returns a simple tree data structure. You could override this method to apply your own transformations before the data structure is returned to the caller. =item new_hashref() When the 'simple tree' data structure is being built, this method will be called to create any required anonymous hashrefs. =item sorted_keys(name, hashref) Called when C is translating a hashref to XML. This routine returns a list of hash keys in the order that the corresponding attributes/elements should appear in the output. =item escape_value(string) Called from C, takes a string and returns a copy of the string with XML character escaping rules applied. =item escape_attr(string) Called from C, to handle attribute values. By default, just calls C, but you can override this method if you want attributes escaped differently than text content. =item numeric_escape(string) Called from C, to handle non-ASCII characters (depending on the value of the NumericEscape option). =item copy_hash(hashref, extra_key => value, ...) Called from C, when 'unfolding' a hash of hashes into an array of hashes. You might wish to override this method if you're using tied hashes and don't want them to get untied. =back =head2 Cache Methods XML::Simple implements three caching schemes ('storable', 'memshare' and 'memcopy'). You can implement a custom caching scheme by implementing two methods - one for reading from the cache and one for writing to it. For example, you might implement a new 'dbm' scheme that stores cached data structures using the L module. First, you would add a C method which accepted a filename for use as a lookup key and returned a data structure on success, or undef on failure. Then, you would implement a C method which accepted a data structure and a filename. You would use this caching scheme by specifying the option: Cache => [ 'dbm' ] =head1 STRICT MODE If you import the B routines like this: use XML::Simple qw(:strict); the following common mistakes will be detected and treated as fatal errors =over 4 =item * Failing to explicitly set the C option - if you can't be bothered reading about this option, turn it off with: KeyAttr => [ ] =item * Failing to explicitly set the C option - if you can't be bothered reading about this option, set it to the safest mode with: ForceArray => 1 =item * Setting ForceArray to an array, but failing to list all the elements from the KeyAttr hash. =item * Data error - KeyAttr is set to say { part => 'partnum' } but the XML contains one or more EpartE elements without a 'partnum' attribute (or nested element). Note: if strict mode is not set but C is in force, this condition triggers a warning. =item * Data error - as above, but non-unique values are present in the key attribute (eg: more than one EpartE element with the same partnum). This will also trigger a warning if strict mode is not enabled. =item * Data error - as above, but value of key attribute (eg: partnum) is not a scalar string (due to nested elements etc). This will also trigger a warning if strict mode is not enabled. =back =head1 SAX SUPPORT From version 1.08_01, B includes support for SAX (the Simple API for XML) - specifically SAX2. In a typical SAX application, an XML parser (or SAX 'driver') module generates SAX events (start of element, character data, end of element, etc) as it parses an XML document and a 'handler' module processes the events to extract the required data. This simple model allows for some interesting and powerful possibilities: =over 4 =item * Applications written to the SAX API can extract data from huge XML documents without the memory overheads of a DOM or tree API. =item * The SAX API allows for plug and play interchange of parser modules without having to change your code to fit a new module's API. A number of SAX parsers are available with capabilities ranging from extreme portability to blazing performance. =item * A SAX 'filter' module can implement both a handler interface for receiving data and a generator interface for passing modified data on to a downstream handler. Filters can be chained together in 'pipelines'. =item * One filter module might split a data stream to direct data to two or more downstream handlers. =item * Generating SAX events is not the exclusive preserve of XML parsing modules. For example, a module might extract data from a relational database using DBI and pass it on to a SAX pipeline for filtering and formatting. =back B can operate at either end of a SAX pipeline. For example, you can take a data structure in the form of a hashref and pass it into a SAX pipeline using the 'Handler' option on C: use XML::Simple; use Some::SAX::Filter; use XML::SAX::Writer; my $ref = { .... # your data here }; my $writer = XML::SAX::Writer->new(); my $filter = Some::SAX::Filter->new(Handler => $writer); my $simple = XML::Simple->new(Handler => $filter); $simple->XMLout($ref); You can also put B at the opposite end of the pipeline to take advantage of the simple 'tree' data structure once the relevant data has been isolated through filtering: use XML::SAX; use Some::SAX::Filter; use XML::Simple; my $simple = XML::Simple->new(ForceArray => 1, KeyAttr => ['partnum']); my $filter = Some::SAX::Filter->new(Handler => $simple); my $parser = XML::SAX::ParserFactory->parser(Handler => $filter); my $ref = $parser->parse_uri('some_huge_file.xml'); print $ref->{part}->{'555-1234'}; You can build a filter by using an XML::Simple object as a handler and setting its DataHandler option to point to a routine which takes the resulting tree, modifies it and sends it off as SAX events to a downstream handler: my $writer = XML::SAX::Writer->new(); my $filter = XML::Simple->new( DataHandler => sub { my $simple = shift; my $data = shift; # Modify $data here $simple->XMLout($data, Handler => $writer); } ); my $parser = XML::SAX::ParserFactory->parser(Handler => $filter); $parser->parse_uri($filename); I but it could also have been specified in the constructor>. =head1 ENVIRONMENT If you don't care which parser module B uses then skip this section entirely (it looks more complicated than it really is). B will default to using a B parser if one is available or B if SAX is not available. You can dictate which parser module is used by setting either the environment variable 'XML_SIMPLE_PREFERRED_PARSER' or the package variable $XML::Simple::PREFERRED_PARSER to contain the module name. The following rules are used: =over 4 =item * The package variable takes precedence over the environment variable if both are defined. To force B to ignore the environment settings and use its default rules, you can set the package variable to an empty string. =item * If the 'preferred parser' is set to the string 'XML::Parser', then L will be used (or C will die if L is not installed). =item * If the 'preferred parser' is set to some other value, then it is assumed to be the name of a SAX parser module and is passed to L. If L is not installed, or the requested parser module is not installed, then C will die. =item * If the 'preferred parser' is not defined at all (the normal default state), an attempt will be made to load L. If L is installed, then a parser module will be selected according to L's normal rules (which typically means the last SAX parser installed). =item * if the 'preferred parser' is not defined and B is not installed, then B will be used. C will die if L is not installed. =back Note: The B distribution includes an XML parser written entirely in Perl. It is very portable but it is not very fast. You should consider installing L or L if they are available for your platform. =head1 ERROR HANDLING The XML standard is very clear on the issue of non-compliant documents. An error in parsing any single element (for example a missing end tag) must cause the whole document to be rejected. B will die with an appropriate message if it encounters a parsing error. If dying is not appropriate for your application, you should arrange to call C in an eval block and look for errors in $@. eg: my $config = eval { XMLin() }; PopUpMessage($@) if($@); Note, there is a common misconception that use of B will significantly slow down a script. While that may be true when the code being eval'd is in a string, it is not true of code like the sample above. =head1 EXAMPLES When C reads the following very simple piece of XML: it returns the following data structure: { 'username' => 'testuser', 'password' => 'frodo' } The identical result could have been produced with this alternative XML: Or this (although see 'ForceArray' option for variations): testuser frodo Repeated nested elements are represented as anonymous arrays: joe@smith.com jsmith@yahoo.com bob@smith.com { 'person' => [ { 'email' => [ 'joe@smith.com', 'jsmith@yahoo.com' ], 'firstname' => 'Joe', 'lastname' => 'Smith' }, { 'email' => 'bob@smith.com', 'firstname' => 'Bob', 'lastname' => 'Smith' } ] } Nested elements with a recognised key attribute are transformed (folded) from an array into a hash keyed on the value of that attribute (see the C option): { 'person' => { 'jbloggs' => { 'firstname' => 'Joe', 'lastname' => 'Bloggs' }, 'tsmith' => { 'firstname' => 'Tom', 'lastname' => 'Smith' }, 'jsmith' => { 'firstname' => 'Joe', 'lastname' => 'Smith' } } } The tag can be used to form anonymous arrays: Col 1Col 2Col 3 R1C1R1C2R1C3 R2C1R2C2R2C3 R3C1R3C2R3C3 { 'head' => [ [ 'Col 1', 'Col 2', 'Col 3' ] ], 'data' => [ [ 'R1C1', 'R1C2', 'R1C3' ], [ 'R2C1', 'R2C2', 'R2C3' ], [ 'R3C1', 'R3C2', 'R3C3' ] ] } Anonymous arrays can be nested to arbitrary levels and as a special case, if the surrounding tags for an XML document contain only an anonymous array the arrayref will be returned directly rather than the usual hashref: Col 1Col 2 R1C1R1C2 R2C1R2C2 [ [ 'Col 1', 'Col 2' ], [ 'R1C1', 'R1C2' ], [ 'R2C1', 'R2C2' ] ] Elements which only contain text content will simply be represented as a scalar. Where an element has both attributes and text content, the element will be represented as a hashref with the text content in the 'content' key (see the C option): first second { 'one' => 'first', 'two' => { 'attr' => 'value', 'content' => 'second' } } Mixed content (elements which contain both text content and nested elements) will be not be represented in a useful way - element order and significant whitespace will be lost. If you need to work with mixed content, then XML::Simple is not the right tool for your job - check out the next section. =head1 WHERE TO FROM HERE? B is able to present a simple API because it makes some assumptions on your behalf. These include: =over 4 =item * You're not interested in text content consisting only of whitespace =item * You don't mind that when things get slurped into a hash the order is lost =item * You don't want fine-grained control of the formatting of generated XML =item * You would never use a hash key that was not a legal XML element name =item * You don't need help converting between different encodings =back In a serious XML project, you'll probably outgrow these assumptions fairly quickly. This section of the document used to offer some advice on choosing a more powerful option. That advice has now grown into the 'Perl-XML FAQ' document which you can find at: L The advice in the FAQ boils down to a quick explanation of tree versus event based parsers and then recommends: For event based parsing, use SAX (do not set out to write any new code for XML::Parser's handler API - it is obsolete). For tree-based parsing, you could choose between the 'Perlish' approach of L and more standards based DOM implementations - preferably one with XPath support such as L. =head1 SEE ALSO B requires either L or L. To generate documents with namespaces, L is required. The optional caching functions require L. Answers to Frequently Asked Questions about XML::Simple are bundled with this distribution as: L =head1 COPYRIGHT Copyright 1999-2004 Grant McLean Egrantm@cpan.orgE This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut usr/share/perl5/vendor_perl/Test/Simple.pm000064400000014534152351531760014552 0ustar00package Test::Simple; use 5.006; use strict; our $VERSION = '1.302135'; use Test::Builder::Module; our @ISA = qw(Test::Builder::Module); our @EXPORT = qw(ok); my $CLASS = __PACKAGE__; =head1 NAME Test::Simple - Basic utilities for writing tests. =head1 SYNOPSIS use Test::Simple tests => 1; ok( $foo eq $bar, 'foo is bar' ); =head1 DESCRIPTION ** If you are unfamiliar with testing B first!> ** This is an extremely simple, extremely basic module for writing tests suitable for CPAN modules and other pursuits. If you wish to do more complicated testing, use the Test::More module (a drop-in replacement for this one). The basic unit of Perl testing is the ok. For each thing you want to test your program will print out an "ok" or "not ok" to indicate pass or fail. You do this with the C function (see below). The only other constraint is you must pre-declare how many tests you plan to run. This is in case something goes horribly wrong during the test and your test program aborts, or skips a test or whatever. You do this like so: use Test::Simple tests => 23; You must have a plan. =over 4 =item B ok( $foo eq $bar, $name ); ok( $foo eq $bar ); C is given an expression (in this case C<$foo eq $bar>). If it's true, the test passed. If it's false, it didn't. That's about it. C prints out either "ok" or "not ok" along with a test number (it keeps track of that for you). # This produces "ok 1 - Hell not yet frozen over" (or not ok) ok( get_temperature($hell) > 0, 'Hell not yet frozen over' ); If you provide a $name, that will be printed along with the "ok/not ok" to make it easier to find your test when if fails (just search for the name). It also makes it easier for the next guy to understand what your test is for. It's highly recommended you use test names. All tests are run in scalar context. So this: ok( @stuff, 'I have some stuff' ); will do what you mean (fail if stuff is empty) =cut sub ok ($;$) { ## no critic (Subroutines::ProhibitSubroutinePrototypes) return $CLASS->builder->ok(@_); } =back Test::Simple will start by printing number of tests run in the form "1..M" (so "1..5" means you're going to run 5 tests). This strange format lets L know how many tests you plan on running in case something goes horribly wrong. If all your tests passed, Test::Simple will exit with zero (which is normal). If anything failed it will exit with how many failed. If you run less (or more) tests than you planned, the missing (or extras) will be considered failures. If no tests were ever run Test::Simple will throw a warning and exit with 255. If the test died, even after having successfully completed all its tests, it will still be considered a failure and will exit with 255. So the exit codes are... 0 all tests successful 255 test died or all passed but wrong # of tests run any other number how many failed (including missing or extras) If you fail more than 254 tests, it will be reported as 254. This module is by no means trying to be a complete testing system. It's just to get you started. Once you're off the ground its recommended you look at L. =head1 EXAMPLE Here's an example of a simple .t file for the fictional Film module. use Test::Simple tests => 5; use Film; # What you're testing. my $btaste = Film->new({ Title => 'Bad Taste', Director => 'Peter Jackson', Rating => 'R', NumExplodingSheep => 1 }); ok( defined($btaste) && ref $btaste eq 'Film', 'new() works' ); ok( $btaste->Title eq 'Bad Taste', 'Title() get' ); ok( $btaste->Director eq 'Peter Jackson', 'Director() get' ); ok( $btaste->Rating eq 'R', 'Rating() get' ); ok( $btaste->NumExplodingSheep == 1, 'NumExplodingSheep() get' ); It will produce output like this: 1..5 ok 1 - new() works ok 2 - Title() get ok 3 - Director() get not ok 4 - Rating() get # Failed test 'Rating() get' # in t/film.t at line 14. ok 5 - NumExplodingSheep() get # Looks like you failed 1 tests of 5 Indicating the Film::Rating() method is broken. =head1 CAVEATS Test::Simple will only report a maximum of 254 failures in its exit code. If this is a problem, you probably have a huge test script. Split it into multiple files. (Otherwise blame the Unix folks for using an unsigned short integer as the exit status). Because VMS's exit codes are much, much different than the rest of the universe, and perl does horrible mangling to them that gets in my way, it works like this on VMS. 0 SS$_NORMAL all tests successful 4 SS$_ABORT something went wrong Unfortunately, I can't differentiate any further. =head1 NOTES Test::Simple is B tested all the way back to perl 5.6.0. Test::Simple is thread-safe in perl 5.8.1 and up. =head1 HISTORY This module was conceived while talking with Tony Bowden in his kitchen one night about the problems I was having writing some really complicated feature into the new Testing module. He observed that the main problem is not dealing with these edge cases but that people hate to write tests B. What was needed was a dead simple module that took all the hard work out of testing and was really, really easy to learn. Paul Johnson simultaneously had this idea (unfortunately, he wasn't in Tony's kitchen). This is it. =head1 SEE ALSO =over 4 =item L More testing functions! Once you outgrow Test::Simple, look at L. Test::Simple is 100% forward compatible with L (i.e. you can just use L instead of Test::Simple in your programs and things will still work). =back Look in L's SEE ALSO for more testing modules. =head1 AUTHORS Idea by Tony Bowden and Paul Johnson, code by Michael G Schwern Eschwern@pobox.comE, wardrobe by Calvin Klein. =head1 MAINTAINERS =over 4 =item Chad Granum Eexodist@cpan.orgE =back =head1 COPYRIGHT Copyright 2001-2008 by Michael G Schwern Eschwern@pobox.comE. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See F =cut 1; usr/share/perl5/vendor_perl/Pod/Simple.pm000064400000151247152351717640014363 0ustar00 require 5; package Pod::Simple; use strict; use Carp (); BEGIN { *DEBUG = sub () {0} unless defined &DEBUG } use integer; use Pod::Escapes 1.04 (); use Pod::Simple::LinkSection (); use Pod::Simple::BlackBox (); #use utf8; use vars qw( $VERSION @ISA @Known_formatting_codes @Known_directives %Known_formatting_codes %Known_directives $NL ); @ISA = ('Pod::Simple::BlackBox'); $VERSION = '3.35'; @Known_formatting_codes = qw(I B C L E F S X Z); %Known_formatting_codes = map(($_=>1), @Known_formatting_codes); @Known_directives = qw(head1 head2 head3 head4 item over back); %Known_directives = map(($_=>'Plain'), @Known_directives); $NL = $/ unless defined $NL; #----------------------------------------------------------------------------- # Set up some constants: BEGIN { if(defined &ASCII) { } elsif(chr(65) eq 'A') { *ASCII = sub () {1} } else { *ASCII = sub () {''} } unless(defined &MANY_LINES) { *MANY_LINES = sub () {20} } DEBUG > 4 and print STDERR "MANY_LINES is ", MANY_LINES(), "\n"; unless(MANY_LINES() >= 1) { die "MANY_LINES is too small (", MANY_LINES(), ")!\nAborting"; } if(defined &UNICODE) { } elsif($] >= 5.008) { *UNICODE = sub() {1} } else { *UNICODE = sub() {''} } } if(DEBUG > 2) { print STDERR "# We are ", ASCII ? '' : 'not ', "in ASCII-land\n"; print STDERR "# We are under a Unicode-safe Perl.\n"; } # The NO BREAK SPACE and SOFT HYHPEN are used in several submodules. if ($] ge 5.007_003) { # On sufficiently modern Perls we can handle any # character set $Pod::Simple::nbsp = chr utf8::unicode_to_native(0xA0); $Pod::Simple::shy = chr utf8::unicode_to_native(0xAD); } elsif (Pod::Simple::ASCII) { # Hard code ASCII early Perl $Pod::Simple::nbsp = "\xA0"; $Pod::Simple::shy = "\xAD"; } else { # EBCDIC on early Perl. We know what the values are for the code # pages supported then. $Pod::Simple::nbsp = "\x41"; $Pod::Simple::shy = "\xCA"; } # Design note: # This is a parser for Pod. It is not a parser for the set of Pod-like # languages which happens to contain Pod -- it is just for Pod, plus possibly # some extensions. # @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ #@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ __PACKAGE__->_accessorize( 'nbsp_for_S', # Whether to map S<...>'s to \xA0 characters 'source_filename', # Filename of the source, for use in warnings 'source_dead', # Whether to consider this parser's source dead 'output_fh', # The filehandle we're writing to, if applicable. # Used only in some derived classes. 'hide_line_numbers', # For some dumping subclasses: whether to pointedly # suppress the start_line attribute 'line_count', # the current line number 'pod_para_count', # count of pod paragraphs seen so far 'no_whining', # whether to suppress whining 'no_errata_section', # whether to suppress the errata section 'complain_stderr', # whether to complain to stderr 'doc_has_started', # whether we've fired the open-Document event yet 'bare_output', # For some subclasses: whether to prepend # header-code and postpend footer-code 'keep_encoding_directive', # whether to emit =encoding 'nix_X_codes', # whether to ignore X<...> codes 'merge_text', # whether to avoid breaking a single piece of # text up into several events 'preserve_whitespace', # whether to try to keep whitespace as-is 'strip_verbatim_indent', # What indent to strip from verbatim 'parse_characters', # Whether parser should expect chars rather than octets 'content_seen', # whether we've seen any real Pod content 'errors_seen', # TODO: document. whether we've seen any errors (fatal or not) 'codes_in_verbatim', # for PseudoPod extensions 'code_handler', # coderef to call when a code (non-pod) line is seen 'cut_handler', # ... when a =cut line is seen 'pod_handler', # ... when a =pod line is seen 'whiteline_handler', # ... when a line with only whitespace is seen #Called like: # $code_handler->($line, $self->{'line_count'}, $self) if $code_handler; # $cut_handler->($line, $self->{'line_count'}, $self) if $cut_handler; # $pod_handler->($line, $self->{'line_count'}, $self) if $pod_handler; # $wl_handler->($line, $self->{'line_count'}, $self) if $wl_handler; 'parse_empty_lists', # whether to acknowledge empty =over/=back blocks 'raw_mode', # to report entire raw lines instead of Pod elements ); #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ sub any_errata_seen { # good for using as an exit() value... return shift->{'errors_seen'} || 0; } sub errata_seen { return shift->{'all_errata'} || {}; } # Returns the encoding only if it was recognized as being handled and set sub detected_encoding { return shift->{'detected_encoding'}; } sub encoding { my $this = shift; return $this->{'encoding'} unless @_; # GET. $this->_handle_encoding_line("=encoding $_[0]"); if ($this->{'_processed_encoding'}) { delete $this->{'_processed_encoding'}; if(! $this->{'encoding_command_statuses'} ) { DEBUG > 2 and print STDERR " CRAZY ERROR: encoding wasn't really handled?!\n"; } elsif( $this->{'encoding_command_statuses'}[-1] ) { $this->scream( "=encoding $_[0]", sprintf "Couldn't do %s: %s", $this->{'encoding_command_reqs' }[-1], $this->{'encoding_command_statuses'}[-1], ); } else { DEBUG > 2 and print STDERR " (encoding successfully handled.)\n"; } return $this->{'encoding'}; } else { return undef; } } #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ # Pull in some functions that, for some reason, I expect to see here too: BEGIN { *pretty = \&Pod::Simple::BlackBox::pretty; *stringify_lol = \&Pod::Simple::BlackBox::stringify_lol; } #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ sub version_report { my $class = ref($_[0]) || $_[0]; if($class eq __PACKAGE__) { return "$class $VERSION"; } else { my $v = $class->VERSION; return "$class $v (" . __PACKAGE__ . " $VERSION)"; } } #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ #sub curr_open { # read-only list accessor # return @{ $_[0]{'curr_open'} || return() }; #} #sub _curr_open_listref { $_[0]{'curr_open'} ||= [] } sub output_string { # Works by faking out output_fh. Simplifies our code. # my $this = shift; return $this->{'output_string'} unless @_; # GET. require Pod::Simple::TiedOutFH; my $x = (defined($_[0]) and ref($_[0])) ? $_[0] : \( $_[0] ); $$x = '' unless defined $$x; DEBUG > 4 and print STDERR "# Output string set to $x ($$x)\n"; $this->{'output_fh'} = Pod::Simple::TiedOutFH->handle_on($_[0]); return $this->{'output_string'} = $_[0]; #${ ${ $this->{'output_fh'} } }; } sub abandon_output_string { $_[0]->abandon_output_fh; delete $_[0]{'output_string'} } sub abandon_output_fh { $_[0]->output_fh(undef) } # These don't delete the string or close the FH -- they just delete our # references to it/them. # TODO: document these #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ sub new { # takes no parameters my $class = ref($_[0]) || $_[0]; #Carp::croak(__PACKAGE__ . " is a virtual base class -- see perldoc " # . __PACKAGE__ ); return bless { 'accept_codes' => { map( ($_=>$_), @Known_formatting_codes ) }, 'accept_directives' => { %Known_directives }, 'accept_targets' => {}, }, $class; } # TODO: an option for whether to interpolate E<...>'s, or just resolve to codes. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ sub _handle_element_start { # OVERRIDE IN DERIVED CLASS my($self, $element_name, $attr_hash_r) = @_; return; } sub _handle_element_end { # OVERRIDE IN DERIVED CLASS my($self, $element_name) = @_; return; } sub _handle_text { # OVERRIDE IN DERIVED CLASS my($self, $text) = @_; return; } #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ # # And now directives (not targets) sub accept_directive_as_verbatim { shift->_accept_directives('Verbatim', @_) } sub accept_directive_as_data { shift->_accept_directives('Data', @_) } sub accept_directive_as_processed { shift->_accept_directives('Plain', @_) } sub _accept_directives { my($this, $type) = splice @_,0,2; foreach my $d (@_) { next unless defined $d and length $d; Carp::croak "\"$d\" isn't a valid directive name" unless $d =~ m/^[a-zA-Z][a-zA-Z0-9]*$/s; Carp::croak "\"$d\" is already a reserved Pod directive name" if exists $Known_directives{$d}; $this->{'accept_directives'}{$d} = $type; DEBUG > 2 and print STDERR "Learning to accept \"=$d\" as directive of type $type\n"; } DEBUG > 6 and print STDERR "$this\'s accept_directives : ", pretty($this->{'accept_directives'}), "\n"; return sort keys %{ $this->{'accept_directives'} } if wantarray; return; } #-------------------------------------------------------------------------- # TODO: document these: sub unaccept_directive { shift->unaccept_directives(@_) }; sub unaccept_directives { my $this = shift; foreach my $d (@_) { next unless defined $d and length $d; Carp::croak "\"$d\" isn't a valid directive name" unless $d =~ m/^[a-zA-Z][a-zA-Z0-9]*$/s; Carp::croak "But you must accept \"$d\" directives -- it's a builtin!" if exists $Known_directives{$d}; delete $this->{'accept_directives'}{$d}; DEBUG > 2 and print STDERR "OK, won't accept \"=$d\" as directive.\n"; } return sort keys %{ $this->{'accept_directives'} } if wantarray; return } #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ # # And now targets (not directives) sub accept_target { shift->accept_targets(@_) } # alias sub accept_target_as_text { shift->accept_targets_as_text(@_) } # alias sub accept_targets { shift->_accept_targets('1', @_) } sub accept_targets_as_text { shift->_accept_targets('force_resolve', @_) } # forces them to be processed, even when there's no ":". sub _accept_targets { my($this, $type) = splice @_,0,2; foreach my $t (@_) { next unless defined $t and length $t; # TODO: enforce some limitations on what a target name can be? $this->{'accept_targets'}{$t} = $type; DEBUG > 2 and print STDERR "Learning to accept \"$t\" as target of type $type\n"; } return sort keys %{ $this->{'accept_targets'} } if wantarray; return; } #-------------------------------------------------------------------------- sub unaccept_target { shift->unaccept_targets(@_) } sub unaccept_targets { my $this = shift; foreach my $t (@_) { next unless defined $t and length $t; # TODO: enforce some limitations on what a target name can be? delete $this->{'accept_targets'}{$t}; DEBUG > 2 and print STDERR "OK, won't accept \"$t\" as target.\n"; } return sort keys %{ $this->{'accept_targets'} } if wantarray; return; } #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ # # And now codes (not targets or directives) # XXX Probably it is an error that the digit '9' is excluded from these re's. # Broken for early Perls on EBCDIC my $xml_name_re = eval "qr/[^-.0-8:A-Z_a-z[:^ascii:]]/"; if (! defined $xml_name_re) { $xml_name_re = qr/[\x00-\x2C\x2F\x39\x3B-\x40\x5B-\x5E\x60\x7B-\x7F]/; } sub accept_code { shift->accept_codes(@_) } # alias sub accept_codes { # Add some codes my $this = shift; foreach my $new_code (@_) { next unless defined $new_code and length $new_code; # A good-enough check that it's good as an XML Name symbol: Carp::croak "\"$new_code\" isn't a valid element name" if $new_code =~ $xml_name_re # Characters under 0x80 that aren't legal in an XML Name. or $new_code =~ m/^[-\.0-9]/s or $new_code =~ m/:[-\.0-9]/s; # The legal under-0x80 Name characters that # an XML Name still can't start with. $this->{'accept_codes'}{$new_code} = $new_code; # Yes, map to itself -- just so that when we # see "=extend W [whatever] thatelementname", we say that W maps # to whatever $this->{accept_codes}{thatelementname} is, # i.e., "thatelementname". Then when we go re-mapping, # a "W" in the treelet turns into "thatelementname". We only # remap once. # If we say we accept "W", then a "W" in the treelet simply turns # into "W". } return; } #-------------------------------------------------------------------------- sub unaccept_code { shift->unaccept_codes(@_) } sub unaccept_codes { # remove some codes my $this = shift; foreach my $new_code (@_) { next unless defined $new_code and length $new_code; # A good-enough check that it's good as an XML Name symbol: Carp::croak "\"$new_code\" isn't a valid element name" if $new_code =~ $xml_name_re # Characters under 0x80 that aren't legal in an XML Name. or $new_code =~ m/^[-\.0-9]/s or $new_code =~ m/:[-\.0-9]/s; # The legal under-0x80 Name characters that # an XML Name still can't start with. Carp::croak "But you must accept \"$new_code\" codes -- it's a builtin!" if grep $new_code eq $_, @Known_formatting_codes; delete $this->{'accept_codes'}{$new_code}; DEBUG > 2 and print STDERR "OK, won't accept the code $new_code<...>.\n"; } return; } #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ sub parse_string_document { my $self = shift; my @lines; foreach my $line_group (@_) { next unless defined $line_group and length $line_group; pos($line_group) = 0; while($line_group =~ m/([^\n\r]*)(\r?\n?)/g # supports \r, \n ,\r\n #m/([^\n\r]*)((?:\r?\n)?)/g ) { #print(">> $1\n"), $self->parse_lines($1) if length($1) or length($2) or pos($line_group) != length($line_group); # I.e., unless it's a zero-length "empty line" at the very # end of "foo\nbar\n" (i.e., between the \n and the EOS). } } $self->parse_lines(undef); # to signal EOF return $self; } sub _init_fh_source { my($self, $source) = @_; #DEBUG > 1 and print STDERR "Declaring $source as :raw for starters\n"; #$self->_apply_binmode($source, ':raw'); #binmode($source, ":raw"); return; } #:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:. # sub parse_file { my($self, $source) = (@_); if(!defined $source) { Carp::croak("Can't use empty-string as a source for parse_file"); } elsif(ref(\$source) eq 'GLOB') { $self->{'source_filename'} = '' . ($source); } elsif(ref $source) { $self->{'source_filename'} = '' . ($source); } elsif(!length $source) { Carp::croak("Can't use empty-string as a source for parse_file"); } else { { local *PODSOURCE; open(PODSOURCE, "<$source") || Carp::croak("Can't open $source: $!"); $self->{'source_filename'} = $source; $source = *PODSOURCE{IO}; } $self->_init_fh_source($source); } # By here, $source is a FH. $self->{'source_fh'} = $source; my($i, @lines); until( $self->{'source_dead'} ) { splice @lines; for($i = MANY_LINES; $i--;) { # read those many lines at a time local $/ = $NL; push @lines, scalar(<$source>); # readline last unless defined $lines[-1]; # but pass thru the undef, which will set source_dead to true } my $at_eof = ! $lines[-1]; # keep track of the undef pop @lines if $at_eof; # silence warnings # be eol agnostic s/\r\n?/\n/g for @lines; # make sure there are only one line elements for parse_lines @lines = split(/(?<=\n)/, join('', @lines)); # push the undef back after popping it to set source_dead to true push @lines, undef if $at_eof; $self->parse_lines(@lines); } delete($self->{'source_fh'}); # so it can be GC'd return $self; } #:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:. sub parse_from_file { # An emulation of Pod::Parser's interface, for the sake of Perldoc. # Basically just a wrapper around parse_file. my($self, $source, $to) = @_; $self = $self->new unless ref($self); # so we tolerate being a class method if(!defined $source) { $source = *STDIN{IO} } elsif(ref(\$source) eq 'GLOB') { # stet } elsif(ref($source) ) { # stet } elsif(!length $source or $source eq '-' or $source =~ m/^<&(?:STDIN|0)$/i ) { $source = *STDIN{IO}; } if(!defined $to) { $self->output_fh( *STDOUT{IO} ); } elsif(ref(\$to) eq 'GLOB') { $self->output_fh( $to ); } elsif(ref($to)) { $self->output_fh( $to ); } elsif(!length $to or $to eq '-' or $to =~ m/^>&?(?:STDOUT|1)$/i ) { $self->output_fh( *STDOUT{IO} ); } elsif($to =~ m/^>&(?:STDERR|2)$/i) { $self->output_fh( *STDERR{IO} ); } else { require Symbol; my $out_fh = Symbol::gensym(); DEBUG and print STDERR "Write-opening to $to\n"; open($out_fh, ">$to") or Carp::croak "Can't write-open $to: $!"; binmode($out_fh) if $self->can('write_with_binmode') and $self->write_with_binmode; $self->output_fh($out_fh); } return $self->parse_file($source); } #----------------------------------------------------------------------------- sub whine { #my($self,$line,$complaint) = @_; my $self = shift(@_); ++$self->{'errors_seen'}; if($self->{'no_whining'}) { DEBUG > 9 and print STDERR "Discarding complaint (at line $_[0]) $_[1]\n because no_whining is on.\n"; return; } push @{$self->{'all_errata'}{$_[0]}}, $_[1]; return $self->_complain_warn(@_) if $self->{'complain_stderr'}; return $self->_complain_errata(@_); } sub scream { # like whine, but not suppressible #my($self,$line,$complaint) = @_; my $self = shift(@_); ++$self->{'errors_seen'}; push @{$self->{'all_errata'}{$_[0]}}, $_[1]; return $self->_complain_warn(@_) if $self->{'complain_stderr'}; return $self->_complain_errata(@_); } sub _complain_warn { my($self,$line,$complaint) = @_; return printf STDERR "%s around line %s: %s\n", $self->{'source_filename'} || 'Pod input', $line, $complaint; } sub _complain_errata { my($self,$line,$complaint) = @_; if( $self->{'no_errata_section'} ) { DEBUG > 9 and print STDERR "Discarding erratum (at line $line) $complaint\n because no_errata_section is on.\n"; } else { DEBUG > 9 and print STDERR "Queuing erratum (at line $line) $complaint\n"; push @{$self->{'errata'}{$line}}, $complaint # for a report to be generated later! } return 1; } #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ sub _get_initial_item_type { # A hack-wrapper here for when you have like "=over\n\n=item 456\n\n" my($self, $para) = @_; return $para->[1]{'~type'} if $para->[1]{'~type'}; return $para->[1]{'~type'} = 'text' if join("\n", @{$para}[2 .. $#$para]) =~ m/^\s*(\d+)\.?\s*$/s and $1 ne '1'; # Else fall thru to the general case: return $self->_get_item_type($para); } sub _get_item_type { # mutates the item!! my($self, $para) = @_; return $para->[1]{'~type'} if $para->[1]{'~type'}; # Otherwise we haven't yet been to this node. Maybe alter it... my $content = join "\n", @{$para}[2 .. $#$para]; if($content =~ m/^\s*\*\s*$/s or $content =~ m/^\s*$/s) { # Like: "=item *", "=item * ", "=item" splice @$para, 2; # so it ends up just being ['=item', { attrhash } ] $para->[1]{'~orig_content'} = $content; return $para->[1]{'~type'} = 'bullet'; } elsif($content =~ m/^\s*\*\s+(.+)/s) { # tolerance # Like: "=item * Foo bar baz"; $para->[1]{'~orig_content'} = $content; $para->[1]{'~_freaky_para_hack'} = $1; DEBUG > 2 and print STDERR " Tolerating $$para[2] as =item *\\n\\n$1\n"; splice @$para, 2; # so it ends up just being ['=item', { attrhash } ] return $para->[1]{'~type'} = 'bullet'; } elsif($content =~ m/^\s*(\d+)\.?\s*$/s) { # Like: "=item 1.", "=item 123412" $para->[1]{'~orig_content'} = $content; $para->[1]{'number'} = $1; # Yes, stores the number there! splice @$para, 2; # so it ends up just being ['=item', { attrhash } ] return $para->[1]{'~type'} = 'number'; } else { # It's anything else. return $para->[1]{'~type'} = 'text'; } } #----------------------------------------------------------------------------- sub _make_treelet { my $self = shift; # and ($para, $start_line) my $treelet; if(!@_) { return ['']; } if(ref $_[0] and ref $_[0][0] and $_[0][0][0] eq '~Top') { # Hack so we can pass in fake-o pre-cooked paragraphs: # just have the first line be a reference to a ['~Top', {}, ...] # We use this feechure in gen_errata and stuff. DEBUG and print STDERR "Applying precooked treelet hack to $_[0][0]\n"; $treelet = $_[0][0]; splice @$treelet, 0, 2; # lop the top off return $treelet; } else { $treelet = $self->_treelet_from_formatting_codes(@_); } if( $self->_remap_sequences($treelet) ) { $self->_treat_Zs($treelet); # Might as well nix these first $self->_treat_Ls($treelet); # L has to precede E and S $self->_treat_Es($treelet); $self->_treat_Ss($treelet); # S has to come after E $self->_wrap_up($treelet); # Nix X's and merge texties } else { DEBUG and print STDERR "Formatless treelet gets fast-tracked.\n"; # Very common case! } splice @$treelet, 0, 2; # lop the top off return $treelet; } #:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:. sub _wrap_up { my($self, @stack) = @_; my $nixx = $self->{'nix_X_codes'}; my $merge = $self->{'merge_text' }; return unless $nixx or $merge; DEBUG > 2 and print STDERR "\nStarting _wrap_up traversal.\n", $merge ? (" Merge mode on\n") : (), $nixx ? (" Nix-X mode on\n") : (), ; my($i, $treelet); while($treelet = shift @stack) { DEBUG > 3 and print STDERR " Considering children of this $treelet->[0] node...\n"; for($i = 2; $i < @$treelet; ++$i) { # iterate over children DEBUG > 3 and print STDERR " Considering child at $i ", pretty($treelet->[$i]), "\n"; if($nixx and ref $treelet->[$i] and $treelet->[$i][0] eq 'X') { DEBUG > 3 and print STDERR " Nixing X node at $i\n"; splice(@$treelet, $i, 1); # just nix this node (and its descendants) # no need to back-update the counter just yet redo; } elsif($merge and $i != 2 and # non-initial !ref $treelet->[$i] and !ref $treelet->[$i - 1] ) { DEBUG > 3 and print STDERR " Merging ", $i-1, ":[$treelet->[$i-1]] and $i\:[$treelet->[$i]]\n"; $treelet->[$i-1] .= ( splice(@$treelet, $i, 1) )[0]; DEBUG > 4 and print STDERR " Now: ", $i-1, ":[$treelet->[$i-1]]\n"; --$i; next; # since we just pulled the possibly last node out from under # ourselves, we can't just redo() } elsif( ref $treelet->[$i] ) { DEBUG > 4 and print STDERR " Enqueuing ", pretty($treelet->[$i]), " for traversal.\n"; push @stack, $treelet->[$i]; if($treelet->[$i][0] eq 'L') { my $thing; foreach my $attrname ('section', 'to') { if(defined($thing = $treelet->[$i][1]{$attrname}) and ref $thing) { unshift @stack, $thing; DEBUG > 4 and print STDERR " +Enqueuing ", pretty( $treelet->[$i][1]{$attrname} ), " as an attribute value to tweak.\n"; } } } } } } DEBUG > 2 and print STDERR "End of _wrap_up traversal.\n\n"; return; } #:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:. sub _remap_sequences { my($self,@stack) = @_; if(@stack == 1 and @{ $stack[0] } == 3 and !ref $stack[0][2]) { # VERY common case: abort it. DEBUG and print STDERR "Skipping _remap_sequences: formatless treelet.\n"; return 0; } my $map = ($self->{'accept_codes'} || die "NO accept_codes in $self?!?"); my $start_line = $stack[0][1]{'start_line'}; DEBUG > 2 and printf "\nAbout to start _remap_sequences on treelet from line %s.\n", $start_line || '[?]' ; DEBUG > 3 and print STDERR " Map: ", join('; ', map "$_=" . ( ref($map->{$_}) ? join(",", @{$map->{$_}}) : $map->{$_} ), sort keys %$map ), ("B~C~E~F~I~L~S~X~Z" eq join '~', sort keys %$map) ? " (all normal)\n" : "\n" ; # A recursive algorithm implemented iteratively! Whee! my($is, $was, $i, $treelet); # scratch while($treelet = shift @stack) { DEBUG > 3 and print STDERR " Considering children of this $treelet->[0] node...\n"; for($i = 2; $i < @$treelet; ++$i) { # iterate over children next unless ref $treelet->[$i]; # text nodes are uninteresting DEBUG > 4 and print STDERR " Noting child $i : $treelet->[$i][0]<...>\n"; $is = $treelet->[$i][0] = $map->{ $was = $treelet->[$i][0] }; if( DEBUG > 3 ) { if(!defined $is) { print STDERR " Code $was<> is UNKNOWN!\n"; } elsif($is eq $was) { DEBUG > 4 and print STDERR " Code $was<> stays the same.\n"; } else { print STDERR " Code $was<> maps to ", ref($is) ? ( "tags ", map("$_<", @$is), '...', map('>', @$is), "\n" ) : "tag $is<...>.\n"; } } if(!defined $is) { $self->whine($start_line, "Deleting unknown formatting code $was<>"); $is = $treelet->[$i][0] = '1'; # But saving the children! # I could also insert a leading "$was<" and tailing ">" as # children of this node, but something about that seems icky. } if(ref $is) { my @dynasty = @$is; DEBUG > 4 and print STDERR " Renaming $was node to $dynasty[-1]\n"; $treelet->[$i][0] = pop @dynasty; my $nugget; while(@dynasty) { DEBUG > 4 and printf " Grafting a new %s node between %s and %s\n", $dynasty[-1], $treelet->[0], $treelet->[$i][0], ; #$nugget = ; splice @$treelet, $i, 1, [pop(@dynasty), {}, $treelet->[$i]]; # relace node with a new parent } } elsif($is eq '0') { splice(@$treelet, $i, 1); # just nix this node (and its descendants) --$i; # back-update the counter } elsif($is eq '1') { splice(@$treelet, $i, 1 # replace this node with its children! => splice @{ $treelet->[$i] },2 # (not catching its first two (non-child) items) ); --$i; # back up for new stuff } else { # otherwise it's unremarkable unshift @stack, $treelet->[$i]; # just recurse } } } DEBUG > 2 and print STDERR "End of _remap_sequences traversal.\n\n"; if(@_ == 2 and @{ $_[1] } == 3 and !ref $_[1][2]) { DEBUG and print STDERR "Noting that the treelet is now formatless.\n"; return 0; } return 1; } # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sub _ponder_extend { # "Go to an extreme, move back to a more comfortable place" # -- /Oblique Strategies/, Brian Eno and Peter Schmidt my($self, $para) = @_; my $content = join ' ', splice @$para, 2; $content =~ s/^\s+//s; $content =~ s/\s+$//s; DEBUG > 2 and print STDERR "Ogling extensor: =extend $content\n"; if($content =~ m/^ (\S+) # 1 : new item \s+ (\S+) # 2 : fallback(s) (?:\s+(\S+))? # 3 : element name(s) \s* $ /xs ) { my $new_letter = $1; my $fallbacks_one = $2; my $elements_one; $elements_one = defined($3) ? $3 : $1; DEBUG > 2 and print STDERR "Extensor has good syntax.\n"; unless($new_letter =~ m/^[A-Z]$/s or $new_letter) { DEBUG > 2 and print STDERR " $new_letter isn't a valid thing to entend.\n"; $self->whine( $para->[1]{'start_line'}, "You can extend only formatting codes A-Z, not like \"$new_letter\"" ); return; } if(grep $new_letter eq $_, @Known_formatting_codes) { DEBUG > 2 and print STDERR " $new_letter isn't a good thing to extend, because known.\n"; $self->whine( $para->[1]{'start_line'}, "You can't extend an established code like \"$new_letter\"" ); #TODO: or allow if last bit is same? return; } unless($fallbacks_one =~ m/^[A-Z](,[A-Z])*$/s # like "B", "M,I", etc. or $fallbacks_one eq '0' or $fallbacks_one eq '1' ) { $self->whine( $para->[1]{'start_line'}, "Format for second =extend parameter must be like" . " M or 1 or 0 or M,N or M,N,O but you have it like " . $fallbacks_one ); return; } unless($elements_one =~ m/^[^ ,]+(,[^ ,]+)*$/s) { # like "B", "M,I", etc. $self->whine( $para->[1]{'start_line'}, "Format for third =extend parameter: like foo or bar,Baz,qu:ux but not like " . $elements_one ); return; } my @fallbacks = split ',', $fallbacks_one, -1; my @elements = split ',', $elements_one, -1; foreach my $f (@fallbacks) { next if exists $Known_formatting_codes{$f} or $f eq '0' or $f eq '1'; DEBUG > 2 and print STDERR " Can't fall back on unknown code $f\n"; $self->whine( $para->[1]{'start_line'}, "Can't use unknown formatting code '$f' as a fallback for '$new_letter'" ); return; } DEBUG > 3 and printf STDERR "Extensor: Fallbacks <%s> Elements <%s>.\n", @fallbacks, @elements; my $canonical_form; foreach my $e (@elements) { if(exists $self->{'accept_codes'}{$e}) { DEBUG > 1 and print STDERR " Mapping '$new_letter' to known extension '$e'\n"; $canonical_form = $e; last; # first acceptable elementname wins! } else { DEBUG > 1 and print STDERR " Can't map '$new_letter' to unknown extension '$e'\n"; } } if( defined $canonical_form ) { # We found a good N => elementname mapping $self->{'accept_codes'}{$new_letter} = $canonical_form; DEBUG > 2 and print "Extensor maps $new_letter => known element $canonical_form.\n"; } else { # We have to use the fallback(s), which might be '0', or '1'. $self->{'accept_codes'}{$new_letter} = (@fallbacks == 1) ? $fallbacks[0] : \@fallbacks; DEBUG > 2 and print "Extensor maps $new_letter => fallbacks @fallbacks.\n"; } } else { DEBUG > 2 and print STDERR "Extensor has bad syntax.\n"; $self->whine( $para->[1]{'start_line'}, "Unknown =extend syntax: $content" ) } return; } #:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:. sub _treat_Zs { # Nix Z<...>'s my($self,@stack) = @_; my($i, $treelet); my $start_line = $stack[0][1]{'start_line'}; # A recursive algorithm implemented iteratively! Whee! while($treelet = shift @stack) { for($i = 2; $i < @$treelet; ++$i) { # iterate over children next unless ref $treelet->[$i]; # text nodes are uninteresting unless($treelet->[$i][0] eq 'Z') { unshift @stack, $treelet->[$i]; # recurse next; } DEBUG > 1 and print STDERR "Nixing Z node @{$treelet->[$i]}\n"; # bitch UNLESS it's empty unless( @{$treelet->[$i]} == 2 or (@{$treelet->[$i]} == 3 and $treelet->[$i][2] eq '') ) { $self->whine( $start_line, "A non-empty Z<>" ); } # but kill it anyway splice(@$treelet, $i, 1); # thereby just nix this node. --$i; } } return; } # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # Quoting perlpodspec: # In parsing an L<...> code, Pod parsers must distinguish at least four # attributes: ############# Not used. Expressed via the element children plus ############# the value of the "content-implicit" flag. # First: # The link-text. If there is none, this must be undef. (E.g., in "L", the link-text is "Perl Functions". In # "L" and even "L<|Time::HiRes>", there is no link text. Note # that link text may contain formatting.) # ############# The element children # Second: # The possibly inferred link-text -- i.e., if there was no real link text, # then this is the text that we'll infer in its place. (E.g., for # "L", the inferred link text is "Getopt::Std".) # ############# The "to" attribute (which might be text, or a treelet) # Third: # The name or URL, or undef if none. (E.g., in "L", the name -- also sometimes called the page -- is # "perlfunc". In "L", the name is undef.) # ############# The "section" attribute (which might be next, or a treelet) # Fourth: # The section (AKA "item" in older perlpods), or undef if none. E.g., in # Getopt::Std/DESCRIPTION, "DESCRIPTION" is the section. (Note that this # is not the same as a manpage section like the "5" in "man 5 crontab". # "Section Foo" in the Pod sense means the part of the text that's # introduced by the heading or item whose text is "Foo".) # # Pod parsers may also note additional attributes including: # ############# The "type" attribute. # Fifth: # A flag for whether item 3 (if present) is a URL (like # "http://lists.perl.org" is), in which case there should be no section # attribute; a Pod name (like "perldoc" and "Getopt::Std" are); or # possibly a man page name (like "crontab(5)" is). # ############# The "raw" attribute that is already there. # Sixth: # The raw original L<...> content, before text is split on "|", "/", etc, # and before E<...> codes are expanded. # For L<...> codes without a "name|" part, only E<...> and Z<> codes may # occur -- no other formatting codes. That is, authors should not use # "L>". # # Note, however, that formatting codes and Z<>'s can occur in any and all # parts of an L<...> (i.e., in name, section, text, and url). sub _treat_Ls { # Process our dear dear friends, the L<...> sequences # L # L or L # L or L or L<"sec"> # L # L or L # L or L or L # L # L my($self,@stack) = @_; my($i, $treelet); my $start_line = $stack[0][1]{'start_line'}; # A recursive algorithm implemented iteratively! Whee! while($treelet = shift @stack) { for(my $i = 2; $i < @$treelet; ++$i) { # iterate over children of current tree node next unless ref $treelet->[$i]; # text nodes are uninteresting unless($treelet->[$i][0] eq 'L') { unshift @stack, $treelet->[$i]; # recurse next; } # By here, $treelet->[$i] is definitely an L node my $ell = $treelet->[$i]; DEBUG > 1 and print STDERR "Ogling L node $ell\n"; # bitch if it's empty if( @{$ell} == 2 or (@{$ell} == 3 and $ell->[2] eq '') ) { $self->whine( $start_line, "An empty L<>" ); $treelet->[$i] = 'L<>'; # just make it a text node next; # and move on } if( (! ref $ell->[2] && $ell->[2] =~ /\A\s/) ||(! ref $ell->[-1] && $ell->[-1] =~ /\s\z/) ) { $self->whine( $start_line, "L<> starts or ends with whitespace" ); } # Catch URLs: # there are a number of possible cases: # 1) text node containing url: http://foo.com # -> [ 'http://foo.com' ] # 2) text node containing url and text: foo|http://foo.com # -> [ 'foo|http://foo.com' ] # 3) text node containing url start: mailto:xEfoo.com # -> [ 'mailto:x', [ E ... ], 'foo.com' ] # 4) text node containing url start and text: foo|mailto:xEfoo.com # -> [ 'foo|mailto:x', [ E ... ], 'foo.com' ] # 5) other nodes containing text and url start: OE<39>Malley|http://foo.com # -> [ 'O', [ E ... ], 'Malley', '|http://foo.com' ] # ... etc. # anything before the url is part of the text. # anything after it is part of the url. # the url text node itself may contain parts of both. if (my ($url_index, $text_part, $url_part) = # grep is no good here; we want to bail out immediately so that we can # use $1, $2, etc. without having to do the match twice. sub { for (2..$#$ell) { next if ref $ell->[$_]; next unless $ell->[$_] =~ m/^(?:([^|]*)\|)?(\w+:[^:\s]\S*)$/s; return ($_, $1, $2); } return; }->() ) { $ell->[1]{'type'} = 'url'; my @text = @{$ell}[2..$url_index-1]; push @text, $text_part if defined $text_part; my @url = @{$ell}[$url_index+1..$#$ell]; unshift @url, $url_part; unless (@text) { $ell->[1]{'content-implicit'} = 'yes'; @text = @url; } $ell->[1]{to} = Pod::Simple::LinkSection->new( @url == 1 ? $url[0] : [ '', {}, @url ], ); splice @$ell, 2, $#$ell, @text; next; } # Catch some very simple and/or common cases if(@{$ell} == 3 and ! ref $ell->[2]) { my $it = $ell->[2]; if($it =~ m{^[^/|]+[(][-a-zA-Z0-9]+[)]$}s) { # man sections # Hopefully neither too broad nor too restrictive a RE DEBUG > 1 and print STDERR "Catching \"$it\" as manpage link.\n"; $ell->[1]{'type'} = 'man'; # This's the only place where man links can get made. $ell->[1]{'content-implicit'} = 'yes'; $ell->[1]{'to' } = Pod::Simple::LinkSection->new( $it ); # treelet! next; } if($it =~ m/^[^\/\|,\$\%\@\ \"\<\>\:\#\&\*\{\}\[\]\(\)]+(\:\:[^\/\|,\$\%\@\ \"\<\>\:\#\&\*\{\}\[\]\(\)]+)*$/s) { # Extremely forgiving idea of what constitutes a bare # modulename link like L or even L DEBUG > 1 and print STDERR "Catching \"$it\" as ho-hum L link.\n"; $ell->[1]{'type'} = 'pod'; $ell->[1]{'content-implicit'} = 'yes'; $ell->[1]{'to' } = Pod::Simple::LinkSection->new( $it ); # treelet! next; } # else fall thru... } # ...Uhoh, here's the real L<...> parsing stuff... # "With the ill behavior, with the ill behavior, with the ill behavior..." DEBUG > 1 and print STDERR "Running a real parse on this non-trivial L\n"; my $link_text; # set to an arrayref if found my @ell_content = @$ell; splice @ell_content,0,2; # Knock off the 'L' and {} bits DEBUG > 3 and print STDERR " Ell content to start: ", pretty(@ell_content), "\n"; # Look for the "|" -- only in CHILDREN (not all underlings!) # Like L DEBUG > 3 and print STDERR " Peering at L content for a '|' ...\n"; for(my $j = 0; $j < @ell_content; ++$j) { next if ref $ell_content[$j]; DEBUG > 3 and print STDERR " Peering at L-content text bit \"$ell_content[$j]\" for a '|'.\n"; if($ell_content[$j] =~ m/^([^\|]*)\|(.*)$/s) { my @link_text = ($1); # might be 0-length $ell_content[$j] = $2; # might be 0-length DEBUG > 3 and print STDERR " FOUND a '|' in it. Splitting into [$1] + [$2]\n"; if ($link_text[0] =~ m{[|/]}) { $self->whine( $start_line, "alternative text '$link_text[0]' contains non-escaped | or /" ); } unshift @link_text, splice @ell_content, 0, $j; # leaving only things at J and after @ell_content = grep ref($_)||length($_), @ell_content ; $link_text = [grep ref($_)||length($_), @link_text ]; DEBUG > 3 and printf " So link text is %s\n and remaining ell content is %s\n", pretty($link_text), pretty(@ell_content); last; } } # Now look for the "/" -- only in CHILDREN (not all underlings!) # And afterward, anything left in @ell_content will be the raw name # Like L my $section_name; # set to arrayref if found DEBUG > 3 and print STDERR " Peering at L-content for a '/' ...\n"; for(my $j = 0; $j < @ell_content; ++$j) { next if ref $ell_content[$j]; DEBUG > 3 and print STDERR " Peering at L-content text bit \"$ell_content[$j]\" for a '/'.\n"; if($ell_content[$j] =~ m/^([^\/]*)\/(.*)$/s) { my @section_name = ($2); # might be 0-length $ell_content[$j] = $1; # might be 0-length DEBUG > 3 and print STDERR " FOUND a '/' in it.", " Splitting to page [...$1] + section [$2...]\n"; push @section_name, splice @ell_content, 1+$j; # leaving only things before and including J @ell_content = grep ref($_)||length($_), @ell_content ; @section_name = grep ref($_)||length($_), @section_name ; # Turn L<.../"foo"> into L<.../foo> if(@section_name and !ref($section_name[0]) and !ref($section_name[-1]) and $section_name[ 0] =~ m/^\"/s and $section_name[-1] =~ m/\"$/s and !( # catch weird degenerate case of L<"> ! @section_name == 1 and $section_name[0] eq '"' ) ) { $section_name[ 0] =~ s/^\"//s; $section_name[-1] =~ s/\"$//s; DEBUG > 3 and print STDERR " Quotes removed: ", pretty(@section_name), "\n"; } else { DEBUG > 3 and print STDERR " No need to remove quotes in ", pretty(@section_name), "\n"; } $section_name = \@section_name; last; } } # Turn L<"Foo Bar"> into L if(!$section_name and @ell_content and !ref($ell_content[0]) and !ref($ell_content[-1]) and $ell_content[ 0] =~ m/^\"/s and $ell_content[-1] =~ m/\"$/s and !( # catch weird degenerate case of L<"> ! @ell_content == 1 and $ell_content[0] eq '"' ) ) { $section_name = [splice @ell_content]; $section_name->[ 0] =~ s/^\"//s; $section_name->[-1] =~ s/\"$//s; } # Turn L into L. if(!$section_name and !$link_text and @ell_content and grep !ref($_) && m/ /s, @ell_content ) { $section_name = [splice @ell_content]; # That's support for the now-deprecated syntax. # (Maybe generate a warning eventually?) # Note that it deliberately won't work on L<...|Foo Bar> } # Now make up the link_text # L -> L # L -> L<"Bar"|Bar> # L -> L<"Bar" in Foo/Foo> unless($link_text) { $ell->[1]{'content-implicit'} = 'yes'; $link_text = []; push @$link_text, '"', @$section_name, '"' if $section_name; if(@ell_content) { $link_text->[-1] .= ' in ' if $section_name; push @$link_text, @ell_content; } } # And the E resolver will have to deal with all our treeletty things: if(@ell_content == 1 and !ref($ell_content[0]) and $ell_content[0] =~ m{^[^/]+[(][-a-zA-Z0-9]+[)]$}s ) { $ell->[1]{'type'} = 'man'; DEBUG > 3 and print STDERR "Considering this ($ell_content[0]) a man link.\n"; } else { $ell->[1]{'type'} = 'pod'; DEBUG > 3 and print STDERR "Considering this a pod link (not man or url).\n"; } if( defined $section_name ) { $ell->[1]{'section'} = Pod::Simple::LinkSection->new( ['', {}, @$section_name] ); DEBUG > 3 and print STDERR "L-section content: ", pretty($ell->[1]{'section'}), "\n"; } if( @ell_content ) { $ell->[1]{'to'} = Pod::Simple::LinkSection->new( ['', {}, @ell_content] ); DEBUG > 3 and print STDERR "L-to content: ", pretty($ell->[1]{'to'}), "\n"; } # And update children to be the link-text: @$ell = (@$ell[0,1], defined($link_text) ? splice(@$link_text) : ''); DEBUG > 2 and print STDERR "End of L-parsing for this node $treelet->[$i]\n"; unshift @stack, $treelet->[$i]; # might as well recurse } } return; } # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sub _treat_Es { my($self,@stack) = @_; my($i, $treelet, $content, $replacer, $charnum); my $start_line = $stack[0][1]{'start_line'}; # A recursive algorithm implemented iteratively! Whee! # Has frightening side effects on L nodes' attributes. #my @ells_to_tweak; while($treelet = shift @stack) { for(my $i = 2; $i < @$treelet; ++$i) { # iterate over children next unless ref $treelet->[$i]; # text nodes are uninteresting if($treelet->[$i][0] eq 'L') { # SPECIAL STUFF for semi-processed L<>'s my $thing; foreach my $attrname ('section', 'to') { if(defined($thing = $treelet->[$i][1]{$attrname}) and ref $thing) { unshift @stack, $thing; DEBUG > 2 and print STDERR " Enqueuing ", pretty( $treelet->[$i][1]{$attrname} ), " as an attribute value to tweak.\n"; } } unshift @stack, $treelet->[$i]; # recurse next; } elsif($treelet->[$i][0] ne 'E') { unshift @stack, $treelet->[$i]; # recurse next; } DEBUG > 1 and print STDERR "Ogling E node ", pretty($treelet->[$i]), "\n"; # bitch if it's empty if( @{$treelet->[$i]} == 2 or (@{$treelet->[$i]} == 3 and $treelet->[$i][2] eq '') ) { $self->whine( $start_line, "An empty E<>" ); $treelet->[$i] = 'E<>'; # splice in a literal next; } # bitch if content is weird unless(@{$treelet->[$i]} == 3 and !ref($content = $treelet->[$i][2])) { $self->whine( $start_line, "An E<...> surrounding strange content" ); $replacer = $treelet->[$i]; # scratch splice(@$treelet, $i, 1, # fake out a literal 'E<', splice(@$replacer,2), # promote its content '>' ); # Don't need to do --$i, as the 'E<' we just added isn't interesting. next; } DEBUG > 1 and print STDERR "Ogling E<$content>\n"; # XXX E<>'s contents *should* be a valid char in the scope of the current # =encoding directive. Defaults to iso-8859-1, I believe. Fix this in the # future sometime. $charnum = Pod::Escapes::e2charnum($content); DEBUG > 1 and print STDERR " Considering E<$content> with char ", defined($charnum) ? $charnum : "undef", ".\n"; if(!defined( $charnum )) { DEBUG > 1 and print STDERR "I don't know how to deal with E<$content>.\n"; $self->whine( $start_line, "Unknown E content in E<$content>" ); $replacer = "E<$content>"; # better than nothing } elsif($charnum >= 255 and !UNICODE) { $replacer = ASCII ? "\xA4" : "?"; DEBUG > 1 and print STDERR "This Perl version can't handle ", "E<$content> (chr $charnum), so replacing with $replacer\n"; } else { $replacer = Pod::Escapes::e2char($content); DEBUG > 1 and print STDERR " Replacing E<$content> with $replacer\n"; } splice(@$treelet, $i, 1, $replacer); # no need to back up $i, tho } } return; } # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sub _treat_Ss { my($self,$treelet) = @_; _change_S_to_nbsp($treelet,0) if $self->{'nbsp_for_S'}; # TODO: or a change_nbsp_to_S # Normalizing nbsp's to S is harder: for each text node, make S content # out of anything matching m/([^ \xA0]*(?:\xA0+[^ \xA0]*)+)/ return; } sub _change_S_to_nbsp { # a recursive function # Sanely assumes that the top node in the excursion won't be an S node. my($treelet, $in_s) = @_; my $is_s = ('S' eq $treelet->[0]); $in_s ||= $is_s; # So in_s is on either by this being an S element, # or by an ancestor being an S element. for(my $i = 2; $i < @$treelet; ++$i) { if(ref $treelet->[$i]) { if( _change_S_to_nbsp( $treelet->[$i], $in_s ) ) { my $to_pull_up = $treelet->[$i]; splice @$to_pull_up,0,2; # ...leaving just its content splice @$treelet, $i, 1, @$to_pull_up; # Pull up content $i += @$to_pull_up - 1; # Make $i skip the pulled-up stuff } } else { $treelet->[$i] =~ s/\s/$Pod::Simple::nbsp/g if $in_s; # Note that if you apply nbsp_for_S to text, and so turn # "foo S quux" into "foo bar faz quux", you # end up with something that fails to say "and don't hyphenate # any part of 'bar baz'". However, hyphenation is such a vexing # problem anyway, that most Pod renderers just don't render it # at all. But if you do want to implement hyphenation, I guess # that you'd better have nbsp_for_S off. } } return $is_s; } #----------------------------------------------------------------------------- sub _accessorize { # A simple-minded method-maker no strict 'refs'; foreach my $attrname (@_) { next if $attrname =~ m/::/; # a hack *{caller() . '::' . $attrname} = sub { use strict; $Carp::CarpLevel = 1, Carp::croak( "Accessor usage: \$obj->$attrname() or \$obj->$attrname(\$new_value)" ) unless (@_ == 1 or @_ == 2) and ref $_[0]; (@_ == 1) ? $_[0]->{$attrname} : ($_[0]->{$attrname} = $_[1]); }; } # Ya know, they say accessories make the ensemble! return; } # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . #============================================================================= sub filter { my($class, $source) = @_; my $new = $class->new; $new->output_fh(*STDOUT{IO}); if(ref($source || '') eq 'SCALAR') { $new->parse_string_document( $$source ); } elsif(ref($source)) { # it's a file handle $new->parse_file($source); } else { # it's a filename $new->parse_file($source); } return $new; } #----------------------------------------------------------------------------- sub _out { # For use in testing: Class->_out($source) # returns the transformation of $source my $class = shift(@_); my $mutor = shift(@_) if @_ and ref($_[0] || '') eq 'CODE'; DEBUG and print STDERR "\n\n", '#' x 76, "\nAbout to parse source: {{\n$_[0]\n}}\n\n"; my $parser = ref $class && $class->isa(__PACKAGE__) ? $class : $class->new; $parser->hide_line_numbers(1); my $out = ''; $parser->output_string( \$out ); DEBUG and print STDERR " _out to ", \$out, "\n"; $mutor->($parser) if $mutor; $parser->parse_string_document( $_[0] ); # use Data::Dumper; print STDERR Dumper($parser), "\n"; return $out; } sub _duo { # For use in testing: Class->_duo($source1, $source2) # returns the parse trees of $source1 and $source2. # Good in things like: &ok( Class->duo(... , ...) ); my $class = shift(@_); Carp::croak "But $class->_duo is useful only in list context!" unless wantarray; my $mutor = shift(@_) if @_ and ref($_[0] || '') eq 'CODE'; Carp::croak "But $class->_duo takes two parameters, not: @_" unless @_ == 2; my(@out); while( @_ ) { my $parser = $class->new; push @out, ''; $parser->output_string( \( $out[-1] ) ); DEBUG and print STDERR " _duo out to ", $parser->output_string(), " = $parser->{'output_string'}\n"; $parser->hide_line_numbers(1); $mutor->($parser) if $mutor; $parser->parse_string_document( shift( @_ ) ); # use Data::Dumper; print STDERR Dumper($parser), "\n"; } return @out; } #----------------------------------------------------------------------------- 1; __END__ TODO: A start_formatting_code and end_formatting_code methods, which in the base class call start_L, end_L, start_C, end_C, etc., if they are defined. have the POD FORMATTING ERRORS section note the localtime, and the version of Pod::Simple. option to delete all Es? option to scream if under-0x20 literals are found in the input, or under-E<32> E codes are found in the tree. And ditto \x7f-\x9f Option to turn highbit characters into their compromised form? (applies to E parsing too) TODO: BOM/encoding things. TODO: ascii-compat things in the XML classes? usr/share/perl5/Locale/Maketext/Simple.pm000064400000023100152360423150014253 0ustar00package Locale::Maketext::Simple; $Locale::Maketext::Simple::VERSION = '0.21_01'; use strict; use 5.005; =head1 NAME Locale::Maketext::Simple - Simple interface to Locale::Maketext::Lexicon =head1 VERSION This document describes version 0.18 of Locale::Maketext::Simple, released Septermber 8, 2006. =head1 SYNOPSIS Minimal setup (looks for F and F): package Foo; use Locale::Maketext::Simple; # exports 'loc' loc_lang('fr'); # set language to French sub hello { print loc("Hello, [_1]!", "World"); } More sophisticated example: package Foo::Bar; use Locale::Maketext::Simple ( Class => 'Foo', # search in auto/Foo/ Style => 'gettext', # %1 instead of [_1] Export => 'maketext', # maketext() instead of loc() Subclass => 'L10N', # Foo::L10N instead of Foo::I18N Decode => 1, # decode entries to unicode-strings Encoding => 'locale', # but encode lexicons in current locale # (needs Locale::Maketext::Lexicon 0.36) ); sub japh { print maketext("Just another %1 hacker", "Perl"); } =head1 DESCRIPTION This module is a simple wrapper around B, designed to alleviate the need of creating I for module authors. The language used is chosen from the loc_lang call. If a lookup is not possible, the i-default language will be used. If the lookup is not in the i-default language, then the key will be returned. If B is not present, it implements a minimal localization function by simply interpolating C<[_1]> with the first argument, C<[_2]> with the second, etc. Interpolated function like C<[quant,_1]> are treated as C<[_1]>, with the sole exception of C<[tense,_1,X]>, which will append C to C<_1> when X is C, or appending C to <_1> otherwise. =head1 OPTIONS All options are passed either via the C statement, or via an explicit C. =head2 Class By default, B draws its source from the calling package's F directory; you can override this behaviour by explicitly specifying another package as C. =head2 Path If your PO and MO files are under a path elsewhere than C, you may specify it using the C option. =head2 Style By default, this module uses the C style of C<[_1]> and C<[quant,_1]> for interpolation. Alternatively, you can specify the C style, which uses C<%1> and C<%quant(%1)> for interpolation. This option is case-insensitive. =head2 Export By default, this module exports a single function, C, into its caller's namespace. You can set it to another name, or set it to an empty string to disable exporting. =head2 Subclass By default, this module creates an C<::I18N> subclass under the caller's package (or the package specified by C), and stores lexicon data in its subclasses. You can assign a name other than C via this option. =head2 Decode If set to a true value, source entries will be converted into utf8-strings (available in Perl 5.6.1 or later). This feature needs the B or B module. =head2 Encoding Specifies an encoding to store lexicon entries, instead of utf8-strings. If set to C, the encoding from the current locale setting is used. Implies a true value for C. =cut sub import { my ($class, %args) = @_; $args{Class} ||= caller; $args{Style} ||= 'maketext'; $args{Export} ||= 'loc'; $args{Subclass} ||= 'I18N'; my ($loc, $loc_lang) = $class->load_loc(%args); $loc ||= $class->default_loc(%args); no strict 'refs'; *{caller(0) . "::$args{Export}"} = $loc if $args{Export}; *{caller(0) . "::$args{Export}_lang"} = $loc_lang || sub { 1 }; } my %Loc; sub reload_loc { %Loc = () } sub load_loc { my ($class, %args) = @_; my $pkg = join('::', grep { defined and length } $args{Class}, $args{Subclass}); return $Loc{$pkg} if exists $Loc{$pkg}; eval { local @INC = @INC; pop @INC if $INC[-1] eq '.'; require Locale::Maketext::Lexicon; 1 } or return; $Locale::Maketext::Lexicon::VERSION > 0.20 or return; eval { require File::Spec; 1 } or return; my $path = $args{Path} || $class->auto_path($args{Class}) or return; my $pattern = File::Spec->catfile($path, '*.[pm]o'); my $decode = $args{Decode} || 0; my $encoding = $args{Encoding} || undef; $decode = 1 if $encoding; $pattern =~ s{\\}{/}g; # to counter win32 paths eval " package $pkg; use base 'Locale::Maketext'; Locale::Maketext::Lexicon->import({ 'i-default' => [ 'Auto' ], '*' => [ Gettext => \$pattern ], _decode => \$decode, _encoding => \$encoding, }); *${pkg}::Lexicon = \\%${pkg}::i_default::Lexicon; *tense = sub { \$_[1] . ((\$_[2] eq 'present') ? 'ing' : 'ed') } unless defined &tense; 1; " or die $@; my $lh = eval { $pkg->get_handle } or return; my $style = lc($args{Style}); if ($style eq 'maketext') { $Loc{$pkg} = sub { $lh->maketext(@_) }; } elsif ($style eq 'gettext') { $Loc{$pkg} = sub { my $str = shift; $str =~ s{([\~\[\]])}{~$1}g; $str =~ s{ ([%\\]%) # 1 - escaped sequence | % (?: ([A-Za-z#*]\w*) # 2 - function call \(([^\)]*)\) # 3 - arguments | ([1-9]\d*|\*) # 4 - variable ) }{ $1 ? $1 : $2 ? "\[$2,"._unescape($3)."]" : "[_$4]" }egx; return $lh->maketext($str, @_); }; } else { die "Unknown Style: $style"; } return $Loc{$pkg}, sub { $lh = $pkg->get_handle(@_); }; } sub default_loc { my ($self, %args) = @_; my $style = lc($args{Style}); if ($style eq 'maketext') { return sub { my $str = shift; $str =~ s{((? 1) ? ($4 || "$3s") : $3) : '' ) : '' ); }egx; return $str; }; sub _escape { my $text = shift; $text =~ s/\b_([1-9]\d*)/%$1/g; return $text; } sub _unescape { join(',', map { /\A(\s*)%([1-9]\d*|\*)(\s*)\z/ ? "$1_$2$3" : $_ } split(/,/, $_[0])); } sub auto_path { my ($self, $calldir) = @_; $calldir =~ s#::#/#g; my $path = $INC{$calldir . '.pm'} or return; # Try absolute path name. if ($^O eq 'MacOS') { (my $malldir = $calldir) =~ tr#/#:#; $path =~ s#^(.*)$malldir\.pm\z#$1auto:$malldir:#s; } else { $path =~ s#^(.*)$calldir\.pm\z#$1auto/$calldir/#; } return $path if -d $path; # If that failed, try relative path with normal @INC searching. $path = "auto/$calldir/"; foreach my $inc (@INC) { return "$inc/$path" if -d "$inc/$path"; } return; } 1; =head1 ACKNOWLEDGMENTS Thanks to Jos I. Boumans for suggesting this module to be written. Thanks to Chia-Liang Kao for suggesting C and C. =head1 SEE ALSO L, L =head1 AUTHORS Audrey Tang Ecpan@audreyt.orgE =head1 COPYRIGHT Copyright 2003, 2004, 2005, 2006 by Audrey Tang Ecpan@audreyt.orgE. This software is released under the MIT license cited below. Additionally, when this software is distributed with B, you may also redistribute it and/or modify it under the same terms as Perl itself. =head2 The "MIT" License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. =cut usr/share/perl5/vendor_perl/Error/Simple.pm000064400000002106152362041240014703 0ustar00# Error/Simple.pm # # Copyright (c) 2006 Shlomi Fish . # This file is free software; you can redistribute it and/or # modify it under the terms of the MIT/X11 license (whereas the licence # of the Error distribution as a whole is the GPLv1+ and the Artistic # licence). package Error::Simple; use strict; use warnings; use vars qw($VERSION); $VERSION = "0.17025"; use Error; 1; __END__ =head1 NAME Error::Simple - the simple error sub-class of Error =head1 SYNOPSIS use base 'Error::Simple'; =head1 DESCRIPTION The only purpose of this module is to allow one to say: use base 'Error::Simple'; and the only thing it does is "use" Error.pm. Refer to the documentation of L for more information about Error::Simple. =head1 METHODS =head2 Error::Simple->new($text [, $value]) Constructs an Error::Simple with the text C<$text> and the optional value C<$value>. =head2 $err->stringify() Error::Simple overloads this method. =head1 KNOWN BUGS None. =head1 AUTHORS Shlomi Fish ( L ) =head1 SEE ALSO L