The principle of this obfu is pretty enough that I didn't want to junk it up with ugly obfusatory cruft. It is a simple Perl-doodle inspired by Damian's book. Enjoy.

qr;;for qq$Just another Perl hacker,\n$;package Regexp;BEGIN{%{__PACKAGE__.::}=''}sub AUTOLOAD{print}

Dave

Replies are listed 'Best First'.
Re: This engine burns regular
by jdalbec (Deacon) on Jun 14, 2005 at 01:37 UTC
    Given that package Regexp has only one method, you could also just override that method:
    qr;;for qq$Just another Perl hacker,\n$;package Regexp;sub DESTROY{print}
    Or you could override all the methods in the package (which amounts to the same thing), although that's longer than the original code by a character:
    qr;;for qq$Just another Perl hacker,\n$;package Regexp;BEGIN{$_=sub{print}for values%{__PACKAGE__.::}}

      You're on target. It's interesting that.....

      ...interesting that the regexp returned by qr// is automatically an object in package Regexp, and that there's nothing preventing you from adding other methods to the Regexp class. They can then be invoked as object instance methods like this:

      package Regexp; use strict; use warnings; sub wierd { print "Invoked wierd\n"; return $_[1] =~ $_[0]; } package main; use strict; use warnings; my $string = "This is a test.\n"; my $re = qr/\bis\b/; print "Match!\n" if $re->wierd( $string );

      This allows functionality to be added to the base Regexp class. But that's probably not the best way to mess around with regular expressions. It's probably more sane to bless a regular expression into a new class like this:

      package WierdRE; sub new { my( $class, $regexp ) = @_; bless qr/$regexp/, $class; } sub wierd { print "Invoked wierd\n"; return $_[1] =~ $_[0]; } package main; use strict; use warnings; my $string = "This is a test.\n"; my $re = WierdRE->new( '\bis\b' ); print "Match!\n" if $re->wierd( $string );

      The latter method at least leaves the built-in Regexp class as is, and creates a new class that is based on a blessed Regular Expression object.

      ....just food for thought...


      Dave