Monolith-0 has asked for the wisdom of the Perl Monks concerning the following question:

I'm having a hard time phrasing this question in my mind, so I hope it'll make some sence.

Question, part 1:
How can I run a Perl script from an HTML document? The .pl file just does some stuff and then prints out some text ment to be seen in the HTML.

Question, part 2:
Instead of using HTML, if I use Perl to create the page, how can I run another Perl file, or a subroutine from another file in that first Perl script. I'm sure this is very common, and very important to know, but I don't know the Perl equivilent of it.

Someone please help this novice :)

If I made no sence at all, I'll try to explain it again.

Replies are listed 'Best First'.
Re: Sub Perl Scrips
by dws (Chancellor) on Jan 28, 2001 at 01:23 UTC
    Your questions makes enough sense.

    To answer question one, consult your web server documentation for "Server-Side Includes". If you're using Apache, look here.

    To run another perl script (or another program) from within a perl script, capturing the output, read perlipc.

    To use a subroutine that exists in another script, you're going to have to read up on how to construct Perl modules (pacakges), or how the require statement works. It's not trivial to reach into another script and invoke another subroutine, but that's a part of the path to Perl knowledge yet ahead of you. Why spoil the fun here?

Re: Sub Perl Scrips
by AgentM (Curate) on Jan 28, 2001 at 01:23 UTC
    It seems to me that for 1), you'll want server side includes (SSI). Search for SSI using Super Search for more info. This topic is well-explained in other places as well- notably at apache.org where numerous tutorials are housed.

    For number 2), you'll need a require statement in your script. Be sure to append any required file with a "1;". If it's a nice module, you can use it.

    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
(redmist) Re: Sub Perl Scrips
by redmist (Deacon) on Jan 28, 2001 at 02:37 UTC

    Your first question seems like you are talking about CGI (Common Gateway Interface), which is a way of creating dynamic HTML content.

    The actual mechanics to call a CGI script from a form is:

    <form action="/home/httpd/cgi-bin/script.cgi">

    Make sure to look at the <form> and <input> HTML tags. Make sure to scope out Ovid's CGI course at: http://www.easystreet.com/~ovid/cgi_course.

    Good Luck!



    redmist
    Silicon Cowboy
    redmist::webnode
Re: Sub Perl Scrips
by wardk (Deacon) on Jan 28, 2001 at 03:41 UTC

    packing up for a ski trip....not much time...last perlmonk session for over a week...have to write something.....(perl withdrawals impending), so I haven't tested...but it should be close for a beginning of a "perl script that prints a page that can be used to fire off the perl script to do something"

    hell, if it was perfect you would have the fun of debugging it, eh? :-)

    good luck

    #!/usr/bin/perl $|++; use strict; use CGI; my $q = new CGI(); my $thingtodo = $q->param("thingtodo"); print $q->header(); print $q->starthtml(); print qq(<form method=post action="/dir/cgi-bin/this.pl"); if ( $thingtodo eq "somethingnew" ) { somethingnew(); else defaultpage(); } print qq(</form>); $q->end_html(); exit; sub defaultpage { print $q->p("default page"); print <<EOHTML; <input type=hidden name=thingtodo value=somethingnew> <input type=submit value="press for something new"> EOHTML ; } sub somethingnew { print $q->("something new"); print <<EOHTML; <input type=submit value="press to go home"> EOHTML ; }

    oh yeah, mixed some styles for outputting html...timtowtdi

Re: Sub Perl Scrips
by sierrathedog04 (Hermit) on Jan 28, 2001 at 02:34 UTC
    If you merely wish to run another perl script then the easiest way to do that is to use the backtick operator. Assuming that perl is accessible from your command line and that your script is named myscript.pl then just add the line:

    `perl myscript.pl`;

    You can even capture the output of the other perl script if any as in:

    my $output = `perl myscript.pl`;

    As Larry Wall always says, "There's more than one way to do it."

      Avoid `back-ticks` unless you need to get at the output (as you do in your second example). Using back-ticks in a void context only makes perl do extra work unnecessarily. In this case, I'd use system instead.
        Why can't backticks use defined(wantarray) to find out if they where called in a void context, and if so, do not return or capture the output? (Redirecting to /dev/null comes to mind)
Re: Sub Perl Scrips
by Monolith-0 (Beadle) on Jan 28, 2001 at 05:08 UTC
    Thank you.
    Yes, SSI was what I was asking about in the first part, and modules are just what I needed.
    Thanks for your help everybody.