HASH vs Regexp

M

Michele Dondi

First of all please let me apologize for the (most probably) badly
chosen subject line...

I found out that qr// returns a *blessed* reference into the Regexp
class, allowing one to do things like

#!/usr/bin/perl -l

use strict;
use warnings;

sub Regexp::matches {
my $re=shift;
grep /$re/, @_;
}

print for qr/[a-z]+$/->matches(qw/foo bar1 baz baz2/);

__END__


Now, rumors insist on the fact that basically in Perl6 everything will
be an object. OTOH I wonder why in current perl references are not
automatically bless()ed, so that one could do something like this

#!/usr/bin/perl -l

use strict;
use warnings;

sub HASH::keys { keys %{shift()} };

print for { A => 1, B => 2, C => 3 }->keys;

__END__


Of course I understand that technically some difficulties may arise
because (things like) hashrefs are commonly used to implement generic
classes, but then wouldn't add such a support as that hinted above
require just some more bits of magic a' la Perl?


Michele
 
T

Tassilo v. Parseval

Also sprach Michele Dondi:
Now, rumors insist on the fact that basically in Perl6 everything will
be an object. OTOH I wonder why in current perl references are not
automatically bless()ed, so that one could do something like this

#!/usr/bin/perl -l

use strict;
use warnings;

sub HASH::keys { keys %{shift()} };

print for { A => 1, B => 2, C => 3 }->keys;

__END__


Of course I understand that technically some difficulties may arise
because (things like) hashrefs are commonly used to implement generic
classes, but then wouldn't add such a support as that hinted above
require just some more bits of magic a' la Perl?

IMHO, it certainly would and actually making that work requires just a
tiny patch to the core. However, this idea has unfortunately been
rejected by the porters. It's available from the CPAN, though:

<http://search.cpan.org/~chocolate/autobox-0.11/>

It requires you to patch the perl source and compile yourself. I had
quite a bit of fun with this modification and if I were in charge of
things, it'd surely go into perl5.10. But, alas, I'm not. :)

Tassilo
 
M

Michele Dondi

IMHO, it certainly would and actually making that work requires just a
tiny patch to the core. However, this idea has unfortunately been

Fine to know! I feared that this may have revealed to sound just like
yet another crankery, like some of my posts on "similar subjects"
(i.e. Perl's syntax/semantics) in the past did...
rejected by the porters. It's available from the CPAN, though:

<http://search.cpan.org/~chocolate/autobox-0.11/>

I'll try it ASAP, TY!


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top