I am combining into my module, subs for two scripts doing very similar, yet a little different functions.

Many subs can be used by both unchanged.
Some are almost right except for the arguments

One script works on two output files at once, the other, just one.
So I did this and it seems to work perfectly.

Is this an OK way to accomplish this?

($result_code, $error) = submit_changes($r, \...@user_names, $latest_news_file, $archived_news_file, $q);


($result_code, $error) = submit_changes($r, \...@user_names, "$site_directory/$article_directory/$article_file", undef, $q);


#######################################################################
##              sub submit_changes

sub submit_changes {
        my $r = shift;
        my $usernames_aref = shift;
        my $latest_news_file = shift;
        my $archived_news_file = shift;
        my $q = shift;
        my $body = shift || '';
        
        my $sent_username = $q->param("username") || '';
        my $sent_password = $q->param("password") || '';
        my $can_pass = 0;
        foreach my $user_w_pass (@$usernames_aref) {
                if ($user_w_pass eq "$sent_username:$sent_password") {
                        $can_pass = 1;
                        #last;
                        }
                }
                
        unless ($can_pass) {
                return(0, "<p><b>Username or Password unsuccessful</b></p>");
        }

        my $textfile1 = $q->param("filetext1") || '';
        my $textfile2 = $q->param("filetext2") || '';

open (OUTFILE, ">", "$latest_news_file") || die("unable to open $latest_news_file $!");
        print OUTFILE $textfile1;
        print OUTFILE $body;
        close (OUTFILE);

        if (defined $archived_news_file) {
open (OUTFILE2, ">", "$archived_news_file") || die("unable to open $archived_news_file $!");
                print OUTFILE2 $textfile2;
                close (OUTFILE2);
        }
        return(1, "<p>Changes Successful</p>");
}



--
A human being should be able to change a diaper, plan an invasion,
butcher a hog, conn a ship, design a building, write a sonnet, balance
accounts, build a wall, set a bone, comfort the dying, take orders,
give orders, cooperate, act alone, solve equations, analyze a new
problem, pitch manure, program a computer, cook a tasty meal, fight
efficiently, die gallantly. Specialization is for insects.
   -- Robert Heinlein

Reply via email to