perl-modperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "cfaust-dougot" <cfa...@doyougot.com>
Subject RE: Is this an acceptable way to multipurpose a sub?
Date Mon, 26 Apr 2010 20:46:53 GMT
I'm sure someone where will pipe in a better way, but you could pass via a hash, something
like....
 
($result_code, $error) = submit_changes({ 'apache_request' = >$r, 'user_name' => \@user_names,
'latest_news' => $latest_news_file, 'archived_news' =>$archived_news_file, 'cgi_obj'
=>$q);

sub submit_changes {
  my $hash_ref = @_;
 # $hash_ref->{'apache_request'} is $r
# $hash_ref->{'user_name' } is the user arrayref 
# $hash_ref->{'archived_news' } may or may not be defined if it was passed in.. etc.

}

-Chris


________________________________

From: Chris Bennett [mailto:chris@bennettconstruction.biz]
Sent: Mon 4/26/2010 4:12 PM
To: mod_perl list
Subject: Is this an acceptable way to multipurpose a sub?



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



Mime
View raw message