Implement the Delete a Forum option in default.pl. Selecting this option should display the initial screen, but with each forum name followed by a hyperlink to a script named del- Forum.pl. Your script should remove the given forum from forums.xml and delete the underlying XML document. [Hint: Look at the getElementsByTagName and removeChild 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
5use 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" );
261 #!/usr/bin/perl
2 # default.pl
3 # Default page for XML forums
45
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 ( 0 .. $#files );
36
37 if ( param( "modify" ) )
38 {
39 my $action = param( "modify" ) eq "modify" ? "mod" : "del";
40 my $link = param( "modify" ) eq "modify" ? "Modify" : "Delete";
41
42 @items = map { $items[ $_ ] . " " .
43 a( { -href => "$action" . "Forum.pl?file=$files
44 [$_ ]",
45 -class => "modify" },
46 "[ $link ]" ) }
47 ( 0 .. $#files );
48 }
49
50 print p( strong( "Avaliable Forums" ), ul( li( \@items ) ) );
51
52 @items = ( a( { -href => "addForum.pl" }, "Add a Forum" ),
53 a( { -href => "default.pl?modify=modify" }, "Modify a
54 Forum" ),
55 a( { -href => "default.pl?modify=delete" }, "Delete a
56 Forum" ),
57 a( { -href => "default.pl" }, "Home" ) );
58
59 print p( strong( "Forum Management" ), ul( li( \@items ) ) ),
60 end_html;
61 close XML;
62
63 sub startTag
64 {
65 my ( $expat, $element, %attributes ) = @_;
66 push( @files, $attributes{ "filename" } ) if $element eq "forum";
67 }
68
69 sub text
70 {
71 my ( $expat, $string ) = @_;
72 push( @forums, $string ) if $expat->in_element( "name" );
73 }
```
```
1 #!perl
2 # Sol. 17.4: delForum.pl
34use 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
14 my $file = "forums.xml";
15
16 open XML, "+< ../htdocs/XML/$file" or die "Could not open: $!";
17 flock XML, LOCK_EX;
18
19 $parser = new XML::DOM::Parser;
20 $document = $parser->parse( \*XML );
21 $forums = $document->getDocumentElement;
22
23 foreach $forum ( $forums->getElementsByTagName( "forum" ) ) {
24 $file = $forum->getAttribute( "filename" );
25 if ( $file eq param( "file" ) ) {
26 $file =~ /^\w+\.xml$/ or die "Not a valid file: $!";
27 unlink( "../htdocs/XML/$file" ) or die
28 "Could not unlink: $!";
29 $forums->removeChild( $forum );
30 }
31 }
32
33 seek XML, 0, 0;
34 truncate XML, 0;
35 $document->print( \*XML );
36 close XML;
37 print redirect( "default.pl" );
38
39 }
40 else {
41 print redirect( "error.html" );
42 }
```
```
1 #!perl
2 # Sol. 17.4: delForum.pl
34use 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
14 my $file = "forums.xml";
15
16 open XML, "+< ../htdocs/XML/$file" or die "Could not open: $!";
17 flock XML, LOCK_EX;
18
19 $parser = new XML::DOM::Parser;
20 $document = $parser->parse( \*XML );
21 $forums = $document->getDocumentElement;
22
23 foreach $forum ( $forums->getElementsByTagName( "forum" ) ) {
24 $file = $forum->getAttribute( "filename" );
25 if ( $file eq param( "file" ) ) {
26 $file =~ /^\w+\.xml$/ or die "Not a valid file: $!";
27 unlink( "../htdocs/XML/$file" ) or die
28 "Could not unlink: $!";
29 $forums->removeChild( $forum );
30 }
31 }
32
33 seek XML, 0, 0;
34 truncate XML, 0;
35 $document->print( \*XML );
36 close XML;
37 print redirect( "default.pl" );
38
39 }
40 else {
41 print redirect( "error.html" );
42 }
```
You might also like to view...
Which of the following statements is false?
a) Exception handling enables programmers to write robust and fault-tolerant programs. b) Exception handling can catch the exception but cannot resolve the exception. c) Exception handling can resolve exceptions. d) There are many ways of handling exceptions.
The process of having a class contain an instance of another class is known as
A) object overloading. B) operator overloading. C) object composition. D) dynamic composition. E) None of the above
A ________ reference is a tag or use of a table element as a reference in a formula
Fill in the blank(s) with correct word
What function would you use to calculate the total number of periods in a loan or investment?
What will be an ideal response?`