A
Andy Glew
Q: is the below the right way to
place Perl classes "inline", near to where
they are being used?
E.g. in file bar.pl
----------------------------------
use strict
print "Code befre interleaved package/classes\n";
{
package A;
sub new { bless {}; }
sub bar { print "A"; }
}
print "Code between interleaved package/classes\n";
{
package B;
sub new { bless {}; }
sub bar { print "B"; }
}
print "Code after interleaved package/classes\n";
A->bar();
B->bar();
my $a = A->new();
my $b = B->new();
$a->bar();
$b->bar();
----------------------------------
Putting the package statement inside the block curlies
looks wierd, but I guess it's the right way.
I know the basics of Perl classes:
A class is simply a package
A package is simply a file with "packge foo" at the top
(usually one that is on your include path)
Problem: I like to use programming patterns that
create lots of light-weight classes. Typically
they derive from some base class and override
one or two methods as close as possible to the
point of use.
E.g. in C++
int foo() {
class spec1 : public base {
public: int bar() { return 45; }
} s1;
return blif( s1 );
class spec2 : public base {
public: int bar() { return 67; }
} s2;
return blif( s2 );
}
I like this pattern because it puts the
classes right next to the point of use.
(Sometimes language restrictions make you move
them outside a function, but still they are
closer to the point of use than they would be
in a separate function.)
I.e. I use classes almost as a way of creating
lambda functions.
Q: is there any way to do this in Perl?
Placing these classes in separate files
makes them much less lightweight.
I've tried putting package more than once
in a file, with no luck. E.g. how do you
"use" multiple packages from the same file?
Perl seems to tightly couple classes and files.
---
Andy Glew
PREFERRED EMAIL: (e-mail address removed)
Although I am trying to quit Outlook,
I fetchmail all of my Outlook/Exchange email
to UNIX to read
FALLBACK EMAIL: (e-mail address removed)
503-264-4119
Potential bias: employed now by Intel
past by AMD, Intel, Motorola, Gould ...
This post is personal, and is not the opinion of
any of my employers, past or present.
place Perl classes "inline", near to where
they are being used?
E.g. in file bar.pl
----------------------------------
use strict
print "Code befre interleaved package/classes\n";
{
package A;
sub new { bless {}; }
sub bar { print "A"; }
}
print "Code between interleaved package/classes\n";
{
package B;
sub new { bless {}; }
sub bar { print "B"; }
}
print "Code after interleaved package/classes\n";
A->bar();
B->bar();
my $a = A->new();
my $b = B->new();
$a->bar();
$b->bar();
----------------------------------
Putting the package statement inside the block curlies
looks wierd, but I guess it's the right way.
I know the basics of Perl classes:
A class is simply a package
A package is simply a file with "packge foo" at the top
(usually one that is on your include path)
Problem: I like to use programming patterns that
create lots of light-weight classes. Typically
they derive from some base class and override
one or two methods as close as possible to the
point of use.
E.g. in C++
int foo() {
class spec1 : public base {
public: int bar() { return 45; }
} s1;
return blif( s1 );
class spec2 : public base {
public: int bar() { return 67; }
} s2;
return blif( s2 );
}
I like this pattern because it puts the
classes right next to the point of use.
(Sometimes language restrictions make you move
them outside a function, but still they are
closer to the point of use than they would be
in a separate function.)
I.e. I use classes almost as a way of creating
lambda functions.
Q: is there any way to do this in Perl?
Placing these classes in separate files
makes them much less lightweight.
I've tried putting package more than once
in a file, with no luck. E.g. how do you
"use" multiple packages from the same file?
Perl seems to tightly couple classes and files.
---
Andy Glew
PREFERRED EMAIL: (e-mail address removed)
Although I am trying to quit Outlook,
I fetchmail all of my Outlook/Exchange email
to UNIX to read
FALLBACK EMAIL: (e-mail address removed)
503-264-4119
Potential bias: employed now by Intel
past by AMD, Intel, Motorola, Gould ...
This post is personal, and is not the opinion of
any of my employers, past or present.