addpost.pl

来自「PERL语言资料 可以用于PERL程序设计」· PL 代码 · 共 75 行

PL
75
字号
#!perl
# Fig. 22.36: addPost.pl
# Adds a posting to a forum

use warnings;
use strict;
use CGI qw( :standard );
use XML::DOM;
use Fcntl qw( :flock );

if ( param( "submit" ) ) {
   my ( $parser, $document, $forum, $message, $element );

   my $file = param( "file" );
   $file  =~ /^\w+\.xml$/ or die( "Not a valid file: $!" );
   open( FORUM, "+<../htdocs/XML/$file" ) 
      or die( "Could not open: $!" );
   flock( FORUM, LOCK_EX );

   $parser = new XML::DOM::Parser;
   $document = $parser->parse( \*FORUM );
   $forum = $document->getDocumentElement;

   $message = $document->createElement( "message" ); 
   $message->setAttribute( "timestamp", scalar( localtime ) );
   $forum->appendChild( $message );

   foreach ( qw( user title text ) ) {
      $element = $document->createElement( $_ );
      $element->addText( param( $_ ) );
      $message->appendChild( $element );
   }

   seek( FORUM, 0, 0 );
   truncate( FORUM, 0 );
   $document->printToFileHandle( \*FORUM );
   close( FORUM );
   print( redirect( "/XML/$file" ) );
} 
elsif ( param ) {
   my $file = param( "file" );
   print( header, start_html( -title => "Add a posting", 
      -style => { -src => "/XML/site.css" } ) );
   print( start_form(),
       "User", br(),
       textfield( -name => "user", -size => 40 ), br(),
       "Message Title", br(), 
       textfield( -name => "title", -size => 40 ), br(),
       "Message Text", br(),
       textarea( -name => "text", -cols =>  40, -rows => 5 ), 
       br(), hidden( -name => "file", -value => $file ),
       submit( -name => "submit", -value => "Submit" ),
       reset(), end_form(), 
       a( { -href => "/XML/$file" }, "Return to Forum" ), 
          end_html() );
} 
else {
   print( redirect( "error.html" ) );
}


 ########################################################################## 
 # (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 + =
减小字号Ctrl + -
显示快捷键?