Problems? Is your data what you think it is? | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Nice. But ... ;-) The next thing your users will ask for is defined_or_warn(), and if only for symmetry. It would contain nearly the same code as the final defined_or_die(), essentially a no-brainer. Then, many people (me included) prefer to use Carp instead of die() and warn(), and for them, having defined_or_carp(), defined_or_croak(), defined_or_confess(), and defined_or_cluck() would be natural. Other people would get mad if you always used Carp inside your module, so loading Carp should perhaps happen at runtime, or only when one or more of the four Carp-Class functions are exported. defined_or_call() is the generic solution for all of these cases, so you will probably end wrapping that function for all of the six warn()ing and die()ing functions. The only problem I see with these shortcuts is that they perhaps won't work reliably because the string argument may be interpolated too early:
... gives ...
... on 5.10.0 (Linux and Windows Strawberry). So, no problem here as long as the error string is on the right hand side of the maybe-undefined value. But who guarantees that function arguments are always evaluated from left to right? Maybe that fact is documented somewhere, I'm too lazy to search right now, and after all, it's your idea. ;-) Another quite obvious problem is the wrong line number in the second and third error message. Your module would have to compensate that. You will probably end re-implementing or using parts of Carp. CGI::Carp could also be a little problem. It uses Carp internally, but it also redefines die() and warn(), and it exports its own, modified versions of confess(), croak(), carp(), and cluck(). I think a possible workaround would be to detect a loaded CGI::Carp at runtime (if exists $INC{'CGI/Carp.pm'}?) and call its functions instead of the original ones. Another nice shortcut could replace //=, something like defined_or_assign(\$var,$value), or even defined_or_assign($var,$value) when the wrapper has a prototype of (\$$). For boolean shortcuts, you would again need a callback function (defined_or_assign($var,expensive_or_killing_function(...)) vs. defined_or_assign_return_value_of($var,sub { expensive_or_killing_function(...) })). And when you really implement defined_or_assign_return_value_of(), think about a shorter name for it. The other function names are also awfully long, perhaps you could export shorter aliases for them (def_or(), def_or_die(), def_or_warn(), ..., def_or_set(), def_or_set_rv()). Alexander
-- Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-) In reply to Re^3: RFC: Defined-Or for before Perl 5.10
by afoken
|
|