Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

def_or is a good name, short and quite obvious. Many computer languages use "def" as a shortcut for "define" or "defined". So, no surprises here.

dor_call, on the other hand, is not so obvious, you have cut away too much. What the heck is "dor"? A mis-spelled door? And what would be a door-call? "Don't make me think!" def_or_call is longer, but also clearer.

So, now that I have def_or_call(), my code will very soon look very ugly and my fingers will bleed from the many keystrokes that I need:

# Perl >= 5.10 my $pid=fork() // die "Can't fork: $!"; # Perl < 5.9 manually my $pid=fork(); defined($pid) or die "Can't fork: $!"; # 123456789012345 # 15 extra characters # Perl < 5.9 + your module my $pid=def_or_call(fork(),sub { die "Can't fork: $!" }); # 123456789012345678 # 18 extra characters -- oops! # And you should never forget to wrap die in a sub ... # Perl < 5.9 + your module with defined_or_die # (remember to check if arguments are ALWAYS evaluated left-to-right!) # (or find a better solution ...) my $pid=def_or_die(fork(),"Can't fork: $!"); # 12345 # just 5 extra characters - 10 less than manually # and no wrapper needed

So, I think you should really provide def(ined)_or_die and def(ined)_or_warn. Sure, it is more work than the other functions. You should really look into the Carp sources to see how they cope with the problems your module will also have. Like I said, I'm very sure that you can use Carp for the line number problem. You could load it at runtime, inside def_or_die()/def_or_warn(). Something like this:

sub def_or_die { my ($v,$msg)=@_; return def_or_call($v,sub { require Carp; die Carp::some_magic_helper_function($msg); }); }

And I think you should also provide their Carp equivalents, for the same reasons. You could export them on demand only, and load Carp at runtime. Something like this:

sub def_or_carp { my ($v,$msg)=@_; return def_or_call($v,sub { require Carp; Carp::please_ignore_these_wrappers(); Carp::carp($msg); }); }

Support for CGI::Carp would look about like this:

sub def_or_carp { my ($v,$msg)=@_; return def_or_call($v,sub { if (exists $INC{'CGI/Carp.pm'}) { # CGI::Carp loaded CGI::Carp::please_ignore_these_wrappers(); CGI::Carp::carp($msg); } else { require Carp; Carp::please_ignore_these_wrappers(); Carp::carp($msg); } }); }

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^5: RFC: Defined-Or for before Perl 5.10 by afoken
in thread RFC: Defined-Or for before Perl 5.10 by molecules

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-16 15:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found