http://www.perlmonks.org?node_id=977370

This is a slightly silly thing that I've recently been surprised to discover actually works...

{ package String; use base 'CORE'; use overload q[bool] => '_deref', q[""] => '_deref', q[0+] => '_deref', fallback => 1, ; sub new { my ($class, $str) = @_; bless \$str => $class; } sub _deref { ${$_[0]}; } } my $greeting = String->new("Hello World\n"); print $greeting->substr(6);

It's not really obfuscated, but even experienced Perl programmers would probably do a double-take when seeing it, and I couldn't think of a better section to put it in.

By the way, you can also use CORE directly - the subclassing is not strictly necessary.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re: Autoboxing by subclassing CORE
by moritz (Cardinal) on Jun 20, 2012 at 15:11 UTC
Re: Autoboxing by subclassing CORE
by choroba (Cardinal) on Jun 20, 2012 at 21:19 UTC
    What version of Perl is needed to make it run? I am getting
    Can't locate object method "substr" via package "String" at - line 20.
    in 5.14.2.

      I'm using 5.16. perl5160delta notes various improvements to the CORE and CORE::GLOBAL namespaces. Perhaps in earlier Perls CORE::substr doesn't work, but some other interesting CORE functions are likely to work.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Autoboxing by subclassing CORE
by ambrus (Abbot) on Oct 09, 2012 at 21:05 UTC

    Wow, this might be the first obfu posted here that requires perl 5.16 in an essential way. Nice.

A reply falls below the community's threshold of quality. You may see it by logging in.