I've been experimenting with ideas to allow people to create their own Perl objects via a web application. The objects will execute in a server framework (becoming application components).

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?

#!/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;
and the Sub.pm file is, initially:
#!/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;
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?

In reply to Redefining Subroutines on the Fly in a Persistent Application by chromatic

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.