2017/09/11

Copy subroutines in Perl

no strict 'refs';
*{ $self . '::sub' } = \&{ $class . '::sub' };

2017/06/12

Subclassing with overriding in Perl

package MyClass;

use strict;
use warnings;

use parent 'ParentClass';

sub new {
    my $class = shift;
    my %options = @_;
    my $self = $class->SUPER::new(@_);
    $self->{my_option} = 1 if $options{my_option};
    return $self;
}

sub my_option {
    my $self = shift;
    $self->{my_option} = shift if @_;
    return $self->{my_option};
}

sub overridden_sub {
    my $self = shift;
    my %args = @_;
    if ($self->{my_option}) {
        return do_my_logic_here();
    }
    return $self->SUPER::overridden_sub(@_);
}

2017/01/30

Vim Git trick

http://felixge.de/2013/08/08/vim-trick-open-current-line-on-github.html

~/.gitconfig:
[alias]
    url =! bash -c 'git config --get remote.origin.url | sed -E "s/.+:\\(.+\\)\\.git$/https:\\\\/\\\\/github\\\\.com\\\\/\\\\1/g"'

~/.vimrc:
nnoremap <leader>o :!echo `git url`/blob/`git rev-parse --abbrev-ref HEAD`/%\#L<C-R>=line('.')<CR> \| xargs open<CR><CR>

2016/12/22

Perl hash slices

# filling
%{ $hash } = map { $_ => 1 } @{$array};

# squashing
@{ $hash }{ @{$array} } = (1) x @{$array};
@{ $hash }{ @{$array} } = ();

2016/12/19

Quotes

codes:  friendly  numerical  hex
    "   &quot;    &#34;      &#x22;
    '             &#39;      &#x27;