October 2013 Archives

Arrow Operator Shenanigans

Out of curiosity, boredom, and some inspiration by the method abuse in Safe::Isa and Object::Remote, I tried the following:

use strict;
use warnings FATAL => 'all';
use feature 'say';

my $split = sub {
    my ( $str, $sep ) = @_;
    return [ split( $sep, $str ) ];
};

my $join = sub {
    my( $arr, $sep ) = @_;
    return join($sep,@$arr);
};

say "Lorem ipsum dolor sit amet"->$split(" ")->$join(":");

my $inc = sub {
    $_[0]++;
};

my $num = 4;
$num->$inc;
$num->$inc;
say $num;

I was mildly surprised that it worked. I was more surprised that I couldn't find a reason for why it worked in the documentation for the Arrow Operator.

edit [ In the comments I was pointed to perldoc perlobj where this behavior is documented. ]

I'm certain coding this way would be a Pretty Bad Idea ( especially as an undocumented language feature ) but it has a sort of appeal; though 70% of the appeal is just the sheer hackery of it with the rest being some theory that chaining may be easier to read than g(f(x)).

Update dist.ini with the versions you have installed

My usual behavior for setting version requirements in dist.ini:

Go through each dep, search for it on CPAN, copy and paste the version number.

I decided that
a) that takes too long
b) That's not the versions I've been testing with.

So I hacked this together: dzil_myversions.pl

Synopsis

$ dzil listdeps | ~/dotfiles/home_bin/dzil_myversions.pl 
aliased = 0.31
Compress::Raw::Zlib = 2.062
Data::Dumper::Concise = 2.020
Encode = 2.54
Ext…

About Samuel Kaufman

user-pic CTO / Codemonkey, Socialflow.com