#!/usr/local/bin/perl use warnings; use strict; use Want; sub lvalueMethod($) { my ($name) = @_; my ($package) = caller; no strict 'refs'; *{$package . "::" . $name} = sub :lvalue { my $self = shift; if (!want('LVALUE') && ref($self->{$name}) eq 'CODE') { goto &{$self->{$name}}; } $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 Re^4: defining methods on the fly
by jdhedden
in thread defining methods on the fly
by flogic
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |