Implement the Modify a Forum option in default.pl such that individual messages can be deleted. Selecting this option should display the initial screen, but with each forum name followed by a hyperlink to a script named modForum.pl. Script modForum.pl should display the messages as in forum.pl, but each message title should be followed by a link to a script named del- Post.pl, which removes the given message from the current forum. [Hint: Look at the getElementsByTagName, removeChild, and item methods described in the XML::DOM documentation.]
What will be an ideal response?
```
1 #!/usr/bin/perl
2 # default.pl
3 # Default page for XML forums
4
5 use warnings;
6 use strict;
7 use CGI qw( :standard );
8 use XML::Parser;
9 use Fcntl qw( :flock );
10
11 my ( $parser, $prefix, @files, @forums, @items );
12
13 open XML, "../htdocs/XML/forums.xml" or die "Could not open: $!";
14 flock XML, LOCK_UN;
15 flock XML, LOCK_SH;
16 $parser = new XML::Parser( Handlers => { Start => \&startTag,
17 Char => \&text } );
18 $parser->parse( \*XML );
19
20 print header( -expires => "-1d", -cache_control => "no-cache",
21 -pragma => "no-cache"),
22 start_html( -title => "Deitel Message Forums",
23 -style => { -src => "../XML/site.css" } );
24
25 print h1( "Deitel Message Forums" );
26
27 if ( $ENV{ "HTTP_USER_AGENT" } =~ /MSIE/i ) {
28 $prefix = "../XML/";
29 }
30 else {
31 $prefix = "forum.pl?file=";
32 }
33
34 @items = map { a( { -href => "$prefix$files[ $_ ]" }, $forums[ $_ ]
35 ) }
36 ( 0 .. $#files );
37
38 if ( param( "modify" ) )
39 {
40 my $action = param( "modify" ) eq "modify" ? "mod" : "del";
41 my $link = param( "modify" ) eq "modify" ? "Modify" : "Delete";
42
43 @items = map { $items[ $_ ] . " " .
44 a( { -href => "$action" . "Forum.pl?file=$files
45 [$_ ]",
46 -class => "modify" },
47 "[ $link ]" ) }
48 ( 0 .. $#files );
49 }
50
51 print p( strong( "Avaliable Forums" ), ul( li( \@items ) ) );
52
53 @items = ( a( { -href => "addForum.pl" }, "Add a Forum" ),
54 a( { -href => "default.pl?modify=modify" }, "Modify a
55 Forum" ),
56 a( { -href => "default.pl?modify=delete" }, "Delete a
57 Forum" ),
58 a( { -href => "default.pl" }, "Home" ) );
59
60 print p( strong( "Forum Management" ), ul( li( \@items ) ) ),
61 end_html;
62 close XML;
63
64 sub startTag
65 {
66 my ( $expat, $element, %attributes ) = @_;
67 push( @files, $attributes{ "filename" } ) if $element eq "forum";
68 }
69
70 sub text
71 {
72 my ( $expat, $string ) = @_;
73 push( @forums, $string ) if $expat->in_element( "name" );
74 }
```
```
1 #!/usr/bin/perl
2 # Sol. 17.5: modForum.pl
34
use warnings;
5 use strict;
6 use CGI qw( :standard *center *table );
7 use XML::Parser;
8 use Fcntl qw( :flock );
9 use CGI::Carp qw( fatalsToBrowser );
10
11 print redirect( "error.html" ) if not param;
12
13 my ( $file, $parser, %info );
14 my $number = 0;
15
16 if ( param ) {
17 $file = param( "file" );
18 $file =~ /^\w+\.xml$/ or die "Not a valid file: $!";
19 open FORUM, "../htdocs/XML/$file" or die "Could not open: $!";
20 flock FORUM, LOCK_SH;
21
22 $parser = new XML::Parser( Style => "Subs",
23 Handlers => { Char => \&text } );
24 $parser->parse( \*FORUM );
25 close FORUM;
26
27 }
28 else {
29 print redirect( "error.html" );
30 }
31
32 sub forum
33 {
34 print header( -cache_control => "no-cache" );
35 }
36
37 sub forum_
38 {
39 print br, start_center,
40
41 a( { -href => "../cgi-bin/addPost.pl?file=$file" },
42 "Post a Message" ),
43 br, br,
44 a( { -href => "../cgi-bin/default.pl" },
45 "Return to Main Page" ),
46
47 end_center, end_html;
48 }
49
50 sub name_
51 {
52 print start_html( -title => $info{ "name" },
53 -style => { -src => "../XML/site.css" } ),
54
55 start_table( { -width => "100%",
56 -cellspacing => "0",
57 -cellpadding => "2" } ),
58
59 Tr( td( { -class => "forumTitle" }, $info{ "name" } ) ),
60
61 end_table, br;
62 }
63
64 sub message
65 {
66 my ( $expat, $element, %attributes ) = @_;
67 $info{ "date" } = $attributes{ "timestamp" };
68 }
69
70 sub message_
71 {
72 print start_table( { -width => "100%",
73 -cellspacing => "0",
74 -cellpadding => "2" } ),
75
76 Tr( [ td( { -class => "msgTitle" },
77 $info{ "title" } . " " .
78 a( { -href => "delPost.pl?file=$file&number=$number",
79 -class => "modify" },
80 "[ Delete ]" ) ),
81
82 td( { -class => "msgInfo" },
83 " by " . em( $info{ "user" } ) . " at " .
84 span( { -class => "date" }, $info{ "date" } ) ),
85
86 td( { -class => "msgText" }, $info{ "text" } ) ] ),
87
88 end_table;
89
90 $number++;
91 }
92
93 sub text
94 {
95 my ( $expat, $string ) = @_;
96 $info{ $expat->current_element } = $string;
97 }
```
```
1 #!/usr/bin/perl
2 # Sol. 17.5: delPost.pl
34
use warnings;
5 use strict;
6 use CGI qw( :standard );
7 use XML::DOM;
8 use Fcntl qw( :flock :DEFAULT );
9 use CGI::Carp qw( fatalsToBrowser );
10
11 if ( param ) {
12 my ( $parser, $document, $forums, $forum, $name, $erase,
13 $forumList );
14
15 my $file = param( "file" );
16
17 open XML, "+< ../htdocs/XML/$file" or die "Could not open: $!";
18 flock XML, LOCK_EX;
19
20 $parser = new XML::DOM::Parser;
21 $document = $parser->parse( \*XML );
22 $forums = $document->getDocumentElement;
23
24 $forumList = $forums->getElementsByTagName( "message" );
25 $forums->removeChild( $forumList->item( param( "number" ) ) );
26
27 seek XML, 0, 0;
28 truncate XML, 0;
29 $document->print( \*XML );
30 close XML;
31 print redirect( -cache_control => "no-cache", -location =>
32 "modForum.pl?file=$file" );
33
34 }
35 else {
36 print header, start_html( -title => "Modify a Forum",
37 -style => { -src => "../XML/site.css" } );
38 print start_form,
39 "Forum Name (to modify)", br,
40 textfield( -name => "file", -size => 40 ), br,
41 submit( -name => "submit", value => "Submit" ),
42 reset, end_form,
43 a( { -href => "default.pl" }, "Return to Main Page" ),
44 end_html;
45 }
```
You might also like to view...
A datacenter is in a geologically active area and has no earthquake abatement installed. Which of the following drive types is BEST for general server use?
A. Solid state drives B. High RPM drives C. Low RPM drives D. USB drives
Every program has a(n) __________ __________ which is where the program begins and normally ends.
Fill in the blank(s) with correct word