[EMAIL PROTECTED] (jason n perkins) wrote:
>php has a function called extract which takes an array (no distinction
>between arrays and hashes in php. in this case in perl, it'd actually
>be a hash). extract takes the key values of the array (and optionally
>prepends a user specified prefix to each of the values to avoid
>clobbering other values in the symbol table), sets each of them up as a
>variable with that name and sets the variable's value equal to the
>value of that associated key in the hash. the beauty of extract comes
>into play when you want to use each of the key/value pairs in the
>array. instead of setting them up one at a time, pass the array to
>extract and voila, you have all of the key/value elements available as
>variables. if my description is too unclear, you can check the extract
>functions info at http://www.php.net/manual/en/function.extract.php.
>
>does embedded perl sport a similar function?

You do know about the built-in Perl function 'each', right?

As others have pointed out, what you're asking for can be done, but it
shouldn't be done.  You can almost always write much much cleaner code
by dealing with the hashes as hashes and not converting all its parts to
individual scalar variables.  I've seen a lot of [bad] code like this:

  $name = $data{name};
  $address = $data{address};
  $phone = $data{phone};
  
  $dbh->do("UPDATE users SET address='$address', phone='$phone'
            WHERE name='$name'");

It's much better (shorter, faster, safer) to let the hash do the work:

  $dbh->do("UPDATE users SET address=?, phone=? WHERE name=?", undef,
           @data{'address','phone','name'});

In addition, in a mod_perl environment you don't want any of your
data-carrying variables to be in the runtime symbol table.  Lexical
variables ('my') are much safer and guard against variables leaking out
of the scope you thought they were supposed to be in.


  -------------------                            -------------------
  Ken Williams                             Last Bastion of Euclidity
  [EMAIL PROTECTED]                            The Math Forum

Reply via email to