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.bat < prev    next >
Encoding:
DOS Batch File  |  2003-09-17  |  2.9 KB  |  100 lines

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