Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I'm not sure in which part of the site to post this, as it's threefold... So I'll just throw it in Meditations so we can also meditate about how to categorize the post :)

A Cool use of Perl: An example (and to proof that it can be done) of a very simple application that uses HTTP::Daemon and HTML and should thus be portable and easy to make.

The meditative part of this thread: is this a good or a bad idea? It's portable, but some platforms have native widgets that would be nicer to create a GUI in.

And finally, a small quest for Perl wisdom: what's a portable way to find out if the user has left the "site" or closed the browser window? So far, I've told users of these little applications to hit ^C when done, but that's not pretty.

#!/usr/bin/perl -w # Pizza calculator, an example of how to build a portable web based # GUI application. use strict; use HTTP::Daemon; use HTTP::Status; use CGI::Simple; sub res { HTTP::Response->new( RC_OK, OK => [ 'Content-Type' => 'text/html' ], shift ) } my %pages = ( index => sub { res qq[ <html><body> <h1>Pizza cost calculator</h1> <form method=post action=calc> Total cost: <input name=cost><br> Number of eaters: <input name=ppl><br> <input type=submit value=Submit> </form> </body></html> ]; }, calc => sub { my ($request) = @_; my $cgi = CGI::Simple->new( $request->content ); res sprintf q[ <html><body> <h1>Result</h1> Cost per eater: %.2f<br> <a href=index>Again!</a> </body></html> ], $cgi->param('cost') / $cgi->param('ppl'); }, ); my $daemon = HTTP::Daemon->new(LocalAddr => '') or die; my $ppid = $$; if (my $pid = fork) { while (my $client = $daemon->accept) { while (my $request = $client->get_request) { my ($page) = $request->url->path =~ m[^/(\w+)$]; $page ||= 'index'; if (exists $pages{$page}) { $client->send_response( $pages{$page}->($request) ); } else { $client->send_error(RC_NOT_FOUND); } } $client->close; } } elsif (defined $pid) { my $url = $daemon->url; my @browsers = qw(firefox konqueror mozilla opera start); { @browsers or die "Couldn't start a browser."; my $b = shift @browsers; my $r = system("$b $url"); redo if $r; } } else { die "Couldn't fork!"; }

Juerd # { site => '', plp_site => '', do_not_use => 'spamtrap' }

In reply to GUI with HTTP::Daemon by Juerd

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (1)
    As of 2021-02-25 02:59 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found