http://www.perlmonks.org?node_id=985956

hp51185 has asked for the wisdom of the Perl Monks concerning the following question:

Hello I am trying to run a perl script on tomcat server. I am putting the incoming http request in a variable data which collects the headers as well as the text file attachment data. The code is like this : .... while(<>){ $data = $data . $_ ; } ... This works fine on apache but on tomcat 6.0 this is getting hanged. What is the issue? Thanks in advance Harsh

Replies are listed 'Best First'.
Re: while loop with diamond operator
by tobyink (Canon) on Aug 07, 2012 at 12:34 UTC
    while(<>){ $data = $data . $_ ; }

    ... can be better written as:

    $data = do { local $/; <> };

    Not only is that shorter, but it should actually run faster. In your version, Perl has to split STDIN on each new line character, and then join them back together. Localizing $/ just slurps the whole thing in at once.

    How exactly are you getting Tomcat to launch your Perl script? Are you 100% sure that whatever mechanism it's using is actually passing data to STDIN?

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: while loop with diamond operator
by blue_cowdawg (Monsignor) on Aug 07, 2012 at 14:03 UTC
        This works fine on apache but on tomcat 6.0 this is getting hanged.

    Strongly suspect this is an issue with how your Tomcat instance is configured. If I remember correctly (and maybe this has been fixed in Tomcat 6.0) Tomcat does not natively understand CGI straight out of the box and has no concept of invoking a Perl interpreter right out of the box.

    There is a "how to" on this (external site) here and I'll put a sniglet of that howto here:

    CGI support is implemented using the servlet class org.apache.catalina.servlets.CGIServlet. Traditionally, this servlet is mapped to the URL pattern "/cgi-bin/*".
    
    By default CGI support is disabled in Tomcat.
    
    Further into the how-to they explain that CGI is executed outside of the JVM that governs Tomcat.

    Are you front ending Tomcat with Apache? If so, there is the possibility of getting Apache to handle the CGI portion of your app and let Tomcat do what it does best and serve as a web container for JSP and friends.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: while loop with diamond operator
by aitap (Curate) on Aug 07, 2012 at 12:34 UTC
Re: while loop with diamond operator
by SerZKO (Beadle) on Aug 07, 2012 at 18:37 UTC

    As blue_cowdawg said, Tomcat doesn't support CGI by default. You have to do 3 things:

    1. Rename file servlets-cgi.renametojar (under your tomcat servlet directory) to servlets-cgi.jar

    2. Modify your web.xml (usually under ../server/default/deploy/conf/) and uncomment whole part arround cgi servlet (don't forget mapping inside same file)

    3. Modify context.xml file (usually under ../web.deployer directory) and add privileged="true" in < Context... >line, so it looks something like this <Context cookies="true" crossContext="true" privileged="true">

    And that would make CGI work on your Tomcat 5 or 6 server