[30082] in Perl-Users-Digest
Perl-Users Digest, Issue: 1325 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Mar 2 00:14:14 2008
Date: Sat, 1 Mar 2008 21:14:06 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sat, 1 Mar 2008 Volume: 11 Number: 1325
Today's topics:
Re: Tecnique and/or Modules for Perl and HTML <RedGrittyBrick@SpamWeary.foo>
Re: Tecnique and/or Modules for Perl and HTML <ben@morrow.me.uk>
Re: Tecnique and/or Modules for Perl and HTML <pgodfrin@gmail.com>
Re: Tecnique and/or Modules for Perl and HTML <pgodfrin@gmail.com>
use CGI::Carp qw(fatalsToBrowser) not sending file hand <bennett@peacefire.org>
Re: use CGI::Carp qw(fatalsToBrowser) not sending file xhoster@gmail.com
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 01 Mar 2008 19:26:57 +0000
From: RedGrittyBrick <RedGrittyBrick@SpamWeary.foo>
Subject: Re: Tecnique and/or Modules for Perl and HTML
Message-Id: <xIGdncEw2PuTM1TaRVnyhAA@bt.com>
pgodfrin wrote:
> On Mar 1, 10:42 am, Ron Bergin <r...@i.frys.com> wrote:
>> On Mar 1, 7:42 am, pgodfrin <pgodf...@gmail.com> wrote:
>>
>>
>>
>>> Greetings,
>>> Could someone point me in the right direction? I'm not a web developer
>>> - just a budding perl dude (who's really a database guy).
>>> I'd like to (at first) read from database (using DBI) and display the
>>> contents on a web page. The trick is I want to do it on the same page
>>> that the action button resides and pop the output of the perl program
>>> into a scrollable box. Nothing fancy - just output some data from a
>>> database call. Eventually I'd like to be able to pass variables from a
>>> form to the waiting perl program
>>> There is a bunch of confusing stuff out there everything from
>>> javascript to embperl and then Mason. And the unspeakable PHP of
>>> course...
>>> Can anyone offer some advice where I should go? (ok - no wisecracks...
>>> <grin>)
>>> thanks,
>>> pg
>> Have you looked at the CGI module? The synopsis section will give you
>> a hint on part of what you need. By the time you finish reading the
>> doc, you should have a pretty good idea where to start. After you've
>> made an attempt, post back with specific questions and example code
>> that demonstrates the problem that you're having.http://search.cpan.org/~lds/CGI.pm-3.33/CGI.pm
>
> Yep - I've even run the samples. Perhaps it's a conceptual thing, but
> it seems to me that the CGI module is all encompassing. When it's run
> - it must produce the entire web page. Which is not what I think I
> want.
>
> I'm not having a problem per se, but a conceptual quagmire - I have a
> web page, I just want to slap a table or text box in the middle of it
> with data from a perl DBI query. Maybe that's not how you do it...
> pg
Never having used DBI, but having Google to hand,
I'd try something like (untested):
---------------------- 8< ---------------------
#!perl
use strict;
use warnings;
use CGI qw/:standard/;
use CGI::Carp qw/warningsToBrowser fatalsToBrowser/;
use DBI
my $dbh = DBI->connect('dbi:DBMSNAME:whatever','pgodfrin','fred')
or die "Can't connect to database because $DBI::errstr";
my $sql = 'SELECT id, name, title, phone FROM employees';
my $sth = $dbh->prepare($sql);
$sth->execute();
my($id, $name, $title, $phone);
$sth->bind_columns(undef, \$id, \$name, \$title, \$phone);
my $rows;
while($sth->fetch()) {
$rows .= Tr(td($id),td($name),td($title),td($phone));
}
$sth->finish();
$dbh->disconnect();
print
header(),
start_html(),
h1('Employees'),
table($rows),
end_html();
---------------------- 8< ---------------------
What have you tried?
------------------------------
Date: Sat, 1 Mar 2008 19:29:10 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Tecnique and/or Modules for Perl and HTML
Message-Id: <6dno95-i0b.ln1@osiris.mauzo.dyndns.org>
Quoth pgodfrin <pgodfrin@gmail.com>:
> On Mar 1, 10:42 am, Ron Bergin <r...@i.frys.com> wrote:
> > On Mar 1, 7:42 am, pgodfrin <pgodf...@gmail.com> wrote:
> >
> > > I'd like to (at first) read from database (using DBI) and display the
> > > contents on a web page. The trick is I want to do it on the same page
> > > that the action button resides and pop the output of the perl program
> > > into a scrollable box. Nothing fancy - just output some data from a
> > > database call. Eventually I'd like to be able to pass variables from a
> > > form to the waiting perl program
> >
> > Have you looked at the CGI module? The synopsis section will give you
> > a hint on part of what you need. By the time you finish reading the
> > doc, you should have a pretty good idea where to start. After you've
> > made an attempt, post back with specific questions and example code
> > that demonstrates the problem that you're
> having.http://search.cpan.org/~lds/CGI.pm-3.33/CGI.pm
>
> Yep - I've even run the samples. Perhaps it's a conceptual thing, but
> it seems to me that the CGI module is all encompassing. When it's run
> - it must produce the entire web page. Which is not what I think I
> want.
It is, though :). Except for modern JS-based techniques like Ajax, which
I would suggest you avoid for now, the way the web works is whole pages
at a time. If you re-read the docs for CGI.pm, you will find it has lots
of methods for re-requesting the current page with something slightly
different, and for preserving your program's state across several
invocations of the 'same' page.
Try this. It's not necessarily a good example of how to use CGI, the
HTML-generation is very simple-minded, and of course passing anything
to 'eval' is just downright stupid; but it should give you an idea of
how such programs work.
#!/usr/bin/perl -T
use strict;
use warnings;
use CGI;
my $Q = CGI->new;
my $url = $Q->url;
print $Q->header;
print <<HTML;
<html>
<head><title>Calculator</title></head>
<body>
<h2>Enter an arithmetic expression:</h2>
<form action="$url" method="POST">
<input name="eval"> <input type="submit" value="Eval">
</form>
HTML
if (my $expr = $Q->param('eval')) {
print <<HTML;
<!-- $expr -->
HTML
if (my ($safe) = $expr =~ m{^([-+/*()\d. ]+)$}) {
my $val = eval $safe;
if (defined $val) {
print <<HTML;
<p>Result: $val.</p>
HTML
}
else {
print <<HTML;
<p style="color: red;">Syntax error: $@</p>
HTML
}
}
else {
print <<HTML;
<p style="color: red;">Bad input.</p>
HTML
}
}
print <<HTML;
</body>
</html>
HTML
__END__
Ben
------------------------------
Date: Sat, 1 Mar 2008 14:36:23 -0800 (PST)
From: pgodfrin <pgodfrin@gmail.com>
Subject: Re: Tecnique and/or Modules for Perl and HTML
Message-Id: <9894764a-3387-41fa-a27e-545bf71e0bee@s12g2000prg.googlegroups.com>
On Mar 1, 1:29 pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth pgodfrin <pgodf...@gmail.com>:
>
>
>
> > On Mar 1, 10:42 am, Ron Bergin <r...@i.frys.com> wrote:
> > > On Mar 1, 7:42 am, pgodfrin <pgodf...@gmail.com> wrote:
>
> > > > I'd like to (at first) read from database (using DBI) and display the
> > > > contents on a web page. The trick is I want to do it on the same page
> > > > that the action button resides and pop the output of the perl program
> > > > into a scrollable box. Nothing fancy - just output some data from a
> > > > database call. Eventually I'd like to be able to pass variables from a
> > > > form to the waiting perl program
>
> > > Have you looked at the CGI module? The synopsis section will give you
> > > a hint on part of what you need. By the time you finish reading the
> > > doc, you should have a pretty good idea where to start. After you've
> > > made an attempt, post back with specific questions and example code
> > > that demonstrates the problem that you're
> > having.http://search.cpan.org/~lds/CGI.pm-3.33/CGI.pm
>
> > Yep - I've even run the samples. Perhaps it's a conceptual thing, but
> > it seems to me that the CGI module is all encompassing. When it's run
> > - it must produce the entire web page. Which is not what I think I
> > want.
>
> It is, though :). Except for modern JS-based techniques like Ajax, which
> I would suggest you avoid for now, the way the web works is whole pages
> at a time. If you re-read the docs for CGI.pm, you will find it has lots
> of methods for re-requesting the current page with something slightly
> different, and for preserving your program's state across several
> invocations of the 'same' page.
>
> Try this. It's not necessarily a good example of how to use CGI, the
> HTML-generation is very simple-minded, and of course passing anything
> to 'eval' is just downright stupid; but it should give you an idea of
> how such programs work.
>
> #!/usr/bin/perl -T
>
> use strict;
> use warnings;
> use CGI;
>
> my $Q = CGI->new;
> my $url = $Q->url;
>
> print $Q->header;
>
> print <<HTML;
> <html>
> <head><title>Calculator</title></head>
> <body>
> <h2>Enter an arithmetic expression:</h2>
> <form action="$url" method="POST">
> <input name="eval"> <input type="submit" value="Eval">
> </form>
> HTML
>
> if (my $expr = $Q->param('eval')) {
>
> print <<HTML;
> <!-- $expr -->
> HTML
>
> if (my ($safe) = $expr =~ m{^([-+/*()\d. ]+)$}) {
> my $val = eval $safe;
> if (defined $val) {
> print <<HTML;
> <p>Result: $val.</p>
> HTML
> }
> else {
> print <<HTML;
> <p style="color: red;">Syntax error: $@</p>
> HTML
> }
> }
> else {
> print <<HTML;
> <p style="color: red;">Bad input.</p>
> HTML
> }
>
> }
>
> print <<HTML;
> </body>
> </html>
> HTML
>
> __END__
>
> Ben
Hi Ben,
Always good to hear from you. I feel like - forgive the expression -
you're my fairy godmother... <grin>
I will read the docs a little closer - I must have been looking for a
silver bullet and peeked at the doc in a somewhat lazy manner.
thanks, as always,
pg
------------------------------
Date: Sat, 1 Mar 2008 19:16:35 -0800 (PST)
From: pgodfrin <pgodfrin@gmail.com>
Subject: Re: Tecnique and/or Modules for Perl and HTML
Message-Id: <70e5f6b3-c4a1-4b28-a9d8-3baedefab0ec@s13g2000prd.googlegroups.com>
On Mar 1, 4:36 pm, pgodfrin <pgodf...@gmail.com> wrote:
> On Mar 1, 1:29 pm, Ben Morrow <b...@morrow.me.uk> wrote:
>
>
>
> > Quoth pgodfrin <pgodf...@gmail.com>:
>
> > > On Mar 1, 10:42 am, Ron Bergin <r...@i.frys.com> wrote:
> > > > On Mar 1, 7:42 am, pgodfrin <pgodf...@gmail.com> wrote:
>
> > > > > I'd like to (at first) read from database (using DBI) and display the
> > > > > contents on a web page. The trick is I want to do it on the same page
> > > > > that the action button resides and pop the output of the perl program
> > > > > into a scrollable box. Nothing fancy - just output some data from a
> > > > > database call. Eventually I'd like to be able to pass variables from a
> > > > > form to the waiting perl program
>
> > > > Have you looked at the CGI module? The synopsis section will give you
> > > > a hint on part of what you need. By the time you finish reading the
> > > > doc, you should have a pretty good idea where to start. After you've
> > > > made an attempt, post back with specific questions and example code
> > > > that demonstrates the problem that you're
> > > having.http://search.cpan.org/~lds/CGI.pm-3.33/CGI.pm
>
> > > Yep - I've even run the samples. Perhaps it's a conceptual thing, but
> > > it seems to me that the CGI module is all encompassing. When it's run
> > > - it must produce the entire web page. Which is not what I think I
> > > want.
>
> > It is, though :). Except for modern JS-based techniques like Ajax, which
> > I would suggest you avoid for now, the way the web works is whole pages
> > at a time. If you re-read the docs for CGI.pm, you will find it has lots
> > of methods for re-requesting the current page with something slightly
> > different, and for preserving your program's state across several
> > invocations of the 'same' page.
>
> > Try this. It's not necessarily a good example of how to use CGI, the
> > HTML-generation is very simple-minded, and of course passing anything
> > to 'eval' is just downright stupid; but it should give you an idea of
> > how such programs work.
>
> > #!/usr/bin/perl -T
>
> > use strict;
> > use warnings;
> > use CGI;
>
> > my $Q = CGI->new;
> > my $url = $Q->url;
>
> > print $Q->header;
>
> > print <<HTML;
> > <html>
> > <head><title>Calculator</title></head>
> > <body>
> > <h2>Enter an arithmetic expression:</h2>
> > <form action="$url" method="POST">
> > <input name="eval"> <input type="submit" value="Eval">
> > </form>
> > HTML
>
> > if (my $expr = $Q->param('eval')) {
>
> > print <<HTML;
> > <!-- $expr -->
> > HTML
>
> > if (my ($safe) = $expr =~ m{^([-+/*()\d. ]+)$}) {
> > my $val = eval $safe;
> > if (defined $val) {
> > print <<HTML;
> > <p>Result: $val.</p>
> > HTML
> > }
> > else {
> > print <<HTML;
> > <p style="color: red;">Syntax error: $@</p>
> > HTML
> > }
> > }
> > else {
> > print <<HTML;
> > <p style="color: red;">Bad input.</p>
> > HTML
> > }
>
> > }
>
> > print <<HTML;
> > </body>
> > </html>
> > HTML
>
> > __END__
>
> > Ben
>
> Hi Ben,
> Always good to hear from you. I feel like - forgive the expression -
> you're my fairy godmother... <grin>
>
> I will read the docs a little closer - I must have been looking for a
> silver bullet and peeked at the doc in a somewhat lazy manner.
>
> thanks, as always,
> pg
sim-sala-bim...
I have seen the light...
I think I'm gonna start calling you Yoda...
pg
------------------------------
Date: Sat, 1 Mar 2008 15:17:28 -0800 (PST)
From: Bennett Haselton <bennett@peacefire.org>
Subject: use CGI::Carp qw(fatalsToBrowser) not sending file handle errors to browser
Message-Id: <0226b3d2-96c8-4933-9c00-008ed95fd519@d4g2000prg.googlegroups.com>
I wrote the following script to test whether, if I try writing to an
invalid filehandle, the error message would get sent to the browser:
>>>
#!/usr/bin/perl
use strict;
use IO::File;
use CGI::Carp qw(fatalsToBrowser);
print "Content-type: text/html\n\n";
# This will not open a valid filehandle since the script runs as
nobody
my $fh = IO::File->new("> doesnotexist.txt");
print $fh "foo";
die "Die here, if you get this far";
>>>
The line 'print $fh "foo";' does generate an error, however that error
gets written to /var/log/httpd/error_log where it says "Can't use an
undefined value as a symbol reference at /var/www/html/carptest/open-
wrong-file-with-carp-fatalstobrowser.cgi line 11". I want that sent
to the *browser*, not written to the log file. The line 'die "Die
here, if you get this far";' never gets executed.
The CGI::Carp page says that "Nonfatal errors will still be directed
to the log file only". But isn't the file handle error a *fatal*
error, since it caused the script to exit before getting to the 'die
"Die here, if you get this far";' line? And if it's a fatal error,
why didn't it get sent to the browser?
------------------------------
Date: 02 Mar 2008 02:57:51 GMT
From: xhoster@gmail.com
Subject: Re: use CGI::Carp qw(fatalsToBrowser) not sending file handle errors to browser
Message-Id: <20080301215754.859$TX@newsreader.com>
Bennett Haselton <bennett@peacefire.org> wrote:
> I wrote the following script to test whether, if I try writing to an
> invalid filehandle, the error message would get sent to the browser:
>
> >>>
> #!/usr/bin/perl
>
> use strict;
> use IO::File;
> use CGI::Carp qw(fatalsToBrowser);
>
> print "Content-type: text/html\n\n";
>
> # This will not open a valid filehandle since the script runs as
> nobody
> my $fh = IO::File->new("> doesnotexist.txt");
> print $fh "foo";
>
> die "Die here, if you get this far";
> >>>
>
> The line 'print $fh "foo";' does generate an error, however that error
> gets written to /var/log/httpd/error_log where it says "Can't use an
> undefined value as a symbol reference at /var/www/html/carptest/open-
> wrong-file-with-carp-fatalstobrowser.cgi line 11". I want that sent
> to the *browser*, not written to the log file. The line 'die "Die
> here, if you get this far";' never gets executed.
I can't replicate your problem.
perl -le 'use CGI::Carp; print $CGI::Carp::VERSION'
1.29
perl -le 'use CGI::Carp qw(fatalsToBrowser); use IO::File;
my $fh=IO::File->new("/fooasdf"); print $fh "foo"'
Content-type: text/html
<h1>Software error:</h1>
<pre>Can't use an undefined value as a symbol reference at -e line 1.
...
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 1325
***************************************