looking for a functin to move %fdat to the symbol table..

2001-05-10 Thread jason n perkins

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?

my apologies if i've posted to the wrong list. i did cross post to the
embperl list, to cover all of the bases.

:: jason n perkins
:: email - [EMAIL PROTECTED]
:: web - www.somebodydial911.com






Re: looking for a functin to move %fdat to the symbol table..

2001-05-10 Thread Joshua Chamas

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?
 

%fdat = qw(var1 2 var2 4); 
map { eval qq(\$$_ = \$fdat{$_}) } keys %fdat; 
print join(qq(\n), $var1, $var2, undef)

You could roll your own extract() with line 2, but
if you want to put it in a module, you might have to
make it smarter about what package its extracting into
with a call to caller().

-- Josh
_
Joshua Chamas   Chamas Enterprises Inc.
NodeWorks  free web link monitoring   Huntington Beach, CA  USA 
http://www.nodeworks.com1-714-625-4051



Re: looking for a functin to move %fdat to the symbol table..

2001-05-10 Thread Perrin Harkins

It's not hard to do, but it is potentially dangerous since you could
overwrite globals like $/ and change the behavior of your program.  In
general, it's best to avoid cluttering the symbol table.
- Perrin




Re: looking for a functin to move %fdat to the symbol table..

2001-05-10 Thread Ken Williams

[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