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

Hello All, Hope everyone is doing great. I had a project where that project takes a URL (perl script) as input, and it will keep post the parameters like project_id, record_id etc to that perl script or the URL. Does any one have the same code that will grab those http post parameters and log them to a log file. Thank you.
  • Comment on How to get post parameters and print/log them in Perl

Replies are listed 'Best First'.
Re: How to get post parameters and print/log them in Perl
by Corion (Patriarch) on Nov 11, 2016 at 07:09 UTC

    For the simplest solution, see CGI and its ->multi_param or ->param method.

Re: How to get post parameters and print/log them in Perl
by 1nickt (Canon) on Nov 11, 2016 at 00:54 UTC

    Sure, here you are:

    my %parameters = params;
    Hope this helps!

    The way forward always starts with a minimal test.
Re: How to get post parameters and print/log them in Perl
by Linicks (Scribe) on Nov 11, 2016 at 14:27 UTC

    I use this on a couple of my CGI scripts... I just use sed thus in the perl code:

    `sed -i '1i $var1 $var2 $var3' /path/to/log/log.txt`;

    If you use this, the file MUST not be empty to start with, so I just put at the top the date the log was created. It also adds the new entry at the top.

    Nick

    P.S. ummm, for some reason the code tags fail here...

      What if $var1 contains something like this: my $var1 = "\\'; cat /etc/passwd";? I'm unsure why you'd want to use sed rather than perl to write some data to a file.

        Well, it depends on the context - for *known* data input, I would rather use existing tools than re-invent the wheel.

        In the example you gave, surely some sort of user input sanitisation goes on first?

        Nick

      I just use sed ...

      Yeah, lots of people use perl as a replacement for bash/csh ... nothing wrong with that

      But even in 1994 even amateur bash programmers acknowledged the crazy insecurity of writing cgi programs in bash ...

      using sed from perl or bash or any programming language, to print out some CGI data into a file , its about as silly as programming can get

      but as actual advice to someone else, its mean and it stinks

      P.S. ummm, for some reason the code tags fail here...

      The code tags are working fine just like these code tags but single-line mostly don't end up in download code