Dianne Van Dulken <[EMAIL PROTECTED]> said something to this effect 
on 08/14/2001:
> I am receiving a lot of values from a form.  I want to loop
> through them and set new variables, with the same name, to the
> same value.  EG:  I want the param value phone to be set to
> $phone.

So, you want to create a variable in the current namespace for
each form parameter that comes in?  A request like
script.cgi?foo=bar will create $foo?

There are a couple of options.  CGI.pm, which you appear to be
using, based on the code snippet below, has a method called
import_names.  From perldoc CGI:

       IMPORTING ALL PARAMETERS INTO A NAMESPACE:

          $query->import_names('R');

       This creates a series of variables in the 'R' namespace.
       For example, $R::foo, @R:foo.  For keyword lists, a
       variable @R::keywords will appear.  If no namespace is
       given, this method will assume 'Q'.  WARNING:  don't
       import anything into 'main'; this is a major security
       risk!!!!

What you want, basically, is to use symbolic references.  Without
this turning into a reference tutorial (perldoc perlref, BTW), a
soft reference uses the value of a scalar as the name of another
variable:

  my $one = 'foo';
  $$one = 42;

  print $foo;
  #produces 42

The problem is that this is one of the things use strict prevents
by default, since it's usually not what people want (you are
using strict, right?).  you can selectively turn off strict
reference checking by block with the 'no strict qw(refs)' pragme.

Here is a loop to do what (I think) you are asking for:

  use strict;

  {   # extra "naked" block to scope the no strict 'refs'
      # turning off strict refs leaves on the other strictures,
      # such as vars and subs
      no strict 'refs';
      for my $p ($q->param) {
          ${"$p"} = $q->param($p);
          # or just $$p = $q->param($p)
          # I prefer the former because it makes explicit what
          # you are trying to do, and also makes it clear that
          # you are assigning to a soft reference and not
          # dereferencing a regular reference
      }
  }

This is just for illustration; use CGI's import_names method,
because it does a lot of checking to ensure that the namespace
you choose is clean:

  my $q = CGI->new;
  $q->import_names("Q");

  print $Q::phone;

(Choose a more descriptive name than Q, though!)

Good luck.

(darren)

-- 
You are what you see.

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

Reply via email to