Skip to content

Instantly share code, notes, and snippets.

View ichesnokov's full-sized avatar

Ilya Chesnokov ichesnokov

View GitHub Profile
@ichesnokov
ichesnokov / prepare-commit-msg.pl
Last active April 22, 2020 15:46
Script adding ticket name to commit message if branch name starts with it
#!/usr/bin/env perl
=head1 DESCRIPTION
Adds branch name to the beginning of your commit message if branch name is
something like C<APP-1234> or C<APP-1111-GL123>, and message doesn't start with
branch name yet.
=head1 USAGE
#!/usr/bin/env perl
use strict;
use warnings;
use experimental qw(signatures);
use Time::HiRes qw(gettimeofday tv_interval);
# Constants are faster than vars
use constant BAILOUT => 16;
use constant MAX_ITERATIONS => 1000;
@ichesnokov
ichesnokov / benchmark-try-vs-eval.pl
Created April 14, 2017 15:55
benchmark-try-vs-eval
#!/usr/bin/env perl
use strict;
use warnings;
use lib 'lib';
use Try::Tiny;
use Benchmark qw( cmpthese );
use JSON::XS;
my $coder = JSON::XS->new->utf8;
@ichesnokov
ichesnokov / gist:7feea4c15a868d48e5a0
Created November 24, 2015 20:38
map-for-foreach speed comparison
perl -MBenchmark=cmpthese -E 'cmpthese(10000, { for => sub { my $i = 0; for (1..1000) { $i += $_; } }, map => sub { my $i=0; map { $i += $_; } 1..1000 }, foreach => sub { my $i=0; foreach (1..1000) { $i+=$_; } }, postfix_for => sub { my $i=0; $i+=$_ for 1..1000; }, postfix_foreach => sub { my $i = 0; $i += $_ foreach 1..1000; } });'
Rate map postfix_foreach for foreach postfix_for
map 12195/s -- -27% -27% -29% -33%
postfix_foreach 16667/s 37% -- -0% -3% -8%
for 16667/s 37% 0% -- -3% -8%
foreach 17241/s 41% 3% 3% -- -5%
postfix_for 18182/s 49% 9% 9% 5% --
@ichesnokov
ichesnokov / Service.pm
Created March 9, 2015 18:02
overridden new()
package My::DB::ResultSet::Service;
use our::way;
use parent 'DBIx::Class::ResultSet';
# Wrapper to exclude services with 'deleted' status from search
sub new {
my $class = shift;
@ichesnokov
ichesnokov / Service.pm
Created March 9, 2015 17:52
Subclassed resultset with overridden search_rs()
# Wrapper to exclude services with 'deleted' status from search
sub search_rs {
my $self = shift;
my $next_method = $self->next::can('search_rs');
my $status_alias = $self->current_source_alias . '.status';
return $self->$next_method(@_)
->$next_method({ $status_alias => { '!=' => 'deleted' } });
}
@ichesnokov
ichesnokov / check-perl
Created November 5, 2013 14:54
Check syntax of all modified perl files (used before committing stuff).
#!/bin/bash
for f in `git status | grep modified | perl -lanE 'say $F[-1] if /[.](p[lm]|t)$/' | sort -u`
do perl -c $f
done
@ichesnokov
ichesnokov / Results
Created May 28, 2013 12:26
Copying benchmark
~/ $ ./copy-benchmark.pl
Rate plain plain_1_2
plain 138889/s -- -70%
plain_1_2 458716/s 230% --
@ichesnokov
ichesnokov / Output
Created August 31, 2012 13:48
Type error
Attribute (details) does not pass the type constraint because: Validation failed for 'My::Details::Stored' with value My::Details=HASH(0x2e9c1b0) (not isa My::Details::Stored) at /opt/perlbrew/perls/perl-5.14.2/lib/site_perl/5.14.2/x86_64-linux/Moose/Meta/Attribute.pm line 1275
Moose::Meta::Attribute::verify_against_type_constraint('Moose::Meta::Attribute=HASH(0x2ea4218)', 'My::Details=HASH(0x2e9c1b0)', 'instance', 'My::API::Request=HASH(0x2ea22e0)') called at /opt/perlbrew/perls/perl-5.14.2/lib/site_perl/5.14.2/x86_64-linux/Moose/Meta/Attribute.pm line 1262
Moose::Meta::Attribute::_coerce_and_verify('Moose::Meta::Attribute=HASH(0x2ea4218)', 'My::Details=HASH(0x2e9c1b0)', 'My::API::Request=HASH(0x2ea22e0)') called at /opt/perlbrew/perls/perl-5.14.2/lib/site_perl/5.14.2/x86_64-linux/Moose/Meta/Attribute.pm line 531
Moose::Meta::Attribute::initialize_instance_slot('Moose::Meta::Attribute=HASH(0x2ea4218)', 'Moose::Meta::Instance=HASH(0x2eaaf98)', 'My::API::Request=HASH(0x2ea22e0)', 'HASH(0x2eab478)') called a
@ichesnokov
ichesnokov / gist:2205373
Created March 26, 2012 14:15
MooseX::LazyRequire bug?
package Account;
use Moose;
use MooseX::LazyRequire;
has 'password' => (
is => 'rw',
isa => 'Str',
);
__PACKAGE__->meta->make_immutable;