How do I create CGI scripts with Perl? - perl

I am creating a website and done with some HTML stuff, but I am thinking to create site using CGI and Perl scripting. I don't have much idea on CGI scripting. Can anyone please suggest me how to create a CGI script and how to create web pages with that?

Try Ovid's CGI Course - it's a good start. "A Beginner's Introduction to Perl Web Programming" is also a good reading, but with less information.
Later you can try framework like CGI::Application - it should be easy even for a beginner.

Have a look at A Beginner's Introduction to Perl Web Programming on perl.com.
I would recommend you make use of tools like Template Toolkit or Markapl to make the process of producing and maintaining your HTML view much simpler.
And moving forward you might want to consider looking at using a web framework ranging from something very light like Squatting to a powerful beast like Catalyst.

CGI is a standard to interface a webserver and a content provider or external application (let's say a Perl script, a compiled program), I think you better study Perl to code some CGI.
This link is a beginner guide to get started with CGI coding in Perl.
Also if you are beginner in Perl you might look to some other scripting language more dedicated to web, the best example is PHP. But if you are already experienced Perl CGI, the CGI.pm is nice also.

As far as I understand your question, you say that you wrote your own web server and now want to implement CGI!?
These links should help you:
CGI Specification
How to implement CGI in a Web Server
Edit: Check the original question before downvoting me.

Related

Writing a MVC application with Perl CGI

I have a client who wants me to design a new web app from scratch
The problem is that he wants me to use only core modules that come with perl (5.10 or 5.12)
Is there a way to write MVC apps with just CGI?
I know about Catalyst, Mojolicious and Dancer and how easy it is to do MVC with them, but I have no clue on how to do it with CGI alone
Are there any code examples that I could see and inspire from? (I've already googled but didn't find anything that I could use)
Also, like with Mojo and Dancer, is there any way I can generate links (Routes from Mojo, and Rails) from CGI?
Thanks
First off, what's the reason for the "core modules only" restriction? Does that also mean that you can't write any new modules of your own? The most effective solution would undoubtedly be to convince the client to let you use CPAN.
If you're allowed to write your own non-core modules, would you be able to get away with including a new module named "Prancer" which looks suspiciously like Dancer? (i.e., Grab the Dancer source tree and s/Dancer/Prancer/g across the whole thing, then add it to your project.)
But, if all else fails... yes, it's possible to at least use MVC principles and strong separation of concerns under CGI.pm, although you won't have an actual framework helping you with it unless you write that framework yourself. You'll also have to write any database abstraction layer and templating engine yourself, too. I know this is possible because I was doing it myself 5-10 years ago, back in the Dark Ages before I switched to Dancer. I can't offer any examples, though, because I've successfully forgotten where I buried those bodies.

Web Programming For The Non-Web Programmer (in Perl)

I am looking to start Web Programming in Perl (Perl is the only language I know). The problem is, I have no prior knowledge of anything to do with the web, except surfing it. I have no idea where to start.
So my question(s) is...
Where do I start learning Web Programming? What should I know? What should I use?
I thank everybody in advance for answering and helping.
The key things to understand are:
What you can send to browsers
… or rather, the things you intend to send to browsers, but having an awareness of what else is out there is useful (since, in complex web applications in particular, you will need to select appropriate data formats).
e.g.
HTML
CSS
JavaScript
Images
JSON
XML
PDFs
When you are generating data dynamically, you should also understand the available tools (e.g. the Perl community has a strong preference for TT for generating HTML, but there are other options such as Mason, while JSON::Any tends to be my goto for JSON).
Transport mechanisms
HTTP (including what status codes to use and when, how to do redirects, what methods (POST, GET, PUT, etc) to use and when).
HTTPS (HTTP with SSL encryption)
How to get a webserver to talk to your Perl
PSGI/Plack if you want modern and efficient
CGI for very simple
mod_perl if you want crazy levels of power (I've seen someone turn then Apache HTTPD into an SMTP spam filter using it).
Security
How to guard against malicious input (which basically comes down to knowing how to take data in one format (such as submitted form data) and convert it to another (such as HTML or SQL).
Web Frameworks
You can push a lot of work off to frameworks, which provide structured ways to organise a web applications.
Web::Simple is simple
Dancer seems to be holding the middle ground (although I have to confess that I haven't had a chance to use it yet)
Catalyst probably has the steepest learning curve but comes with a lot of power and plugins.
Dependent on complexity of your project, you could have a look at Catalyst MVC. This is a good framework, messing up with the most request stuff, but gives you enough in deep view whats going on.
There is a good tutorial in CPAN
If you want to start with mod_perl or CGI, there are also some Tutorials :
mod_perl
CGI Doc
If you're looking to try some web programming in Perl, you could try hosting a Dancer app for free on OpenShift Express.
There's even a "Dancer on OpenShift Express" repo to get you started: https://github.com/openshift/dancer-example

How can I automate website testing with Perl? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Integration Testing for a Web App
I've recently delved into Perl and I'd like to learn how to automate web site testing. To that end I'd appreciate answers to the following:
What modules from CPAN should I look into? Is there something similar to Watir (I know there's a Windows only port of it in Perl)?
I'm using a Windows system, so does it matter if I use ActivePerl or Strawberry Perl?
What books should I look into (aside from "Perl Testing: A Developer's Notebook")?
Edit:
The web application is written in Java
I want to test the application's web GUI, which makes use of javascript/ajax & some flex/flash
WWW::Mechanize, WWW::Selenium and LWP::Simple just to name a few.
ActivePerl is more user friendly than Strawberry when it comes to downloading CPAN modules, but at the end of the day they are both equally as good.
I haven't come across any books on the subject; perhaps a tutorial is what you need.
Are you interested testing the WEB GUI itself via http calls or individual Perl modules via calling Perl code directly?
For the former, Selenium - which AFAIK is not Perl specific at all - seems to be the accepted Best Practice (see Integration Testing for a Web App link helpfully provided by Ether ). It has a Perl CPAN module for integration, mentioned in the same SO question.
You can of course do your own testing frameworks, e.g. implement http calls via WWW::Mechanize. But that would not work with JavaScript-enabled web sites (are there any left that aren't?) since you need a JavaScript engine, either provided by the browser (Selenium's approach) or something embedded which AFAIK Perl doesn't actually have, though Java does.
However, there are non-http approaches to testing Perl side code of the web apps as well:
Have all non-presentation logic nicely modularized away, and test it as usual using Test::More or your other favorite test frameworks.
Test presentation (or presentation+logic) by using your web framework in command line mode, emulating running on web server. CGI.pm allows that, as well as EmbPerl. Not sure about Catalyst apps.

Prerequisites for Perl

I am new to developing a website using Perl CGI scripts.
What are the prerequisites for Perl?
Please give me some basic ideas or some good tutorials.
Do you mean: what prerequisite knowledge would help you learn Perl?
The Perl language grew out of the best features of the Unix commands grep, awk, sed, and shell scripts, plus some C. If you are strong in those areas, Perl will seem like a good fit. You'd already be used to cryptic variable names, quick-n-dirty looping constructs, dynamic typing, regular expressions, and standard input/output.
First decide if you are writing a CGI script or an Apache handler. The 2 are coded completely differently, but both have their advantages and disadvantages.
If you want to run your script using CGI (or FCGI), I recommend this tutorial for beginning programmers. It will help you through creating simple forms. If you already know a bit about programming a script language, you will be able to skim-read a lot of that. If you are a new programmer, you might also want zo look up a few alternatives, like coding CGI scripts with Python, a language often thought to be better suited to new programmers.
I would point you towards learn.perl.org, the Modern Perl book by chromatic, and the overview of web frameworks on the Perl 5 wiki. Also make sure that the tutorials and articles you use are relatively recent. Perl has been around for a long time and modern Perl is different from what people wrote 10 years ago.
I suggest starting with PSGI. It is somewhat new (compared to mod_perl), but it's Perl's answer to Python WSGI. Most of the large frameworks support it, as does other interesting projects like Starman. I would start there and Google anything I don't understand.

How do I develop web 2.0 apps with CGI.pm?

A few years ago I did a lot of work with CGI.pm. I'm evaluating using it again for a quick project. Can someone bring me up to speed on the current state of developing with CGI.pm in the "Web 2.0" world? What are the best libraries on CPAN to use with it? Are there clean ways to include jQuery, YUI, other CSS libs, etc, and do some AJAX. There are of course lots of libraries on CPAN but what works and what is commonly used?
We aren't still doing this?
$JSCRIPT<<EOF;
...
EOF
I realize people are going to offer Catalyst as an answer. However, many people may have legacy CGI.pm apps they simply want to enhance. Is starting over really the best answer?
Personally, I'm no fan of Catalyst (too heavy for my taste) or Mason (mixing code and HTML is bad ju-ju), but I do quite well using CGI.pm for input[1], HTML::Template for output, and CGI::Ajax to provide AJAX functionality where called for.
If you're looking at frameworks, you may also want to consider CGI::Application, which is a widely-used and lighter-weight alternative to Catalyst/Mason.
[1] I can't recall the last time I called anything other than $q->param or $q->cookie from CGI.pm. There are still a lot of tutorials out there saying to use its HTML-generation functions, but that's still mixing code and HTML in a way that's just as bad as using here docs, if not worse.
Consider using something more modern, for example Catalyst. It will make your life much easier and you won't have to reinvent the wheel. I understand that it is just a small project, but from my experience many small projects in time become large ones :)
The "web 2.0" apps that I've worked with usually use client-side JavaScript to request JSON data from the server, then use that data to update the page in-place via DOM.
The JSON module is useful for returning structured data to a browser.
As far as including JavaScript, HTML, or whatever in a here doc - that was never a good idea, and still isn't. Instead, use one of the plethora of template modules to be found on CPAN. For a CGI, I'd avoid "heavy" modules like Mason or Template Toolkit, and use a lighter module for quicker startup, such as Text::Template, or Template::Simple.
Yes, you can write perfect web2.0 web applications WITHOUT using any framework on the server side in any language Perl, Python, Java, etc and WITHOUT using any JavaScript libraries/framework on the client side. The definition of web 2.0 is kind of a loose definition, and I'm guessing by web2.0, you mean Ajax or partial page refresh, then all you would really need is to focus on the following:
Know about the XmlHttpRequest object.
Know how to return JSON object from the server to the client.
Know how to safely evaluate/parse the JSON object using JavaScript and know to manipulate the DOM. Also, at least know about innerHTML. InnerHTML is helpful occasioanally.
Know CSS.
Having said that, it's a lot easier to use some framework on the server side, but not because it's required by web2.0 and it's a lot easier to use some JavaScript on the client like jQuery, mootools, YUI. And you can mix-and-match depends on your needs and your tastes. Most JavaScript provides wrapper around the XmlHttpRequest so that it works across all browsers. No one write "naked" XmlHttpRequest anymore, unless you want to show some samples.
It's perfectly possible to write "Web 2.0" apps using CGI.pm, but you'll have to do the work yourself. From what I've seen, the focus in the Perl development community has been on developing successor frameworks to CGI, not on writing helper modules to let legacy apps get bootstrapped into modern paradigms. So you're somewhat on your own.
As to whether to start over, what are you really trying to accomplish? Everyone's definition of "Web 2.0" is somewhat different.
If you're trying to introduce a few modern features (like AJAX) to a legacy app, then there's no reason you need to start over.
On the other hand if you're trying to write something that truly looks, feels, and works like a modern web app (for example, moving away from the page-load is app-state model), you should probably consider starting from the ground up. Trying to make that much of a transformation happen after the fact is going to be more trouble than it's worth for anything but the most trivial of apps.
I agree with Adam's answer, you probably want to use Catalyst. That being said, if you really don't want to, there's nothing preventing you from using only CGI.pm. The thing is, Catalyst is a collection of packages that do the things you want to make Web 2.0 easy. It combines the various templating engines such as Template Toolkit or Mason with the various ORM interfaces like DBIx::Class and Class::DBI.
Certainly you don't have to use these things to write Web 2.0 apps, it's just a good idea. Part of your question is wondering if javascript and CSS frameworks like jQuery, or prototype require anything from the server-side code. They don't, you can use them with any kind of server-side code you want.
For new apps, if you don't find Catalyst to your taste, Dancer is another lightweight framework you may like. There are also plenty of others, including CGI::Simple, Mojo/Mojolicious, Squatting...
Any of these lightweight frameworks can take care of the boring parts of web programming for you, and let you get on with writing the fun parts the way you want to.
If the jump from CGI.pm to Catalyst seems too daunting then perhaps something like Squatting might be more appropriate?
Squatting is a web microframework and I have found it ideal for quick prototyping and for replacing/upgrading my old CGI scripts.
I have recently built a small "web 2.0" app with Squatting using jQuery with no issues at all. Inside the CPAN distribution there is an example directory which contains some programs using jQuery and AJAX including a very interesting [COMET](http://en.wikipedia.org/wiki/Comet_(programming)) example which makes use of Continuity (which Squatting "squats" on by default).
NB. If required then you can later "squat" your app onto Catalyst with Squatting::On::Catalyst
There is also CGI::Ajax.