Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

What's more readable?

Example 1 (my work's internal source filter format):

  sub function1( CODE $code, ARRAY $arr )
  {
    # body
  }
Example 2 (standard Perl):

  sub function1 {
    my ($code, $arr) = @_;
    die "Bad params." unless ref($code) eq "CODE"
                         and ref($arr)  eq "ARRAY";
    # body
  }
Example 3 (Method::Signature):

  func function1 (Code $code, Array $arr)
  {
    # body
  }


Yes examples 1 & 3 are much better. At some point signatures will be added to core Perl (class/method nearly made it into 5.16 as part of p5-mop proposal. Fingers crossed it will be in 5.18 next year).

When not using perl5i or playing with MooseX::Declare I will usually default to Params::Validate so your example would look like this:

  sub function1 {
    my ($code, $arr) = validate_pos( @_, {type => CODEREF}, {type => ARRAYREF} );
    ...
  }




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: