barrd has asked for the wisdom of the Perl Monks concerning the following question:
The basic synopsis of the problem was creating a conditional with exists using an if / else construct. This didn't work, so a RTFM session followed.
(Q1) WTF does autovivified mean?
From the dox: "...The element is not autovivified if it doesn't exist..."
There is no such word in the English language, there is however "auto" which in this context I take to mean automated and "vivified" which means "To endue with life; to make to be living; to quicken; to animate".
The point being I believe (maybe incorrectly) that this could be the crux of my problem trying to use else.
(Q2) Why no conditionals except if and unless?
A broken example (simplified from a friend):
... use CGI qw/:standard/; my $q = CGI->new(); my $level = $q->param('level'); my %run_level = ('foo' => \&foo, 'bar' => \&bar, 'default' => \&default ); if (exists $run_level{$level}) { &{ $run_level{$level} }; } else { default(); } sub foo { return "Sub foo called"; } sub bar { return "Sub bar called"; } sub default { return "Sub default called"; }
This, for those of you who know what I'm on about doesn't work so I came up with something along the lines of (which works):
default() unless exists $run_level{$level}; exit unless exists $run_level{$level}; # or add exit into default() &{ $run_level{$level} };
Which just 'seems' wrong, there must be a cleaner, nicer way to do it. Please someone tell me there is :) It's weird because I have been using exists for years but never tried it before with else...
Any explanations or pointers would be most appreciated. Thank You.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: exists (EXPR), autovivified and conditionals
by bart (Canon) on Sep 28, 2003 at 14:24 UTC | |
|
Re: exists (EXPR), autovivified and conditionals
by PodMaster (Abbot) on Sep 28, 2003 at 14:26 UTC | |
|
Re: exists (EXPR), autovivified and conditionals
by jasonk (Parson) on Sep 28, 2003 at 14:23 UTC | |
|
Re: exists (EXPR), autovivified and conditionals
by barrd (Canon) on Sep 28, 2003 at 14:53 UTC | |
by aquarium (Curate) on Sep 28, 2003 at 22:08 UTC |