home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / xyx < prev    next >
Encoding:
Text File  |  2003-09-17  |  1.7 KB  |  90 lines

  1. #!perl -w
  2.  
  3. $VERSION = '0.35';
  4.  
  5.  
  6. =head1 NAME
  7.  
  8. xyx - convert xml to yaml or vice versa
  9.  
  10. =head1 USAGE
  11.  
  12.     xyx foo.xml > foo.yml
  13.     xyx foo.yml > foo.xml
  14.  
  15. =head1 DESCRIPTION
  16.  
  17. This program requires the Perl modules XML::Simple and YAML. It will use one of them to Load a document in the respective format, and use the other to Dump the data structure in the other format. Input content type is autodetected.
  18.  
  19. This is a trivial program and probably only useful for trivial tasks.
  20.  
  21. =head1 AUTHOR
  22.  
  23. Brian Ingerson <ingy@cpan.org>
  24.  
  25. =head1 COPYRIGHT
  26.  
  27. Copyright 2002, Brian Ingerson - All rights reserved
  28.  
  29. You may use this hack under the same terms as Perl itself.
  30.  
  31. =cut
  32.  
  33. use strict;
  34. use YAML;
  35. use XML::Simple;
  36.  
  37. my @serieskeys = ();
  38.  
  39. my $data = join '', <>;
  40. my %opts;
  41.  
  42. if ($data =~ /^\s*</) {
  43.     my $xml = XMLin(
  44.     $data,
  45.     keeproot=>1,
  46.     noattr=>1,
  47.     keyattr=>[@serieskeys],
  48.     suppressempty=>''
  49.     );
  50.     print Dump($xml);
  51. }
  52. else {
  53.     for my $object (Load($data)) {
  54.     print XMLout($object,
  55.              keeproot => 1,
  56.              noattr => 1,
  57.             );
  58.     }
  59. }
  60.  
  61. BEGIN {
  62.     my %options = map {("-$_", 1)}
  63.       qw(help);
  64.     my @STDIN = ();
  65.     if (@ARGV and
  66.         $ARGV[-1] !~ /^-/
  67.        ) {
  68.         die "Invalid input file '$ARGV[-1]'\n"
  69.           unless -f $ARGV[-1];
  70.         push @STDIN, pop @ARGV;
  71.     }
  72.  
  73.     for my $arg (@ARGV) {
  74.         if ($arg =~ /^-s(?:erieskeys)?=(\w.*)$/) {
  75.             for my $key (split ',', $1) {
  76.                 push @serieskeys, $key;
  77.             }
  78.         }
  79.         elsif ($options{$arg}) {
  80.             $opts{$arg} = 1;
  81.         }
  82.         else {
  83.             die "Invalid option '$arg'\n";
  84.         }
  85.     }
  86.  
  87.     @ARGV = @STDIN;
  88. }
  89.  
  90.