Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Autoboxing by subclassing CORE

by tobyink (Abbot)
on Jun 20, 2012 at 14:40 UTC ( #977370=obfuscated: print w/ replies, xml ) Need Help??

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'

Comment on Autoboxing by subclassing CORE
Download Code
Re: Autoboxing by subclassing CORE
by moritz (Cardinal) on Jun 20, 2012 at 15:11 UTC
Re: Autoboxing by subclassing CORE
by Anonymous Monk on Jun 20, 2012 at 17:39 UTC

    but even experienced Perl programmers would probably do a double-take when seeing it

    I don't think so

Re: Autoboxing by subclassing CORE
by choroba (Abbot) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: obfuscated [id://977370]
Approved by moritz
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2014-08-29 22:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (289 votes), past polls