[Cedarvalley] upload problem

Stephen D. Wells wells at cedarnet.org
Wed Mar 31 12:24:53 CST 2004


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




More information about the Cedarvalley mailing list