1

I have a raspberry pi running the latest Raspbian image with apache2 installed. I have a perl script in my /usr/lib/cgi-bin directory that I am using on a local, ad-hoc network without internet access.

Here is my perl file:

#!/usr/bin/perl
print "Content-type: text/html\n\n";
$config=`cat /home/pi/Desktop/AutoPi/AutoPi.config`;
print <<"EOF";
<HTML>
<HEAD>
<TITLE>Hello, world!</TITLE>
</HEAD>
<BODY>
<H1>Hello, world!</H1>
<br><br>
</BODY>
</HTML>
EOF

This works great, and I can use perl system commands, like:

$my_dir=`pwd`;

...and pass them to the webpage that the user sees. This is great. However, I need to be able to have the user click a button and execute code as well.

I am comfortable using perl, and want to keep everything in the perl file if possible. If I can just program a simple button that, upon being pressed, can execute a simple command server side, I can do the rest.

1
  • Can you create a form that POSTs to your script? Commented Dec 17, 2015 at 4:45

1 Answer 1

2

Here's a script to get you started using CGI.pm to access parameters from the submitted form.

#!/usr/bin/perl

use strict;
use warnings;

use CGI qw(escapeHTML);
#use CGI::Carp qw(fatalsToBrowser);

my $q = CGI->new;

my $text = $q->param('text') || '';

print $q->header('text/html; charset=utf-8');

my $safe_text = escapeHTML($text);

print <<"EOF";
<html>
<head>
  <title>Hello, world!</title>
</head>
<body>

  <h1>Hello, world!</h1>
  <div>$safe_text</div>

  <form method="post">
    <textarea name="text" rows="5" cols="40">$safe_text</textarea><br>
    <input type="submit" value="Submit">
  </form>

</body>
</html>
EOF

CGI.pm is not really a recommended way to build a web application but it sounds like you're having fun so go for it. Try removing the '#' on the use CGI::Carp line. If the script still runs then leave it that way so you'll get some error messages in the browser. You'll also want to run this command to look for errors:

tail -f /var/log/apache2/error.log
Sign up to request clarification or add additional context in comments.

1 Comment

That is fantastic, thanks a lot. This is exactly what I needed!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.