Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

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

I'm looking only at your first examples in that post for now.

To write a robust Fibonacci function, you might write something like this:
sub fib { croak("…") if @_ != 1; my $nth = $_[0]; croak("…") if not defined $nth; croak("…") if $nth !~ /^+?\d+$/a; return $nth if $nth <= 1; return fib( $nth - 1 ) + fib( $nth - 2 ); }

You are being unnecessarily verbose, aren't you? All those checks just fold into two:

my $fib_die_msg = 'fib() takes one positive integer as argument, abort +ed'; sub fib { my($nth = shift) =~ m{^\d+$} or die $fib_die_msg; @_ and die $fib_die_msg; return $nth if $nth <= 1; return fib( $nth - 1 ) + fib( $nth - 2 ); }

The two checks can also be written on one line and no harm done:

sub fib { my($nth = shift) =~ m{^\d+$} and ! @_ or die $fib_die_msg; ... }

The checks can be skipped completely, if the caller is the recursive subroutine itself:

sub fib { my $nth = shift; (caller)[3] eq 'fib' or $nth !~ m{^\d+$} or @_ and die $fib_die_msg; return $nth if $nth <= 1; return fib( $nth - 1 ) + fib( $nth - 2 ); }

Optional typing in signatures would improve that, for sure - but there's Params::Validate for instance. Then, what should happen if a sub takes an Uint and gets a polymorphic object which resolves to a Uint in numeric context via overloading, but as a filehandle at I/O ? Would we need typecasting? Would the object be aliased to its numeric interpretation in such a subroutine body, or would other slots and/or behaviors still be available and valid?

I personally believe that such constraints go against the Camel's hair, and against the best thing perl has since sliced bread, which is context awareness, which forces the programmer to be aware of the context in which they are moving things around.

Also, neither your Ruby, PHP nor Python versions do any parameter checks, but you write them for your perl5 version. Write an equivalent Python class, and you will have exexption definitions and try/catch branches and such.

No need to be unfair to perl to make a point - and no need to blow away the very foundations of perl either ;-)

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

In reply to Re: Recap: The Future of Perl 5 by shmem
in thread Recap: The Future of Perl 5 by Ovid

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 pondering the Monastery: (7)
As of 2024-03-29 08:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found