#!/usr/bin/perl use Getopt::Std; getopts('CcspneEmwaih'); # -o & -i are boolean flags. Sets opt_* as a side effect. if (not defined (@ARGV)) {$opt_p = 1;}; # if no switch supplied, enable Phone $usage = $0 . ' usage: -c dump phone, email and address in CSV format -C dump everything in CSV format -s dump output in Skype vcf format -p print Phone field (default if no switch specified) -n print Notes field -e print Email field -E print Email field only (do not print Name) -m print Email addresses for mass mail (in format like: "Cecile Vowles" ) -w print Web field -a print Address field -i print IM field -h print this message Note: the Name field will be printed by default, except with the -E option '; if ($opt_h) {print $usage; exit 0; } # print CSV headers if ($opt_C) { print '"Name",' . '"Phone",' . '"Email",' . '"Address",' . '"IM",' . '"Web",' . '"Directions",' . '"Notes",' . "\n"; } # read in addressbook require 'phonebook.dat'; #Reference: "Access and Printing of a LIST OF HASHES", from perldsc man page for $i ( 0 .. $#ab ) { $name_printed = 0; # only print name if there's anything requested # associated with it if ($ab[$i]{'Notes'} and $opt_n) { do { print $ab[$i]{'Name'}; $name_printed = 1; } unless $name_printed; $name_printed = 1; print ' (' . $ab[$i]{'Notes'} . ')'; } if ($ab[$i]{'Phone'} and $opt_p) { do { print $ab[$i]{'Name'}; $name_printed = 1; } unless $name_printed; print ' ' . $ab[$i]{'Phone'}; } # dump output in Skype vcf format if ($ab[$i]{'Phone'} and $opt_s) { $phone = $ab[$i]{'Phone'}; $phone =~ s/-//g; print "BEGIN:VCARD\nVERSION:3.0\n"; print 'N:' . $phone . "\n"; print 'X-SKYPE-PSTNNUMBER:' . $phone . "\n"; print 'X-SKYPE-DISPLAYNAME:' . $ab[$i]{'Name'} . "\n"; print 'REV:01080403T144742Z ' . "\n" . 'END:VCARD' . "\n\n"; undef $phone; } # dump all address info (phone, email, address) in CSV format if ($opt_c) { print '"' . $ab[$i]{'Name'} . '",' . '"' . $ab[$i]{'Phone'} . '",' . '"' . $ab[$i]{'Email'} . '",' . '"' . $ab[$i]{'Address'} . '"' . "\n"; } # dump everything in CSV format if ($opt_C) { print '"' . $ab[$i]{'Name'} . '",' . '"\''. $ab[$i]{'Phone'} . '\'",' . '"' . $ab[$i]{'Email'} . '",' . '"' . $ab[$i]{'Address'} . '",' . '"' . $ab[$i]{'IM'} . '",' . '"' . $ab[$i]{'Web'} . '",' . '"' . $ab[$i]{'Directions'} . '",' . '"' . $ab[$i]{'Notes'} . '",' . "\n"; } if ($ab[$i]{'Email'} and $opt_e) { do { print $ab[$i]{'Name'}; $name_printed = 1; } unless $name_printed; print ' ' . $ab[$i]{'Email'}; } if ($ab[$i]{'Email'} and $opt_E) { print $ab[$i]{'Email'}; print "\n"; } # output somethink like: "Cecile Vowles" if ($ab[$i]{'Email'} and $opt_m) { do { print '"' . $ab[$i]{'Name'} . '"'; $name_printed = 1; } unless $name_printed; print ' <' . $ab[$i]{'Email'} . '>,'; } if ($ab[$i]{'Web'} and $opt_w) { do { print $ab[$i]{'Name'}; $name_printed = 1; } unless $name_printed; print ' ' . $ab[$i]{'Web'}; } if ($ab[$i]{'Address'} and $opt_a) { do { print $ab[$i]{'Name'}; $name_printed = 1; } unless $name_printed; print ' ' . $ab[$i]{'Address'}; } if ($ab[$i]{'IM'} and $opt_i) { do { print $ab[$i]{'Name'}; $name_printed = 1; } unless $name_printed; print ' ' . $ab[$i]{'IM'}; } # if we returned an entry, end it with a CR if ($name_printed) { print "\n"; } } exit;