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.
|
|---|
| 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 | |
by tobyink (Canon) on Jun 21, 2012 at 00:57 UTC | |
|
Re: Autoboxing by subclassing CORE
by ambrus (Abbot) on Oct 09, 2012 at 21:05 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |