Brother
frankus,
Perl does not force you to OO. You have to decide one day to cross the threshold and find out what's in it for you.
So, let me stick my neck out. I'm not crazy about objects,
in Perl or otherwise. I just use them.
In my experience, Perl objects are good for structuring any program,
from the lowly quick hack to a large application involving tens of
modules, other people's and your own.
IMO, all those classical OO benefits show up more
often than not in the programmer's daily work - the bread that our sandwiches are made of, so to speak
- bundling the code and data together
- encapsulating the data
- managing object instances, each with it's state properly initialized and maintained
- abstraction
- separating interface from implementation
- reusable modules
- easy testing of modules
Let me give you an example - a quick hack I did last night.
I wanted to have a simple probe for the quantity of memory that
my perl program is hogging at different phases of execution.
I looked around for a Win32 module that could help me, and I found
two - IProcess and Win32::PerfLib. I installed the former, tried the test scripts that came with it, and then
simplified one of scripts and packed it into a module.
Here is my test script - just to exercise my module
#! perl -w
# TestPerlprocdata.pl
use Perlprocdata;
my $pp = new Perlprocdata;
my @x;
for (0..5) {
$pp->printProcessMemInfoShort; # probe
# gobble some memory
push @x, 1 for (0...100000);
sleep 1;
}
# free the memory (to perl, not to the OS)
@x = ();
for (0..10) {
$pp->printProcessMemInfoShort; # probe
# gobble some more memory
push @x, 1 for (0...100000);
sleep 1;
}
# free the memory (to perl, not to the OS)
@x = ();
__END__
[Name] [PageFaults/s] [PeakWS] [WS]
perl.exe 841 3436544 3436544
perl.exe 1366 5586944 5586944
perl.exe 1894 7753728 7753728
perl.exe 2694 11034624 11034624
perl.exe 2945 12062720 12062720
perl.exe 3344 13697024 13697024
perl.exe 4545 18620416 18620416
perl.exe 4545 18620416 18620416
perl.exe 4545 18620416 18620416
perl.exe 4545 18620416 18620416
perl.exe 4545 18620416 18620416
perl.exe 4545 18620416 18620416
perl.exe 4545 18620416 18620416
perl.exe 4647 19038208 19038208
perl.exe 5046 20676608 20676608
perl.exe 5444 22306816 22306816
perl.exe 5844 23949312 23949312
And here is my quick and dirty module:
#! perl -w
# Perlprocdata.pm by Rudif@bluemail.ch
use strict;
package Perlprocdata;
# uses Win32::IProcess by Amine Moulay Ramdane
# from website: http://www.generation.net/~aminer/Perl/
use Win32::IProcess qw(
PROCESS_QUERY_INFORMATION PROCESS_VM_READ INHERITED INHERITED DIGI
+TAL NOPATH
);
#----------------------------------------------------------
sub new {
my ($class, %args) = @_;
my $self = {};
bless $self, $class;
my $obj = $self->{obj} = new Win32::IProcess || die "Can not creat
+e an IProcess object..\n";
my @EnumInfo;
$obj->EnumProcesses(\@EnumInfo);
my $size=scalar(@EnumInfo);
for(my $j=0;$j<$size;$j++) {
if ($EnumInfo[$j]->{ProcessName} =~ /perl/i) {
$self->{EnumInfo} = $EnumInfo[$j];
my $Info;
$obj->GetProcessMemInfo($EnumInfo[$j]->{ProcessId},\$Info)
+;
$self->{Info} = $Info;
my @data = (
$EnumInfo[$j]->{ProcessName},
$Info->{PageFaultCount},
$Info->{PeakWorkingSetSize},
$Info->{WorkingSetSize},
$Info->{QuotaPagedPoolUsage},
$Info->{QuotaNonPagedPoolUsage},
$Info->{PagefileUsage});
}
}
return $self;
}
#----------------------------------------------------------
sub getProcessMemInfo {
my $self = shift;
$self->{obj}->GetProcessMemInfo($self->{EnumInfo}{ProcessId},\$sel
+f->{Info});
}
#----------------------------------------------------------
sub printProcessMemInfoShort {
my $self = shift;
$self->getProcessMemInfo;
printf("\n\n%17.15s%15.14s%12.11s%12.11s\n\n",
"[Name]","[PageFaults/s]", "[PeakWS]","[WS]")
unless $self->{printed}++;
printf("%17.15s%15.14s%12.11s%12.11s\n",
$self->{EnumInfo}{ProcessName},
$self->{Info}{PageFaultCount},
$self->{Info}{PeakWorkingSetSize},
$self->{Info}{WorkingSetSize});
}
1;
__END__
In this little exercice I can see just about every OO benefit that I listed above.
Convinced?
HTH
Rudif
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.