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 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


In reply to Re: Hacking with objects by Rudif
in thread Hacking with objects by frankus

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.