📄 contacts.pl
字号:
#!/usr/bin/perl
# Fig. 22.12: contacts.pl
# Using the XML::DOM module to manipulate an XML Document.
use warnings;
use strict;
use XML::DOM;
open( CONTACTS, "+<contact.xml" )
or die( "Error opening contact.xml" );
my $parser = new XML::DOM::Parser;
my $document = $parser->parse( \*CONTACTS );
printlist( $document );
instructions();
my $choice = 6;
while ( $choice ne 'q' ) {
print( "\n? " );
chomp( $choice = <STDIN> );
if ( $choice eq 'a' ) {
addContact( $document );
}
elsif ( $choice eq 'l' ) {
printlist( $document );
}
elsif ( $choice eq 'i' ) {
instructions( $document );
}
elsif ( $choice ne 'q' ) {
print( "$choice is not an option." .
" Enter i for instructions.\n");
}
}
seek( CONTACTS, 0, 0 );
truncate( CONTACTS, 0 );
$document->print( \*CONTACTS );
sub printlist
{
my $document = shift;
my $root =
$document->getElementsByTagName( "contacts" )->item( 0 );
my $contactList = $root->getChildNodes();
print( "Your contact list: \n" );
for my $i ( 1 .. $contactList->getLength - 1 ) {
my $contact = $contactList->item( $i );
next unless ( $contact->getNodeName eq 'contact' );
my $first = $contact->getElementsByTagName( "FirstName" );
my $text =
$first->item( 0 )->getChildAtIndex( 0 )->getData();
my $lastN = $contact->getElementsByTagName( "LastName" );
my $last =
$lastN->item( 0 )->getChildAtIndex( 0 )->getData();
print( "$text $last\n" );
}
}
sub addContact
{
my $document = shift;
my $root =
$document->getElementsByTagName( "contacts" )->item( 0 );
print( "Enter the name of the person you wish to add: " );
chomp( my $name = <STDIN> );
my ( $first, $last ) = split( / /, $name );
my $firstNode = $document->createElement( "FirstName" );
$firstNode->addText( $first );
my $lastNode = $document->createElement( "LastName" );
$lastNode->addText( $last );
my $contact = $document->createElement( "contact" );
$contact->appendChild( $firstNode );
$contact->appendChild( $lastNode );
$root->appendChild( $contact );
}
sub instructions
{
print( "\n\n",
"Enter 'a' to add a contact.\n",
"Enter 'l' to list the contacts.\n",
"Enter 'i' for instructions.\n",
"Enter 'q' to quit.\n" );
}
##########################################################################
# (C) Copyright 2001 by Deitel & Associates, Inc. and Prentice Hall. #
# All Rights Reserved. #
# #
# DISCLAIMER: The authors and publisher of this book have used their #
# best efforts in preparing the book. These efforts include the #
# development, research, and testing of the theories and programs #
# to determine their effectiveness. The authors and publisher make #
# no warranty of any kind, expressed or implied, with regard to these #
# programs or to the documentation contained in these books. The authors #
# and publisher shall not be liable in any event for incidental or #
# consequential damages in connection with, or arising out of, the #
# furnishing, performance, or use of these programs. #
##########################################################################
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -