I think Ithe most elegant solution is the eval so far. (Thanks for the other suggestions!)
But I think I will combine the results of the suggestions, and will come up with a new Class, which will be returned if an undefined or nonexistent hash key has been found.
So I will do module like this:
package TableMap::Undef;
use strict;
use overload
"0+" => sub { return undef },
"bool" = sub { return undef };
sub new{
my $a={};
my $x=shift;
bltess ($a,ref($x)||$x);
tie(%$a,"TableMap::Undef::TIE");
return $a;
};
sub AUTOLOAD { return new TableMap::Undef; };
sub DESTROY {};
package TableMap::Undef::TIE;
use strict;
sub TIEHASH {
my $a={};
my $x=shift;
bless ($a,ref($x)||$x);
return $x;
};
sub FETCH { return new TableMap::Undef; };
Every time I need to return an undef I will return "new TableMap::Undef".
This object is treated as undef as bool and numeric value, so this will cause right results when we test the value.
This problem wuold be solved with a new perl pseudo-variable, we can call it $LAST for example, this is the value of the last similar level tag in the current expression.
For example, I want to do things like this:
my $x=$s->{db} && $LAST->{users} && $LAST->{$user_id_1} && $LAST->limi
+ts && $LAST->{$user_id_2};
What do you think about it? Will it be a good feature request in perl6?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.