Trying to make sense of perl for web development.

C

cendrizzi

Ok, I'm trying to learn perl for a potential position.

I would consider myself a very experienced php programmer with a
little exposure to perl.

As far as syntax goes, perl seems pretty easy (some things I even like
a lot). I have an ubuntu feistry server that I've setup with
mod_perl. It works great for parsing simple stuff but now I'm ready
to extend the functionality a bit and want to connect to postgresql,
etc.

Admittedly this is more a systems things than a programming thing but
I need to get things in place so I can program and learn. First of
all, how does mod perl work with the cpan packages? When I install
something using cpan should I be able to use that module (ie "use
module::module") immediately after? So far that has not been my
experience. Furthermore, can I use the cgi stuff in cpan when I'm
using mod_perl, like cgi::ajax?? I've just never had to deal with all
this with php so it's feeling very overwhelming in how it all works
together (I'm trying to read books and have been googling for hours to
make sense of it).

Finally, and most frustrating is getting error or warning feedback.
Where do I go? I currently am trying to connect to my db with:
$dbh = DBI->connect("dbi:pg:dbname=ecommerce;host=127.0.0.1",
"cendrizzi", "************", {AutoCommit => 0});

I then try to do stuff with $dbh and nothing happens but I don't get
errors either. I have no idea what is going on or if I can even
successfully connect to the db?! In PHP you can turn on errors and
warnings to display them in the page, I'm sure Perl is just
different. How can I troubleshoot this stuff?

Thanks for any help you can provide me with.
 
M

Mumia W.

Ok, I'm trying to learn perl for a potential position.

I would consider myself a very experienced php programmer with a
little exposure to perl.

As far as syntax goes, perl seems pretty easy (some things I even like
a lot). I have an ubuntu feistry server that I've setup with
mod_perl. It works great for parsing simple stuff but now I'm ready
to extend the functionality a bit and want to connect to postgresql,
etc.

Admittedly this is more a systems things than a programming thing but
I need to get things in place so I can program and learn. First of
all, how does mod perl work with the cpan packages? When I install
something using cpan should I be able to use that module (ie "use
module::module") immediately after?

If you use the ModPerl::Registry handler, you probably have to restart
the web server. ModPerl::perlRun will probably be more flexible and
recognize newly installed modules without requiring a server restart.

I'm not sure of this--test it.
So far that has not been my
experience. Furthermore, can I use the cgi stuff in cpan when I'm
using mod_perl, like cgi::ajax??

Mod_perl attempts to emulate a CGI environment, but it's not perfect. I
haven't used CGI::Ajax, but it may be one of the CGI modules that hasn't
been changed to suit mod_perl.
I've just never had to deal with all
this with php so it's feeling very overwhelming in how it all works
together (I'm trying to read books and have been googling for hours to
make sense of it).

Finally, and most frustrating is getting error or warning feedback.
Where do I go? I currently am trying to connect to my db with:
$dbh = DBI->connect("dbi:pg:dbname=ecommerce;host=127.0.0.1",
"cendrizzi", "************", {AutoCommit => 0});

I then try to do stuff with $dbh and nothing happens but I don't get
errors either. I have no idea what is going on or if I can even
successfully connect to the db?! In PHP you can turn on errors and
warnings to display them in the page, I'm sure Perl is just
different. How can I troubleshoot this stuff?

Thanks for any help you can provide me with.

Script errors should appear in the Apache error_log. If you want these
error messages to appear in the browser, use the CGI::Carp module. Read
the documentation for that module: "perldoc CGI::Carp"
 
P

Paul Lalli

Finally, and most frustrating is getting error or warning feedback.
Where do I go? I currently am trying to connect to my db with:
$dbh = DBI->connect("dbi:pg:dbname=ecommerce;host=127.0.0.1",
"cendrizzi", "************", {AutoCommit => 0});

I then try to do stuff with $dbh and nothing happens but I don't
get errors either.

You didn't ask for any errors. Either check the return value of
connect() explicitly:
my $dbh = DBI->connect(...) or die "Connect failed: $DBI::errstr";
or turn on RaiseError
my $dbh = DBI->connect($dsn, $user, $pass, { RaiseError => 1 });

See the DBI documentation.
I have no idea what is going on or if I can even
successfully connect to the db?! In PHP you can turn on errors and
warnings to display them in the page, I'm sure Perl is just
different.

You can with Perl as well. You want the CGI::Carp module, which is
standard in Perl.

use CGI qw/:standard/;
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
print header;
warningsToBrowser(1);

Again, see the CGI::Carp documentation.

This would also be a good time to familiarize yourself with the Perl
FAQ. From a command line, type
perldoc perlfaq
for the full list, or do a search via
perldoc -q <search_term>
For example:

$ perldoc -q CGI
Found in /software/perl-5.8.5-0/pkg/lib/5.8.5/pod/perlfaq3.pod
How can I get better error messages from a CGI program?

Paul Lalli
 
C

cendrizzi

Wow, thanks guys!

I will do this for sure. For someone newer to perl it is somewhat
bewildering (and exciting) how many modules exist. Just looking for
postgres drivers made me feel overwhelmed.

BTW, is mod_perl the best way to go? It seems from most things I've
read to have some nice advantages.
 
K

krakle

BTW, is mod_perl the best way to go? It seems from most things I've
read to have some nice advantages.

Only you can decide that... Some people who just want some speed
increase will probably enjoy FastCGI since there's "less to be
concerned with" than mod_perl.

But if you want to be able to create Apache modules and/or extend/
modify the apache life cycle PLUS enjoy the speed advantages you will
want to stick with mod_perl.

I'm glad I did. mod_perl also forces you to write CLEAN and STRICT
Perl code. Which is a good thing but at the same time makes it more
difficult for a "newbie".
 
T

trudge

Ok, I'm trying to learn perl for a potential position.

I would consider myself a very experienced php programmer with a
little exposure to perl.

As far as syntax goes, perl seems pretty easy (some things I even like
a lot). I have an ubuntu feistry server that I've setup with
mod_perl. It works great for parsing simple stuff but now I'm ready
to extend the functionality a bit and want to connect to postgresql,
etc.

Admittedly this is more a systems things than a programming thing but
I need to get things in place so I can program and learn. First of
all, how does mod perl work with the cpan packages? When I install
something using cpan should I be able to use that module (ie "use
module::module") immediately after? So far that has not been my
experience. Furthermore, can I use the cgi stuff in cpan when I'm
using mod_perl, like cgi::ajax?? I've just never had to deal with all
this with php so it's feeling very overwhelming in how it all works
together (I'm trying to read books and have been googling for hours to
make sense of it).

Finally, and most frustrating is getting error or warning feedback.
Where do I go? I currently am trying to connect to my db with:
$dbh = DBI->connect("dbi:pg:dbname=ecommerce;host=127.0.0.1",
"cendrizzi", "************", {AutoCommit => 0});

I then try to do stuff with $dbh and nothing happens but I don't get
errors either. I have no idea what is going on or if I can even
successfully connect to the db?! In PHP you can turn on errors and
warnings to display them in the page, I'm sure Perl is just
different. How can I troubleshoot this stuff?

Thanks for any help you can provide me with.

The first thing you should do is get in the habit of adding the
following lines to the top of all your Perl scripts:
use strict;
use warnings;

You will see this advice given to newcomers constantly, and you are
wise to heed it, if you are not already doing so.

One little trick I do to help me in the development stages is to
redirect the error messages to a file in the same directory as the
script, instead of the Apache log files.

Near the beginning of each script I add:
BEGIN
{
open (STDERR,">>$0-err.txt");
print STDERR "\n",scalar localtime,"\n";
}

This block of code creates a file in the same directory the script is
in, thus making it very easy to find. Also, '$0' is the name of the
current script, so if your script is called 'login.pl', this error
file is called 'login.pl-err.txt', making it even easier to find. This
has the added advantage that if you change the name of the script, you
don't have to edit this block of code. Once your script is working
with no errors, comment out the block of code.

Since I don't know what your situation is, I can't say whether you
should be using mod_perl or not, but if you are just starting out with
Perl, I would say leave it for now - learn to walk first. I have not
yet found a situation where I needed to run mod_perl (which of course
may not be saying much, but it may be that you can get a lot done
without it as well).

A few good books on my shelf: "Perl Cookbook"; "Programming Perl";
"Perl Black Book"; "MySQL and Perl for the Web".
 
M

Michele Dondi

successfully connect to the db?! In PHP you can turn on errors and
warnings to display them in the page, I'm sure Perl is just
different. How can I troubleshoot this stuff?

In Perl, you can do it quite easily. Cool stuff off CPAN as usual. But
the practice is generally *discouraged* except perhaps in development
phases. Which brings us back to the (PHP => rookie, Perl => hacker)
kinda flame... :)


Michele
 
M

Michele Dondi

I will do this for sure. For someone newer to perl it is somewhat

"this"?!? (Hint: *please* quote some trimmed content from the posts
you're replying to.)
bewildering (and exciting) how many modules exist. Just looking for
postgres drivers made me feel overwhelmed.

Hehe!

: CPAN is my programming language of choice; the rest is just syntax.
: - Autrijus Tang, Perl 6 is Here Today
: (http://pugscode.org/talks/apw/start.html)
BTW, is mod_perl the best way to go? It seems from most things I've
read to have some nice advantages.

Not a webby kinda guy by any means, but from discussions I could
recollect I've seen experienced hackers expressing very good comments
about FastCGI and lighttpd.


Michele
 
J

Jamie

In said:
Wow, thanks guys!

I will do this for sure. For someone newer to perl it is somewhat
bewildering (and exciting) how many modules exist. Just looking for
postgres drivers made me feel overwhelmed.

That is (IMO) one of the advantages over PHP, while PHP sort of thrusts
all these functions into the program, Perl uses modules and name spaces
so that you don't have collisions.
BTW, is mod_perl the best way to go? It seems from most things I've
read to have some nice advantages.

mod_perl is excellent if you need low level access to the web server. I use
it sometimes.

I'm not terribly crazy about it for most things as it can really ruin your day,
it sits in your web server whether you're using the application or not. (this
can be a significant issue if you have several applications, some of them being
run once or twice a day or even once a month)

FastCGI is really nice, it's also a fair bit easier to design a FastCGI
application to be compatible with regular CGI. This way, you can start
out with CGI and if the application is hit a lot, move over to FastCGI
for only those things that need it.

I find regular old CGI to be pretty darn good on modern computers, (you may
wish to have a perl binary running some place, perhaps just sitting there
doing nothing) most UNIX's can take advantage of an existing perl binary
in memory and it seems to run pretty quick. (shared forking I'd imagine)

I wouldn't use CGI for programs that are requested 100 times a second though...

Perl's AUTOLOAD can be turned into an excellent tool for optimizing vanilla
CGI. Loading only subs that are needed for any particular request. (though, this
can come back to make a mess for you if you switch to FastCGI/mod_perl) PHP
has an AUTOLOAD, but, the way PHP does it is via the CLASS name, perl can do
it as granular as a SUB name.

One thing you'll discover when using mod_perl (or FastCGI) is that it is
possible to hang on to complex data structures for multiple requests. This is
excellent for parsing configuration files, as it only needs to be done ONCE
during the entire duration of the program. (so each page load doesn't need to
"reload" configuration) Downside, of course, is that all this memory will be
used up, not so good for once in awhile scripts and can be a serious problem if
it contains information you DON'T want shared between page requests so you
have to be extra careful about that.

With PHP, this memory sharing wasn't usually an option.

Hmm.. didn't mean to write a speech. Sorry!

Jamie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top