#!/bin/perl -w package My::Module; use vars qw(@ISA); use Carp; use Tie::Hash; @ISA = qw(Tie::StdHash); sub TIEHASH { my($class) = shift; my($self) = { }; bless $self, $class; } sub FETCH { my($self, $key) = @_; unless (exists $self->{$key}) { if (my $meth = $self->can($key)) { $self->{$key} = $meth->($self); } else { warn "No method for '$key'"; $self->{$key} = undef; } } return $self->{$key}; } sub new { my($class) = shift; my(%hash); tie %hash, $class; $hash{filename} = shift; return \%hash; } sub my_method { my $self = shift; open(IN, $self->{filename}) or croak "Couldn't open file $self->{filename}: $!"; while () { print; } close IN; } 1; package main; # THIS WORKS: my $obj1 = new My::Module($ARGV[0]); my $method = 'my_method'; print $obj1->{$method}; # THIS DOESNT WORK -- error message: # Modification of a read-only value attempted at /home/apirkle/perl/bug.pm line 41. my $obj2 = new My::Module($ARGV[0]); print $obj2->{$_} for (qw|my_method|); exit;