Is this an acceptable way to multipurpose a sub?
am 26.04.2010 22:12:17 von Chris Bennett
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, "
Username or Password unsuccessful
");
}
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, "Changes Successful
");
}
--
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?
am 26.04.2010 22:46:53 von Chris Faust
This is a multi-part message in MIME format.
------_=_NextPart_001_01CAE581.9A21AD17
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
I'm sure someone where will pipe in a better way, but you could pass via =
a hash, something like....
=20
($result_code, $error) =3D submit_changes({ 'apache_request' =3D >$r, =
'user_name' =3D> \@user_names,
'latest_news' =3D> $latest_news_file, 'archived_news' =
=3D>$archived_news_file, 'cgi_obj' =3D>$q);
sub submit_changes {
my $hash_ref =3D @_;
# $hash_ref->{'apache_request'} is $r
# $hash_ref->{'user_name' } is the user arrayref=20
# $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) =3D submit_changes($r, \@user_names,
$latest_news_file, $archived_news_file, $q);
($result_code, $error) =3D submit_changes($r, \@user_names,
"$site_directory/$article_directory/$article_file", undef, $q);
############################################################ ###########
## sub submit_changes
sub submit_changes {
my $r =3D shift;
my $usernames_aref =3D shift;
my $latest_news_file =3D shift;
my $archived_news_file =3D shift;
my $q =3D shift;
my $body =3D shift || '';
=20
my $sent_username =3D $q->param("username") || '';
my $sent_password =3D $q->param("password") || '';
my $can_pass =3D 0;
foreach my $user_w_pass (@$usernames_aref) {
if ($user_w_pass eq "$sent_username:$sent_password") {
$can_pass =3D 1;
#last;
}
}
=20
unless ($can_pass) {
return(0, "
Username or Password =
unsuccessful
");
}
my $textfile1 =3D $q->param("filetext1") || '';
my $textfile2 =3D $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, "Changes Successful
");
}
--
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
------_=_NextPart_001_01CAE581.9A21AD17
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Is this an acceptable way to multipurpose a =
sub?=0A=
=0A=
=0A=
=0A=
=0A=
I'm sure =
someone where will pipe in a better way, but you could pass via a =
hash, something like....
=0A=
=0A=
($result_code, $error) =3D =
submit_changes({ 'apache_request' =3D >$r, 'user_name' =3D> =
\@user_names,
'latest_news' =3D> $latest_news_file, =
'archived_news' =3D>$archived_news_file, 'cgi_obj' =
=3D>$q);
=0A=
sub submit_changes {
=0A=
my $hash_ref =3D @_;
=0A=
# $hash_ref->{'apache_request'} =
is $r
=0A=
# $hash_ref->{'user_name' } is the user =
arrayref
=0A=
# $hash_ref->{'archived_news' } may or =
may not be defined if it was passed in.. etc.
=0A=
}
=0A=
-Chris
=0A=
=0A=
=0A=
=0A=
=0A=
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?
=0A=
=0A=
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) =3D submit_changes($r, =
\@user_names,
$latest_news_file, $archived_news_file, =
$q);
($result_code, $error) =3D submit_changes($r, =
\@user_names,
"$site_directory/$article_directory/$article_file", =
undef, =
$q);
#########################################################=
##############
##  =
; sub submit_changes
sub submit_changes =
{
my $r =3D =
shift;
my $usernames_aref =
=3D shift;
my =
$latest_news_file =3D =
shift;
my =
$archived_news_file =3D =
shift;
my $q =3D =
shift;
my $body =3D shift =
|| =
'';
&n=
bsp; my $sent_username =3D $q->param("username") || =
'';
my $sent_password =3D =
$q->param("password") || =
'';
my $can_pass =3D =
0;
foreach my $user_w_pass =
(@$usernames_aref) {
=
if ($user_w_pass eq =
"$sent_username:$sent_password") =
{
=
=
$can_pass =3D =
1;
=
=
=
#last;
=
=
=
}
=
=
}
=
&nb=
sp; unless ($can_pass) =
{
=
return(0, =
"<p><b>Username or Password =
unsuccessful</b></p>");
&nbs=
p; }
my =
$textfile1 =3D $q->param("filetext1") || =
'';
my $textfile2 =3D =
$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
------_=_NextPart_001_01CAE581.9A21AD17--
RE: Is this an acceptable way to multipurpose a sub?
am 26.04.2010 23:11:15 von israel.leiva
> 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
}
+++++++++++++++++++++++++++++++++++++++++++++++++++