List Info

Thread: Perl equivalent of PHP extract()?




Perl equivalent of PHP extract()?
user name
2006-12-18 00:56:26
Inside a subroutine, I want to use this hash:

%hash = ("apple" => "red",
"pear" => "green", "le!mon" => "yellow");

to set my($apple)="red",
my($pear)="green", my($lemon)="yellow"
[getting rid of the junk '' and '!' chars in the key]

This is approximately what PHP's extract() does. This almost
works:

for $i (keys %hash) {
  $safe = $i; # keep safe copy of $i
  $i=~s/[^a-z_]//isg; # get rid of nonalpha characters in
key
  unless ($i=~/^[a-z_]+$/) {next;} # if key is now empty,
ignore it
  eval("$$i = $hash{'$safe'}");
}

but the assignments are made in the global namespace, not
just in the
subroutine namespace. Replacing the eval with:

eval("my($$i) = $hash{'$safe'}");

doesn't work either, because the my() is now local to the
eval, and
doesn't set variables in the subroutine namespace.

I don't know the hash keys ahead of time, so I can't
hardcode:

my($apple,$pear,$lemon);

Of course, variables in the global namespace are available
in the
subroutine namespace, so the first version is usable, but
dangerous
(could clobber existing global variables). Thoughts?

-- 
We're just a Bunch Of Regular Guys, a collective group
that's trying
to understand and assimilate technology. We feel that
resistance to
new ideas and technology is unwise and ultimately futile.

  ----------



[Non-text portions of this message have been removed]



Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://g
roups.yahoo.com/group/perl-beginner/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http
://groups.yahoo.com/group/perl-beginner/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:perl-beginner-digest@yahoogroups.com 
    mailto:perl-beginner-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    perl-beginner-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 
Perl equivalent of PHP extract()?
user name
2006-12-18 02:33:58
>>>>> "Kelly" == Kelly Jones
<kelly.terry.jonesgmail.com> writes:

Kelly> Inside a subroutine, I want to use this hash:
Kelly> %hash = ("apple" => "red",
"pear" => "green", "le!mon" => "yellow");

Kelly> to set my($apple)="red",
my($pear)="green", my($lemon)="yellow"
Kelly> [getting rid of the junk '' and '!' chars in the key]

You really really *don't* want to do that.

If PHP has a function to do that easily, no wonder there's
so many security
holes reported with PHP scripts.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. -
+1 503 777 0095
<merlynstonehenge.com> <URL:http://www.ston
ehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy,
etc. etc.
See PerlTraining.Stonehenge.com for onsite and
open-enrollment Perl training!


Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://g
roups.yahoo.com/group/perl-beginner/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http
://groups.yahoo.com/group/perl-beginner/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:perl-beginner-digest@yahoogroups.com 
    mailto:perl-beginner-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    perl-beginner-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 
Perl equivalent of PHP extract()?
user name
2006-12-18 15:17:58
As Randal said, there's really _no_ reason to do this kind
of stuff.
Coming to Perl from PHP, you're gonna have to break a lot of
terrible
habits.  Perl makes working with hashes a delight, rather
than the
encumbering BS that is PHP array/hash functions.  Stick to
hashes for
this kind of thing.  That way you can inventory your
variables at any
point by calling keys();

If you're looking to sanitize the hash, then do that.

my %clean = ();

while (my ($k,$v) = each %tainted ) {
	$k =~ s/W//g;
	$clean{ lc $k } = $v;
}

Then you don't have to worry about how many variables you
just created,
you can find out easily:

my $cleanedCount = scalar keys %cleaned;

On Sun, Dec 17, 2006 at 05:56:26PM -0700, Kelly Jones wrote:
>    Inside a subroutine, I want to use this hash:
> 
>    %hash = ("apple" => "red",
"pear" => "green", "le!mon" => "yellow");
> 
>    to set my($apple)="red",
my($pear)="green", my($lemon)="yellow"
>    [getting rid of the junk '' and '!' chars in the key]
> 
>    This is approximately what PHP's extract() does.
This almost works:
> 
>    for $i (keys %hash) {
>    $safe = $i; # keep safe copy of $i
>    $i=~s/[^a-z_]//isg; # get rid of nonalpha characters
in key
>    unless ($i=~/^[a-z_]+$/) {next;} # if key is now
empty, ignore it
>    eval("$$i = $hash{'$safe'}");
>    }
> 
>    but the assignments are made in the global
namespace, not just in the
>    subroutine namespace. Replacing the eval with:
> 
>    eval("my($$i) = $hash{'$safe'}");
> 
>    doesn't work either, because the my() is now local
to the eval, and
>    doesn't set variables in the subroutine namespace.
> 
>    I don't know the hash keys ahead of time, so I can't
hardcode:
> 
>    my($apple,$pear,$lemon);
> 
>    Of course, variables in the global namespace are
available in the
>    subroutine namespace, so the first version is
usable, but dangerous
>    (could clobber existing global variables). Thoughts?
> 
>    --
>    We're just a Bunch Of Regular Guys, a collective
group that's trying
>    to understand and assimilate technology. We feel
that resistance to
>    new ideas and technology is unwise and ultimately
futile.
> 
>    ----------
> 
>    [Non-text portions of this message have been
removed]
> 
>       

-- 
Brad Lhotsky


Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://g
roups.yahoo.com/group/perl-beginner/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http
://groups.yahoo.com/group/perl-beginner/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:perl-beginner-digest@yahoogroups.com 
    mailto:perl-beginner-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    perl-beginner-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )