Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

For something this simple, I would not use subroutines. It only makes things harder to read and harder to maintain if you use subs for code that gets executed only once. A rule of thumb: subroutines should return something and the return value must be useful.

You let the web user define the path to the sendmail program. That is EXTREMELY INSECURE. Imagine what would happen if someone has rm -rf /; as $formdata{sendmail}.

Here's a complete (UNTESTED) rewrite of your script, with some suggestions and remarks:

#!/usr/bin/perl -w

# This script has never been tested

use CGI;
use strict;

# Suggested improvement: use a hash for these

# URL to redirect to after form submission
my $redirect = '';

# Path to the sendmail executable
my $sendmail = '/usr/sbin/sendmail';

# Default subject
my $subject = 'Form Submission Results';

# List of recipients
my @recipients = ('');

# Required fields
my @required = ();

my $cgi = CGI->new();

my %formdata = $cgi->Vars;

sub set {
    my ($thing) = @_;
    return defined $thing and length $thing ? 1 : 0;

@required   = split /,/, $formdata{required}  if set $formdata{required};
$redirect   =            $formdata{redirect}  if set $formdata{redirect};
$subject    =            $formdata{subject}   if set $formdata{subject};
@recipients = split /,/, $formdata{recipient} if set $formdata{recipient};

    my @errors;
    push @errors, 'Redirection URL seems to be invalid'
        unless $redirect =~ m!^http://!;
    for (@recipients) {
        push @errors, "E-Mailaddress $_ seems to be invalid"
            unless /^[\w.-]+\@([\w.-]+\z/;
    for (@required) {
        push @errors, "Field '$_' is a required field" unless set $formdata{$_};
    last CHECK unless @errors;

    # Suggested improvement: a templating module
    print "Content-Type: text/html\n\n";
    print '<html><body><h1>Errors:</h1><ul>', map "<li>$_</li>", @errors;
    print '</ul></body></html>';


# E-Mail already has a Date: header. Why bother having your own?

delete @formdata{ qw/recipient subject required redirect sendmail/ };

# Suggested improvement: an E-mail module
for my $recipient (@recipients) {
    open my $mail, '|-', $sendmail, '-t', '-oi' or die "Couldn't start sendmail: $!";
    print $mail join("\n",
        "To: $recipient",
        "From: webmaster\",
        "Subject: $subject",
        "Remote host: $ENV{REMOTE_HOST} ($ENV{REMOTE_ADDR})",
        "Server: $ENV{SERVER_NAME}",
        map "$_: $formdata{$_}\n", keys %formdata

$cgi->redirect( -url => $redirect );

As you can see, there's still a lot of room for improvement.

Note for PM regulars: no, I still don't like, but I thought it'd be a lot better than the current solution.

- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.

In reply to Re: Some suggestions on coding style - a chance to critique by Juerd
in thread Some suggestions on coding style - a chance to critique by emilford

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (7)
    As of 2017-03-25 21:10 GMT
    Find Nodes?
      Voting Booth?
      Should Pluto Get Its Planethood Back?

      Results (313 votes). Check out past polls.