Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Syntactic Confectionery Delight
 
PerlMonks  

Creating a web application in Perl

by Anonymous Monk
on Jan 07, 2012 at 08:11 UTC ( #946728=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have been assigned a task to:

Create a web application using any technology that accepts an email address and checks if the domain of the email address exists.

I can make code for that in Perl by splitting out the part before @ and storing domain name in a scalar variable and then ping the domain name and checking the reply. It will solve the problem. But the problem is that I can not make a web application. I do not know that much Perl. I do not know CGI. I have to make and deliver this web application within 72 hours. Let me know which thing should I learn? If you could provide me good resources then that would be very kind. Thank you!

Comment on Creating a web application in Perl
Re: Creating a web application in Perl
by Anonymous Monk on Jan 07, 2012 at 08:26 UTC
Re: Creating a web application in Perl
by planetscape (Canon) on Jan 07, 2012 at 08:58 UTC
Re: Creating a web application in Perl
by tobyink (Abbot) on Jan 07, 2012 at 09:45 UTC

    Pinging a domain name is not an especially good indicator - there are a number of scenarios where a domain name will not respond to pings, but can accept mail.

    • Firewall set up to allow incoming traffic on TCP 25 (the port for SMTP) but block ICMP traffic (e.g. pings). Many firewalls block ICMP traffic by default.
    • Yo-yo server - because of the way sending mail servers keep retrying mail, the receiving end only needs to be alive 50% of the time to offer a fairly reliable mail service. So even if the server is down (and not responding to pings), you could still send e-mail to it.
    • A DNS MX record exists for a host but no A/CNAME record. Given the following DNS configuration:
      example.org.      MX 10    mail.example.org.
      mail.example.org. A        10.10.10.1.
      www.example.org.  A        10.10.10.2.
      
      You will end up with a situation where mail.example.org and www.example.org will accept pings, but example.org will not accept pings (unknown host), yet addresses like joe@example.org can still be received.

    So don't ping the domain. Use a Perl module like Net::DNS::Resolver to look up an MX record for the host; if there is no MX record, then look for an A/AAAA/CNAME record; if you've still not found a matching DNS entry, then the domain name probably can't receive mail.

    use Net::DNS::Resolver; my $domain = 'example.com'; my $dns = Net::DNS::Resolver->new; my $valid = $dns->query($domain, 'MX') || gethostbyname($domain); printf( "You could %s send mail to %s.\n", ($valid ? "maybe" : "not"), $domain, );

      On a sidenote: Even if you the MX entry is valid, the mailserver is reachable and you are even to validate the email address by sending one one those "click here to validate" links, you can't besure it will work for more than a few minutes. There are more and more of this one-time email-address services available to avoid SPAM and all these annoying "you have registered at our site a few years ago so we constantly bombard you with our 'newsletter'" thingies.

      I'm currently teaching my private mailservers how to do that onetime address thingy for the same reasons...

      "Believe me, Mike, I calculated the odds of this succeeding against the odds I was doing something incredibly stupidů and I went ahead anyway." (Crow in "MST3K The Movie")
Re: Creating a web application in Perl
by CountZero (Chancellor) on Jan 07, 2012 at 10:32 UTC
    Install Dancer and you are already half-way.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My first reaction to this was that Dancer would be overkill for such a small task. I've been working with Dancer lately and like it a lot, but using a framework for what amounts to a 10-line CGI? But then I reconsidered and tried it. I can't say it saved me any time or lines of code, but it did keep things cleaner and more organized. The rule-based structure of Dancer makes it very easy to see what URI is involved. Keeping the code separate from the HTML makes it easier to write one and then the other, without the back-and-forth in my head that I tend to do when writing CGI with HTML embedded in it (as functions like h1() or as strings), which is nice even for a small task.

      And since I already had Dancer running and configured on this site, I could just add the rule and the template, without worrying about the path to the perl executable, or whether my CGI needed to go in the cgi-bin directory or have a particular extension, or any of that other fun stuff that made installing CGI scripts too difficult for most newbies. And if I want to expand on this utility later, to make it more robust by adding more logging and error checking, or perhaps to save the lookups in a database cache, the payoff from using Dancer in the first place will only grow as the complexity of the thing increases.

      So I'm sold: Dancer is even good for small stuff. I probably still won't use it when I need to write a one-line script to run a one-time command on a server where I don't have shell access, like `rm annoying_file_owned_by_webserver_that_I_can't_remove_by_FTP`; those will still go in quick CGIs (or PHP). But I think everything else will be done in Dancer if it can be.

      Aaron B.
      My Woefully Neglected Blog, where I occasionally mention Perl.

Re: Creating a web application in Perl
by JavaFan (Canon) on Jan 07, 2012 at 11:15 UTC
    I have to make and deliver this web application within 72 hours. Let me know which thing should I learn?
    Learn how to pray. And hope you pray to a entity that actually exists, and it is willing to perform some miracles for you.
      Papa Legba seems a good bet. In the Sprawl trilogy by William Gibson in the second book, Count Zero, Papa Legba stands at the gateway to cyberspace the "master of roads and pathways".

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Creating a web application in Perl
by TJPride (Pilgrim) on Jan 07, 2012 at 19:58 UTC
    use CGI qw(:standard); my $email = param('email');

    CGI scripts also need to have their permissions changed so they can be run properly when accessed via something other than the command line. Generally speaking, this means changing to 755 (read, write, execute for owner, read and execute for group and all).

    You may need to include the path to the Perl interpreter in the first line of the script. Maybe like so:

    #!/usr/bin/perl

    EDIT: And you need to print out a Content-Type before printing any other content:

    print "Content-Type: text/html\n\n";

    EDIT: One final note, you need to make dang sure that if you are using the command line to ping the domain (via backticks or whatever), you use regex to verify that nobody has inserted bad stuff into the email address, because otherwise they could essentially type all sorts of nasty stuff into the email and your script would run it for them.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://946728]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2014-04-25 01:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (579 votes), past polls