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 / phone2yaml < prev    next >
Encoding:
Text File  |  2003-09-17  |  2.5 KB  |  84 lines

  1. #!perl -w
  2.  
  3. $VERSION = '0.35';
  4.  
  5.  
  6. =head1 NAME
  7.  
  8. phone2yaml - Convert a Palm phone book to YAML
  9.  
  10. =head1 USAGE
  11.  
  12.     phone2yaml phone.tab > phone.yaml
  13.  
  14. =head1 DESCRIPTION
  15.  
  16. This program will convert a Palm Pilot phonebook .tab file (tab
  17. separated values) into YAML format. The output can then be easily
  18. processed by using YAML in your favorite programming language.
  19. (Like YAML.pm for Perl) Personally I just grep the YAML file, when
  20. I need a phone number.
  21.  
  22. =head1 AUTHOR
  23.  
  24. Brian Ingerson <ingy@cpan.org>
  25.  
  26. =head1 COPYRIGHT
  27.  
  28. Copyright 2002, Brian Ingerson - All rights reserved
  29.  
  30. You may use this hack under the same terms as Perl itself.
  31.  
  32. =cut
  33.  
  34. use strict;
  35. use YAML;
  36. use YAML::Node;
  37.  
  38. my @entries;
  39.  
  40. while (my $line = <>) {
  41.     $line =~ s/\r\n//g;
  42.     my ($last, $first, $title, $company, $work, $home, $fax, $other, $email,
  43.         $street, $city, $state, $postal, $country, 
  44.         undef, undef, undef, undef, $note, @rest) = 
  45.           map {s/^"(.*)"$/$1/; s/\r/\n/g; $_.="\n" if /\n./; $_} 
  46.             split "\t", $line;
  47.  
  48.     # YAML Nodes preserve key order.
  49.     my $entry = YAML::Node->new({});
  50.     $entry->{name} = YAML::Node->new({}) if $first or $last;
  51.     $entry->{phones} = YAML::Node->new({}) if $work or $home or $fax or $other;
  52.     $entry->{address} = YAML::Node->new({}) if $street or $city or $state or
  53.                                                $postal or $country;
  54.  
  55.     $entry->{name}{first} = $first if $first;
  56.     $entry->{name}{last} = $last if $last;
  57.     $entry->{title} = $title if $title;
  58.     $entry->{company} = $company if $company;
  59.     $entry->{phones}{home} = $home if $home;
  60.     $entry->{phones}{work} = $work if $work;
  61.     $entry->{phones}{fax} = $fax if $fax;
  62.     $entry->{phones}{other} = $other if $other;
  63.     $entry->{email} = $email if $email;
  64.     $entry->{address}{street} = $street if $street;
  65.     $entry->{address}{city} = $city if $city;
  66.     $entry->{address}{state} = $state if $state;
  67.     $entry->{address}{postal} = $postal if $postal;
  68.     $entry->{address}{country} = $country if $country;
  69.     $entry->{note} = $note if $note;
  70.     push @entries, $entry;
  71. }
  72.  
  73. $YAML::UseBlock = 1;
  74. $YAML::UseVersion = 0;
  75. #$YAML::SortKeys = [ qw( name first last title company 
  76. #                        phones work home fax other email
  77. #                        address street city state postal country
  78. #                        note
  79. #                      ) 
  80. #                  ];    
  81.                         
  82. print Dump @entries;
  83.  
  84.