Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Autoboxing by subclassing CORE

by tobyink (Canon)
on Jun 20, 2012 at 14:40 UTC ( [id://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'

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-19 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found