develooper Front page | perl.perl5.porters | Postings from July 2001

sub NAME BLOCK as rvalue?

Thread Next
Michael G Schwern
July 18, 2001 17:18
sub NAME BLOCK as rvalue?
Message ID:
I've been trying to think of a nice way to implement a privacy system
and I've hit a bit of an interface snag.  What I've got at the moment
is this:

        private '_foo', sub {

meaning "declare the private method 'foo'" by passing in a method name
and code ref to private().  Its clumsy syntax.  What I'd really like
to be able to say is this:

        private sub _foo {

This feels like nice syntax to be able to have.  sub NAME BLOCK as an
rvalue.  A more natural alternative to sub NAME : ATTRS BLOCK.  There
are times when you want to simultaneously declare a new named method
*and* do something nasty to it.  Rather than having to do it as a
seperate string + code ref, why not do it together?

Here's another example:

        memoized sub bar {

similar to:

        memoized 'bar', sub { ... };

but a more natural interface.

It would be best if a semi-colon was unnecessary at the end.  I don't
know how horribly this will twist the parser to do.

To make it really work well, sub NAME BLOCK wouldn't return its code
reference like an anonymous sub.  It would return its *name*.  Why?
Consider how to implement private().

        sub private {
            my($name) = shift;
            my $class = caller;
            my $sub_ref = \&{$class.'::'.$name};

            no warnings 'redefined';
            *{caller.'::'.$name} = sub {
                my $self = $_[0];
                unless( caller eq $class ) {
                    croak sprintf "Private method %s of %s called by %s",
                        $name, $class, scalar caller;
                goto $sub_ref;

I need both the name *and* code ref for it to work.  Same with
memoize().  If I have the name, I can get the ref, but vice-versa is
harder.  You might be able to get away with searching the symbol table
for the code ref...

    my($name) = grep { \&{${caller.'::'}{$_}} eq $ref } keys %{caller.'::'};

but that's nasty.



Michael G. Schwern   <>
Perl6 Quality Assurance     <>	     Kwalitee Is Job One
i  need another hit
just gimme some fucking paste
please, it huts so bad
	-- Fmh

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About