chromatic has asked for the wisdom of the Perl Monks concerning the following question:
As people may refine and redefine methods within these objects, I have come up with the following way to "refresh" the existing objects on the fly. (The Everything engine uses eval liberally, but that imposes performance constraints I wish to avoid, if at all possible.) Is there a better way than this?
and the Sub.pm file is, initially:#!/usr/bin/perl -w -I"/home/chromatic/perl" use strict; use Sub; my $tester = Sub->new(); $tester->name; { local *OUTPUT; open(OUTPUT, ">>/home/chromatic/perl/Sub.pm") || die "Can't append +: $!\n"; print OUTPUT "\nsub game {\n"; print OUTPUT " print \"Hi, this is the game subroutine.\\n\";\n"; print OUTPUT "}\n"; print OUTPUT "1;"; close OUTPUT || die "Can't close: $!\n"; } require "/home/chromatic/perl/Sub.pm"; $tester = Sub->new; $tester->game;
This seems to work (aside from having extra 1; statements in the code :), but I get "Subroutine ___ redefined" errors. That makes sense... but is there a better way to do it?#!/usr/bin/perl -w package Sub; use strict; sub new { my $class = shift; my $this = {}; $class = ref($class) || $class; bless($this, $class); return $this; } sub name { print "Hi, this is the name subroutine.\n"; } 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Redefining Subroutines on the Fly in a Persistent Application
by Anonymous Monk on Feb 23, 2000 at 03:46 UTC | |
|
Re: Redefining Subroutines on the Fly in a Persistent Application
by btrott (Parson) on Feb 23, 2000 at 03:25 UTC | |
by chromatic (Archbishop) on Feb 25, 2000 at 21:47 UTC | |
|
Re: Redefining Subroutines on the Fly in a Persistent Application
by stephen (Priest) on Mar 09, 2000 at 22:39 UTC | |
|
Re: Redefining Subroutines on the Fly in a Persistent Application
by Anonymous Monk on Feb 25, 2000 at 07:32 UTC |