tel2 has asked for the wisdom of the Perl Monks concerning the following question:
I've just written some code to do the following:
$item will contain a string in the format 'X:Y'.
First I test if 'X:Y' exists in the hash, and if so, put the hash's value into $level.
Failing that, I test if 'X:*' exists in the hash, and if so, put the hash's value into $level.
Failing that, I test if '*' exists in the hash, and if so, put the hash's value into $level.
Failing that, I set $level = 0.
Here's the code I wrote to achieve the above:
if ($level = $access{$item}) # Assignment & test for existance {} # Not very tidy looking, IMHO elsif ($item =~ /^(.+):.+/ and $level = $access{"$1:*"} ) # Assignmen +t & test for existance {} # Not very tidy looking, IMHO else { unless ($level = $access{'*'}) # Assignment & test for existance { $level = 0 } }
The above code seems to be doing the job, and I'm not totally unhappy with it, but I'm wondering if there's a tidier or more concise way to write it, e.g. avoiding the empty '{}', and anything else you can suggest.
Note, the above was written without strict or warnings, and I'll be leaving it that way, despite the fact that it's against your religion. 8)
Thanks for your time.
Tel2
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Tidy up conditions
by martin (Friar) on Mar 18, 2015 at 23:49 UTC | |
by tel2 (Pilgrim) on Mar 19, 2015 at 02:01 UTC | |
by SuicideJunkie (Vicar) on Mar 19, 2015 at 20:09 UTC | |
by tel2 (Pilgrim) on Mar 19, 2015 at 23:50 UTC | |
by martin (Friar) on Mar 20, 2015 at 03:43 UTC | |
| |
|
Re: Tidy up conditions
by jeffa (Bishop) on Mar 18, 2015 at 23:37 UTC | |
by tel2 (Pilgrim) on Mar 19, 2015 at 02:03 UTC |