# This allows you to capture all inputs that a CGI script runs with, within a webserver, # and allows you to "replay" the captured info, so that you can run your CGI script # from the command-line. # # To use this, temporarily put this at the very top of your script: # BEGIN {$^C or do "/home/myself/capture_CGI_inputs.pm"} # and then run your script via the web. use strict; use warnings; use Cwd; use Data::Dumper; sub capture_CGI_inputs { my ($filename) = @_; local $Data::Dumper::Terse = 1; my %substitute = ( ENV_CONTENTS => Dumper(\%ENV), CURDIR => Cwd::getcwd(), STDIN => "__DATA__\n" . do {local $/=undef; }, ); my $out = <<'EOF'; # pipe the __DATA__ section to this process's STDIN if ($0 eq __FILE__) { print ; exit; } %ENV = %{ <> }; chdir("<>"); open STDIN, '-|', $^X, __FILE__; # pipe the __DATA__ section to this process's STDIN <> EOF $out =~ s/<<(\w+)>>/$substitute{$1}/eg; open CAPTURED, ">$filename" or die $!; print CAPTURED $out; print "Content-type: text/html\n\ncapture_CGI_inputs() has run, and has generated a file. Include this at the top of your CGI script, and it should run happily from the command line.
    BEGIN{do \"$filename\"}
\n"; exit; } capture_CGI_inputs("/tmp/cgi_inputs." . time() . ".pm"); 1;