Click here to get back home

Error-flag references to non-existing hash elements?

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Error-flag references to non-existing hash elements? Jeff Boes 03-08-2005
Posted by Jeff Boes on March 8, 2005, 1:08 pm
Please log in for more thread options


I need a module that will support this behavior:

use strict;
my %hash = ('A' => 1, 'B' => 2);

eval { my $x = $hash; # reference to non-existing hash element! };

# At this point, $EVAL_ERROR should be set to some error message!

I think I can write this up using Tie::Hash, but I'm hoping it's already
done. CPAN was no help, mostly because I can't come up with a good
search string that would narrow down my requirements.


Posted by Gunnar Hjalmarsson on March 8, 2005, 10:50 pm
Please log in for more thread options


Jeff Boes wrote:
> I need a module that will support this behavior:
>
> use strict;
> my %hash = ('A' => 1, 'B' => 2);
>
> eval { my $x = $hash; # reference to non-existing hash element! };
>
> # At this point, $EVAL_ERROR should be set to some error message!

Leaving the syntax error aside, how would eval() catch an error?
Assigning the undefined value to a variable is not wrong.

And why would you need a module? Can't you just do something like:

defined ( my $x = $hash ) or warn 'Undefined %hash value';

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


Posted by Jim Gibson on March 8, 2005, 2:54 pm
Please log in for more thread options



> Jeff Boes wrote:
> > I need a module that will support this behavior:
> >
> > use strict;
> > my %hash = ('A' => 1, 'B' => 2);
> >
> > eval { my $x = $hash; # reference to non-existing hash element! };
> >
> > # At this point, $EVAL_ERROR should be set to some error message!
>
> Leaving the syntax error aside, how would eval() catch an error?
> Assigning the undefined value to a variable is not wrong.
>
> And why would you need a module? Can't you just do something like:
>
> defined ( my $x = $hash ) or warn 'Undefined %hash value';

Or, if the question is whether or not the hash element with key C had
ever been assigned a value (even the undef value):

exists ( $hash ) or warn 'Non-existent %hash value';

Note you can't assign to $x here because exists needs a hash or array
element.


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000
Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---


Posted by Slaven Rezic on March 8, 2005, 11:50 pm
Please log in for more thread options



> I need a module that will support this behavior:
>
> use strict;
> my %hash = ('A' => 1, 'B' => 2);
>
> eval { my $x = $hash; # reference to non-existing hash element! };
>
> # At this point, $EVAL_ERROR should be set to some error message!
>
> I think I can write this up using Tie::Hash, but I'm hoping it's
> already done. CPAN was no help, mostly because I can't come up with a
> good search string that would narrow down my requirements.

Try the lock_keys function from the standard Hash::Util module.

Regards,
Slaven

--
Slaven Rezic - slaven <at> rezic <dot> de

tknotes - A knotes clone, written in Perl/Tk.
http://ptktools.sourceforge.net/#tknotes


Posted by Jeff Boes on March 9, 2005, 8:49 am
Please log in for more thread options



>> eval { my $x = $hash; # reference to non-existing hash element! };
>>
>> # At this point, $EVAL_ERROR should be set to some error message!
>>
>
>Try the lock_keys function from the standard Hash::Util module.
>

Just what I was looking for! Thanks!

--
~~~~~~~~~~~~~~~~| Genius may have its limitations, but stupidity is not
Jeff Boes | thus handicapped.
jboes@qtm.net | --Elbert Hubbard (1856-1915), American author




Similar ThreadsPosted
Modules for hash functions? (ie, common algorithms for computing hash keys, not manipulating perl hashes) August 26, 2006, 7:08 pm
Two references !!!!!! November 23, 2004, 5:08 am
XML::LibXML and getting data from elements/nodes October 26, 2004, 7:50 pm
HTML::Form, Multiple select Elements with Same name July 30, 2004, 3:48 pm
Hash July 14, 2006, 5:27 pm
Is a hash the best method to do this? December 24, 2007, 1:23 pm
What can you use to tie a hash of hashes to either dbm or ascii? December 2, 2007, 12:26 pm
Options for passing Hash to a subroutine. March 30, 2005, 7:39 pm
Announce: Set::Array, Set::Hash, Set::String - new maintainer January 8, 2005, 10:04 am
Bug with hash time & multilines in Config::IniFiles v2.38 ? January 26, 2005, 9:53 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap