in reply to CGI::AppEasy - a quick way to give your perl program a web-based user interface
This is a working sample application for the CGI::AppEasy module above.
use CGI::AppEasy; use Getopt::Long; use Data::Dumper; use MIME::Base64; use HTTP::Daemon; use HTTP::Status; use strict; use warnings; my $dbfilename = 'stash.dat'; # always load existing, first: our $stash1 = {}; # this is the variable set by eval'ing the Dumper-ge +nerated code. my $fh; if ( open $fh, '<', $dbfilename ) { local $/; eval decode_base64( <$fh> ); close $fh; } my $static_content; make_main_content(); my $w = CGI::AppEasy->new( '/' => \$static_content, '/dump' => \&Dump, '/exit' => \&Exit, '/update' => \&Update, '/explore' => \&Explore, ); GetOptions( 'update!' => sub { shift; Update() }, 'dump!' => sub { shift; Dump() }, 'explorer!' => sub { shift; Explore() }, 'server!' => sub { $w->serve }, ); sub Dump { print Dumper $_[0]; print Dumper $stash1; $static_content } sub Update { my %entries = map { my( $title ) = /(.*)\.URL/i; my( $url ) = do { local(@ARGV,$/) = ($_); <> } =~ /^URL=(.*)/m +; $url ? ( $url => $title ) : () } grep /\.URL$/i, do { opendir my $dh, '.' or die; readdir $dh }; keys %entries or die "Nothing new to add.\n"; warn "Adding these entries: \n\n", Dumper \%entries; @{$stash1}{keys %entries} = values %entries; # write it back out: open $fh, '>', $dbfilename or die; local $Data::Dumper::Indent=0; local $Data::Dumper::Varname='stash'; print $fh encode_base64( Dumper $stash1 ); close $fh; make_main_content(); $static_content } sub Explore { system qq(explorer .); $static_content } sub Exit { $_[1]->end; # this causes server loop exit. page_contain( qq(<a href="/">Return, if the server is running agai +n</a>, or close this browser window.<p>\n) ) } sub make_main_content { $static_content = page_contain( join '', # menu: qq( <a href="/">Main</a> <a href="/update">Update stash</a> <a href="/explore">Open Explorer</a> <a href="/exit">Exit the server loop</a> <p> <hr/> ), map { qq(<li><a href="$_">$stash1->{$_}</a></li>\n) } sort { $stash1->{$a} cmp $stash1->{$b} or $a cmp $b } keys %$stash1 ) } sub page_contain { <<EOF <html> <head> <title>Links</title> </head> <body> $_[0] </body> </html> EOF }
|
|---|