Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How to embed Perl in HTML

by html546897 (Initiate)
on Oct 09, 2011 at 05:06 UTC ( #930417=perlquestion: print w/ replies, xml ) Need Help??
html546897 has asked for the wisdom of the Perl Monks concerning the following question:

Can someone please give a detailed tutorial on how to embed Perl in HTML? This means give specific instructions on what to download, where to download it, what files I need to download, and how I would somehow put that into my HTML file.

My code is:

# "E:\My Vaults\My Vault\Perl\happy.pl" # Displays a warm greeting. print "What is your name? "; my $name = <STDIN>; chomp($name); print "Hello, $name! I am a computer! Austin is my author! He created +this Perl quiz!\n"; # User greets the computer. print "Please greet me so I know I am welcome.\n"; my $greet = <STDIN>; # Displays difference between ages. chomp($greet); print "$greet to you, too. What year were you born in, $name? "; my $dob = <STDIN>; if (($dob eq '')) { print "Hey! Why won't you answer?\n"; } if (($dob < 1996)) { my $difference = 1997 - $dob; chomp($difference); print "You are $difference years older than my author, Austin.\n"; } if (($dob > 1998)) { my $difference = $dob - 1997; chomp($difference); print "You are $difference years younger than my author, Austin.\n +"; } if (($dob == 1998)) { my $difference = $dob - 1997; chomp($difference); print "You are $difference year younger than my author, Austin.\n" +; } if (($dob == 1996)) { my $difference = 1997 - $dob; chomp($difference); print "You are $difference year older than my author, Austin.\n"; } if (($dob == 1997)) { print "You are the same age as my author, Austin!\n"; } # Asks favorite color. print "How many people live in your house with you? "; my $housenum = <STDIN>; if (($housenum == 5)) { print "5 people live in my author's house, too!\n"; } if (($housenum != 5)) { print "Oh. There are 5 people living in my author's house.\n"; } chomp($name); print "Good bye, $name!\n";

Thanks!

Comment on How to embed Perl in HTML
Download Code
Re: How to embed Perl in HTML
by anneli (Pilgrim) on Oct 09, 2011 at 05:35 UTC

    Hold up, hold up; what do you mean precisely by "embed Perl in HTML?" Once we work out what outcome you're looking for, we can better help you get there. (one possible interpretation is just to list code in HTML, as you've done on this very page!)

    Anne

      Well, I what I am saying is is there some way to put a Perl form in an HTML document instead of accessing it through the Command Prompt, and if there is - how do I do it?

        Aha. As others have suggested, the answer is CGI, mod_perl, or .. well, anything involving a web server, basically. HTML can't execute code (apart from perhaps client-side scripts, but AFAIK there's no client-side Perl interpreter (around)), but a web server can!

        Best of luck. Let us know if we can help you any further with this.

        Anne

Re: How to embed Perl in HTML
by NetWallah (Abbot) on Oct 09, 2011 at 06:49 UTC
    Most likely, what you are looking for is information on the Common Gateway Interface, commonly known as CGI.

    You have many things to learn, before you can successfully implement the functions you seem to require in your script (which will need to be re-designed, because , when you serve data over the web, there is no traditional STDIN. You will use web forms, instead.)

    A long, but enjoyable journey, well worth the effort.

    Update: To allow for the low probability that you meant client-side perl scripting, the reference is : Scripts - Animated Documents and Smart Forms.

                "XML is like violence: if it doesn't solve your problem, use more."

Re: How to embed Perl in HTML
by exilepanda (Monk) on Oct 09, 2011 at 06:55 UTC
    Don't understand your question...
    If you mean quote this source code in your HTML, you can write
    <!--index.html--> some header lines.... <code> you Perl source script... </ code> <-- no space between / and code

    Also, Perl cannot embed to run in HTML (not the PHP style). You will embed HTML in Perl, and Perl prints the output.

    If you try to perform the interaction like your console script does in the web way. You will need some study on Perl CGI(google CGI programming with perl). You will need to write <form> to create your input area, and use CGI; to retrieve the data, prints the output

        O! yes, I admit it there's some point I missed. My aspect is referring to the pure HTML nature only. But I forget if you are using Apache(or it's derive products), so you can do the hack easily.

        Good luck my friend! =)

Re: How to embed Perl in HTML
by duyet (Monk) on Oct 09, 2011 at 07:21 UTC

    I don't think there is such tutorial, if so i also like to know :)

    What you can do is:

    • create a CGI script like suggested in other post, which should create an HTML form. Let's call it myform.pl
    • put it on your webserver in cgi-bin dir. or any other dir. which is configured to execute scripts.
    • access your script with: http://www.yourserver.com/cgi-bin/myform.pl
    • Once you filled in the form and hit the submit button, you can get the user input and process it, send response back to user.
Re: How to embed Perl in HTML
by keszler (Priest) on Oct 09, 2011 at 09:15 UTC

    There is Embperl - or maybe I should say was, it doesn't appear to have been updated in over 5 years.

    I am not recommending it, nor have I ever used it. I just happened to recall that it existed.

Re: How to embed Perl in HTML
by perl.j (Pilgrim) on Oct 09, 2011 at 18:56 UTC

    If I understand correctly, you may want to try one of the following:

    Option 1

    1) Install HTML::Mason from CPAN. For detailed instructions on how to do this, see A Guide to Installing Modules.
    2) Learn how to use it! See CPAN.
    3)Get started!

    Option 2

    1) Install Embperl from CPAN.
    2) Learn How to use it! See CPAN.
    3) Get Started!

    Hope this helps!

    --perl.j
Re: How to embed Perl in HTML
by wazoox (Prior) on Oct 11, 2011 at 10:00 UTC
    Most probably you want to learn how to use cgi. The best to do is use the standard CGI library, see its documentation. In case you'd want to embed perl code into html (like php does), instead of Embperl I recommend you to try PLP which is much better.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (12)
As of 2014-10-21 15:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (105 votes), past polls