[Cedarvalley] upload problem
Aaron Thompson
thompson at cns.uni.edu
Wed Mar 31 13:21:30 CST 2004
Nice, I ran into to places this would have been useful with lists
in the last week - instead I ended up using a hash:
my %groups;
for my $group (split(" ",`id -nG $self->{_user_name}`)){$groups{$group}++;}
...
if($groups{'valid_group'}){
#I'm a group memeber
}#fi
Your line could have been used instead:
my @groups = split(" ",`id -nG $self->{_user_name}`);
...
if({ map { $_ => 1 } @groups }->{'valid_group'}){
#I'm a group member
}
@
On Wed, Mar 31, 2004 at 12:24:53PM -0600, Stephen D. Wells wrote:
> You probably have already seen this but I just stumbled across it, liked
> it and thought I'd pass it along...
>
> * A one-liner for checking to see if a value exists in a list:
> if ({ map { $_ => 1 } qw(apple orange lemon) }->{apple}) {
> # do stuff
> }
>
> Here's what's happening:
>
> We are creating an anonymous hash of the values in the list:
> { apple => 1, orange => 1, lemon => 1 }
>
> Then checking to see if that hash exists
> { apple => 1, orange => 1, lemon => 1 }->{apple} # should return 1
>
> This replaces code that you see all the time like this:
> my $found = 0;
> {
> for (qw(apple orange lemon)) {
> next unless $_ eq "apple";
> $found++;
> last;
> }
> }
> if ($found) {
> # do stuff
> }
>
> * more complex example
>
> I've been using the Class::DBI module for some extranet reporting
> software I created and have a table structure like so:
>
> user user_access access
> ---- ----------- ------
> id id id
> name user_id name
> pass access_id
>
> see Class::DBI for how to build your Classes
>
> in one line I can check to see if this user is in the 'admin' group and
> allow my administration functions to override the user functions using
> the following:
>
> if ({ map { $_->access_id->name => 1 }
> (EX::DBI::User->accesses)}->{'admin'}) {
> require "EX/Admin.pm";
> push (@ISA, 'EX::Admin');
> }
>
> I was kinda' impressed with the fact there it's such a compact piece of
> code.
>
> Thoughts?
> STEVE
>
> _______________________________________________
> Cedarvalley mailing list
> Cedarvalley at mail.pm.org
> http://mail.pm.org/mailman/listinfo/cedarvalley
--
Aaron Thompson Unix Systems Administrator, College of Natural Science
http://www.cns.uni.edu/~thompson/ University of Northern Iowa
"If there's anything more important than my ego around, I want it caught
and shot now." -Douglas Adams
More information about the Cedarvalley
mailing list