#!/usr/local/bin/perl use warnings; use strict; use Want; sub lvalueMethod($) { my ($name) = @_; my ($package) = caller; my ($fn) = $package . "::" . $name; no strict 'refs'; *{$fn} = sub : lvalue { my $self = shift; local *call = sub { $self->{$name}->(@_); }; if ( !want('LVALUE') && ref( $self->{$name} ) eq 'CODE' ) { goto &call; } $self->{$name}; }; } { package Foo; sub new { bless {}, shift; } main::lvalueMethod("bar"); } my ($x) = Foo->new(); print "$x\n"; $x->bar = 5; print $x->bar, "\n"; $x->bar = sub { print( @_, "\n" ); }; $x->bar("Hello World");
In reply to defining methods on the fly by flogic
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |