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

Please help. I have to write a very simple CGI script to write data submitted in a form to a file. There is a problem with the CGI module on the hosts server and they have not been forthcoming with help after several emails. Can anyone point me towards a script that splits up the contents of a form without using CGI.pm

Replies are listed 'Best First'.
(Ovid) Re: perl script without cgi
by Ovid (Cardinal) on Nov 06, 2001 at 23:53 UTC

    Further, your question about how to split "the contents of a form" is very vague. I really have no idea what you're wanting to do and thus cannot offer advice. If you tell us what the problem is with CGI.pm and post a code snippet, that would go a long way to allowing us to better answer your question.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: perl script without cgi
by Anonymous Monk on Nov 07, 2001 at 01:15 UTC
    I can`t use the CGI.pm module because the server won`t recognise the module. I can`t find out why because the hosting company won`t respond to my emails. It doesn`t matter how the form input is parsed as long as I can get it.

      If the server "won't recognise" the module then you have some more serious problems. Either you're using a very old version of Perl or the Perl installation is seriously broken.

      Either way it sounds like your server admins are people who don't know what they are doing and you should look for replacements.

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you don't talk about Perl club."

Re: perl script without cgi
by Anonymous Monk on Nov 07, 2001 at 05:17 UTC
    thank you so much, that was exactly what I needed to know. I used to use perl quite a bit, but havn`t programmed for over a year, and couldn`t face having to check from ref. manual to ref. manual - re-learning how to code in order to produce a snippet. Gem! pol
Re: perl script without cgi
by chlige (Initiate) on Nov 07, 2001 at 03:23 UTC
    Here is a function that reads POST CGI data and puts it into a hash, where the CGI name is the key and the value is the value. The function returns a reference to that hash. I used to have one that would do both GET and POST methods, but I cannot seem to find it. However, this will work just as good.
    sub ReadCGI {
         my($cgi, @junk, $val, $field, %in, $i);
         read(STDIN, $cgi, $ENV{'CONTENT_LENGTH'});
         @junk = split(/&/,$cgi);
         foreach $i (0 .. $#junk) {
              $junk[$i] =~ s/\+/ /g;
              ($field, $val) = split (/=/,$junk[$i]);
              $field=~ s/%(..)/pack("c",hex($1))/ge;
              $val=~ s/%(..)/pack("c",hex($1))/ge;
              $in{$field} .= $val;
         }
         return(\%in);
    }
    

    The code that can be written
    is not the eternal Code.
    The typeglob that can be named
    is not the eternal Typeglob.
    - The Tao of Perl

      This is broken.

      • This doesn't allow for multiple values in a query string (color=red&color=blue is perfectly legal).
      • What happens if your $ENV{CONTENT_LENGTH} doesn't match the length of data read from STDIN: you get corrupted data, but you'll never know it.
      • Newer clients are supposed to separate name/value pairs with semi-colons instead of ampersands. This will break your code.

      As for the first bullet point, you might think that your code allows for multiple values, but it doesn't. If you have multiple values for one param name, you simply concatenate them. Consider the following:

      color=red&color=blue

      In your code, that results in:

      $in{ 'color' } == 'redblue'

      That's probably going to cause someone problems.

      Hope you don't take this personally, and welcome to the Monastery! :)

      Cheers,
      Ovid

      Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

        And that is why all my CGI scripts now start as such:
        #!/usr/bin/perl
        
        use CGI;
        
        That subroutine was something I worked with about 5 years ago, but then I moved to CGI.pm when I had trouble with all the points you brought up. That and cutting and pasting was a pain in the rumpus.

        The code that can be written
        is not the eternal Code.
        The typeglob that can be named
        is not the eternal Typeglob.
        - The Tao of Perl