Mod Perl Helper Functions

2004-10-05 Thread James Taylor
I posted this to the mod_perl list 2 days ago, but it seems a bit 
inappropriate there as the discussion is much more
advanced than my question... That and I haven't gotten a response, so 
sorry for the crosspost :)

I'm running apache 1.3.x with the associated version of mod_perl, but am 
having trouble creating regular old include
files to be used with helper functions.  Works just great with regular 
old CGI, mod_perl doesn't like it.  Say for example I have two files, 
main.pl and helper.pl.

# helper.pl
sub dowhatever() {   # this is completely pointless, i'm just using it 
as an example
  my [EMAIL PROTECTED];  my $p;
  foreach my $k(sort keys %params) { $k =~ s/'$//g;  $p.=$k; }
  return $p;
}
1;

# main.pl
#!/usr/bin/perl
use strict;
use Apache::Constants qw(OK DECLINED SERVER_ERROR FORBIDDEN);
use Apache::Cookie ();
require('helper.pl');
#..some stuff
my $q=&dowhatever(%somehash);
That doesn't work in the least, I keep getting errors in my apache logs 
saying sub dowhatever is an undefined function, etc.  Is it possible to
even do anything like this in mod_perl, or do I need to write an Apache 
module / Perl Module just to have helper functions in there?
Also, I know it's poor style to put multiple statements on 1 line, I 
just wanted to make this message shorter, so no style tips please. 
Thanks for any help!

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



RE: Mod Perl Helper Functions

2004-10-05 Thread Bob Showalter
James Taylor wrote:
> I posted this to the mod_perl list 2 days ago, but it seems a bit
> inappropriate there as the discussion is much more
> advanced than my question... That and I haven't gotten a response, so
> sorry for the crosspost :)
> 
> I'm running apache 1.3.x with the associated version of mod_perl, but
> am having trouble creating regular old include
> files to be used with helper functions.  Works just great with regular
> old CGI, mod_perl doesn't like it.  Say for example I have two files,
> main.pl and helper.pl.
> 
> # helper.pl
> sub dowhatever() {   # this is completely pointless, i'm just using it
> as an example
>my [EMAIL PROTECTED];  my $p;
>foreach my $k(sort keys %params) { $k =~ s/'$//g;  $p.=$k; }
>return $p;
> }
> 1;
> 
> # main.pl
> #!/usr/bin/perl
> use strict;
> use Apache::Constants qw(OK DECLINED SERVER_ERROR FORBIDDEN);
> use Apache::Cookie ();
> require('helper.pl');
> 
> #..some stuff
> my $q=&dowhatever(%somehash);
> 
> 
> That doesn't work in the least, I keep getting errors in my apache
> logs saying sub dowhatever is an undefined function, etc.  Is it
> possible to even do anything like this in mod_perl, or do I need to
> write an Apache module / Perl Module just to have helper functions in
> there? 

FWIW, That example works OK for me under Apache 1.3.31, mod_perl 1.29 when I
run under Apache::Registry. I had to add a declaration for %somehash, but
otherwise it runs without error. Is main.pl running under Apache::Registry,
or is it a PerlHandler or some other type of handler?

It always makes me nervous to see assumptions about the current working
directory in any web server stuff. You should code the full path to
helper.pl, or better, create a proper module and pull it in with use.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Mod Perl Helper Functions

2004-10-05 Thread James Taylor
Bob Showalter wrote:
FWIW, That example works OK for me under Apache 1.3.31, mod_perl 1.29 when I
run under Apache::Registry. I had to add a declaration for %somehash, but
otherwise it runs without error. Is main.pl running under Apache::Registry,
or is it a PerlHandler or some other type of handler?
It always makes me nervous to see assumptions about the current working
directory in any web server stuff. You should code the full path to
helper.pl, or better, create a proper module and pull it in with use.
 

Here's a better example of something that actually might serve a bit 
more purpose along with the error I'm getting for THIS
particular example.

And yes, this is running under Apache::Registry:
test.pl:
#!/usr/bin/perl
use strict;
use Apache::Constants qw(OK DECLINED SERVER_ERROR FORBIDDEN);
use Apache::Cookie ();
require('lib/helper.pl');
print "Content-type: text/html\n\n";
my $rand=&genrand(20);
print "$rand";
helper.pl:
sub genrand {
  my [EMAIL PROTECTED]; my $r;
  my @chars=('a'..'z','A'..'Z','0'..'9','_');
  $r.=$chars[rand @chars] for(1..$l);
  return $r;
}
1;
When running test.pl, I get the following in my logs:
Use of uninitialized value in foreach loop entry at lib/helper.pl line 4.
Use of uninitialized value in foreach loop entry at lib/helper.pl line 4.
Use of uninitialized value in concatenation (.) or string at 
/export/home/newsite/perl/test.pl line 10.

So, what I can gather is that the value '20' isn't being received by the 
genrand subroutine, and it's getting something else instead.  What 
exactly, I'm not sure
but I know it has to do SOMETHING with Apache::Registry.  To test things 
out, I went ahead and changed the genrand sub to spit out what it was 
receiving:

sub genrand {
   my @[EMAIL PROTECTED]; my $r;
   $r.="$_\n" for @blah;
   return $r;
}
What did it return? "20"
Now how the hell that happened, I don't know, but it STILL isn't getting 
that value passed.  Any idea what apache's sending here and why it 
doesn't want to
grab the variable?

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: Mod Perl Helper Functions

2004-10-06 Thread Paul Johnson
On Tue, Oct 05, 2004 at 02:25:08PM -0700, James Taylor wrote:

> Here's a better example of something that actually might serve a bit 
> more purpose along with the error I'm getting for THIS
> particular example.
> 
> sub genrand {
>   my [EMAIL PROTECTED]; my $r;

I'm not sure that this will solve everything, but you likely want

my ($l) = @_;

>   my @chars=('a'..'z','A'..'Z','0'..'9','_');
>   $r.=$chars[rand @chars] for(1..$l);
>   return $r;
> }

> So, what I can gather is that the value '20' isn't being received by the 
> genrand subroutine, and it's getting something else instead.  What 
> exactly, I'm not sure

I would have expected $l to be set to 1, the number of elements in @_.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]