You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Chris Bennett <ch...@bennettconstruction.biz> on 2010/04/26 22:12:17 UTC
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
RE: Is this an acceptable way to multipurpose a sub?
Posted by is...@alumnos.inf.utfsm.cl.
> 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
Almost the same, but with some extra things:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#!/usr/bin/perl
use strict;
use warnings;
sub some {
my %params = @_;
print $params{p1}."\n";
foreach my $e ( @{$params{p1sub}} ) {
print "\t$e\n";
}
print "$params{p2}\n";
my $result = {};
$result->{error} = 'foo';
$result->{someother} = 'baar';
# etc
return $result;
}
my $res = {};
$res = some(
p1 => 'param1',
p1sub => ['param1.1', 'param1.2', 'param1.3'],
p2 => 'param2',
);
if ($res->{error}) {
# ... do something
}
+++++++++++++++++++++++++++++++++++++++++++++++++++
RE: Is this an acceptable way to multipurpose a sub?
Posted by cfaust-dougot <cf...@doyougot.com>.
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