[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] bad perl - need help - dispatch table




On Aug 25, 2004, at 9:29 PM, Terry wrote:
I have two sets of subs.  One set has similarities and so does the
other.

Perhaps you should probably have 2 packages, as I described earlier. Group the subs by concept, and have one package per concept. Easier to maintain and support in the long run.

$ cat p1.pm
package p1;
sub x { print "x"; }
1;

$ cat p2.pm
package p2;
use Exporter;
@ISA    = qw(Exporter);
@EXPORT = qw(&y &z);
sub y { print "y"; }
sub z { print "z"; }
sub a { print "a"; }
1;

$ cat j.pl
#!/usr/bin/perl

use p1;
use p2;

# You can explicitly identify the packages your subs are in
&p1::x;
&p2::y;

# Or if your .pm's export their subs, like p2.pm does, you can call
# the sub # as if it were defined in main. (Not necessarily recommended.)
# see perldoc Exporter
&y;
&z;

$ ./j.pl
xyyz$

The problem is that they are invoked at different times in
main.    So I don't know how to have them all in one sub.

You can invoke any sub that lives anywhere at any time. perl has not problem with that. Whether or not you could/should have one smart subroutine is a separate question. It depends on what the subs are doing and how similar the different subs are. If you get a few of your subs written you might want to post 'em, and see if anyone has any tips.

This is
getting pretty confusing.  I think I am going to implement what you
suggested with separate subs for each to start with.  I will call it
0.1.  I will re-invent the ocean in 0.2.  :)

Yup. Start writing subs, and if you see that you're writing similar blocks of code, go back and make your code smarter and shorter. Nowadays, that's called "refactoring". It's good for you. -grin-

j