You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Alec Smith <al...@shadowstar.net> on 2001/02/23 11:05:34 UTC

Just learning, and a little stuck...

This isn't specifically a mod_perl question, but something I'm having
trouble doing within mod_perl code. I'm far from a Perl expert, but I
try...

I've got a hash called %post which contains submitted form info and a
variable $db which is the result of a DBI->connect call. I need to take
these 2 values and pass them into a subroutine. I've tried something like

&join($db, %post);

sub join
{
   my ($db, %post) = shift (@_);
   ...

   foreach $key (keys(%post))
   {
 	...
   }
}

and

&join ($db, \%post);

sub join
{
  my ($db, $post) = shift (@_);

  foreach $key (keys(%$post))
  {
     %$post{$key} = $db->quote("%$post{$key}");
  }
}

Using CGI-based Perl I suppose I could just use local() instead of my()
to avoid having to pass arguments, but didn't think this would be
advisable in mod_perl code.

How can I manage to do what I'm trying to do?


Alec



[OT] Re: Just learning, and a little stuck...

Posted by Steve Reppucci <sg...@logsoft.com>.
Yes, this doesn't belong on this list.

But: Couple of immediate problems:

- If you pass a hash in an argument list, it gets inserted into the list
as just a sequence of key,value pairs -- there's no way in the subroutine
to determine that a hash was passed, as opposed to a simple list, or an
array.  You *can* pass it the way you're doing in your first example
(since the final thing you're taking off the argument list in the
subroutine is the hash), but I think most perl folks would agree that the
second example (passing a hash reference) is better form.
Obviously, this depends upon the semantics of the function you're
writing.

- 'shift' shifts one item off a list.  You seem to be inferring that it 
will shift off as many as needed -- you can just use a list assignment,
if that's what you want to do.

I think you want one of these options:

  &join( $db, \%post);

  sub join {
    my ($db, $post_ref) = @_;
    foreach $key (keys %$post_ref) {

or:

  &join( $db, %post);

  sub join {
    my ($db, %post) = @_;
    foreach $key (keys %post) {

No, you definitely want to limit yourself from using 'local' until you
understand the semantic differences between it and 'my'. 'Effective Perl
Programming', by Joseph Hall has a nice description of this.

HTH,
<Steve>

On Fri, 23 Feb 2001, Alec Smith wrote:

> This isn't specifically a mod_perl question, but something I'm having
> trouble doing within mod_perl code. I'm far from a Perl expert, but I
> try...
> 
> I've got a hash called %post which contains submitted form info and a
> variable $db which is the result of a DBI->connect call. I need to take
> these 2 values and pass them into a subroutine. I've tried something like
> 
> &join($db, %post);
> 
> sub join
> {
>    my ($db, %post) = shift (@_);
>    ...
> 
>    foreach $key (keys(%post))
>    {
>  	...
>    }
> }
> 
> and
> 
> &join ($db, \%post);
> 
> sub join
> {
>   my ($db, $post) = shift (@_);
> 
>   foreach $key (keys(%$post))
>   {
>      %$post{$key} = $db->quote("%$post{$key}");
>   }
> }
> 
> Using CGI-based Perl I suppose I could just use local() instead of my()
> to avoid having to pass arguments, but didn't think this would be
> advisable in mod_perl code.
> 
> How can I manage to do what I'm trying to do?
> 
> 
> Alec
> 
> 

=-=-=-=-=-=-=-=-=-=-  My God!  What have I done?  -=-=-=-=-=-=-=-=-=-=
Steve Reppucci                                       sgr@logsoft.com |
Logical Choice Software                          http://logsoft.com/ |