This Week I Learned 2018 - Week 21

Previous week post or something from the past.

Malaysian honorific title, an analysis. How many Datuk, Tan Sri, and others have been granted by each state. Yes, you can check whether a person have received any honorific title.

The more I coded with Perl, the more I've learned something so obvious that I didn't pay much attention. For example, to check if a method or subroutine exists in a class or package, use `can()`.

Check if method exists and invoke it.
sub make {
  my ($self, $item) = @_;
  my $method = "make_$item";
  if ($self->can($method)) {
    $self->$method(whatever);
  } else {
    die "No such method $method";
  }
}

Another approach, using reference. If the method or subroutine not found, `undef` returned.
if ( my $ref = __PACKAGE__->can("$method") ) {
    &$ref(...);
}

Check if a hash is empty? Perl's TMTOWTDI is just crazy.

if (%foo) {
    # %foo has at least one key
} else {
    # %foo has no keys
}

What is the closet HTTP method equivalent to ping? HTTP HEAD.

2018 Perl survey results. Surprised to look at the result of the age group. Two things that caught my attentions. First, there are developers who still code in their 70+ years old. Second, young new developers have been declining. You can see the trend there, Perl it's a dying programming language.


No comments:

Post a Comment