Implementation of Perl Object-Oriented Programming

Source: Internet
Author: User
Reason

Looking back at Perl recently, on the one hand, my customer manufacturers developed a lot of things with Perl, on the other hand, they reviewed the Perl language and had some experiences. Record it for your reference.

I am working on a small program, that is, parsing a text file, processing the content, and saving it in another file.

Background

1. Perl's Object-Oriented Programming implementation

Please refer:

O 'Reilly-advanced Perl programming.pdf-Chapter 7th [object-oriented programming]

2. How to Use references in Perl

Http://www.chinaunix.net/jh/25/504623.html

3. How to read files in Perl

Perl cookbook [O 'Reilly Perl cookbook.pdf]-Chapter 7th [File Access]

4. Regular Expression of Perl

Http://www.chinaunix.net/jh/25/159388.html

Implementation

Object programming is a task of maintenance and expansion to solve long-term dependency troubles. If we write our code only once and do not need to change it any more, the modern programming model may not evolve. Of course we know this is impossible. The only thing in the world that remains unchanged is change.

Perl is a powerful scripting language that is used in all aspects (but not in the UI). However, due to the characteristics of the scripting language, the write once label is always with it. It is generally considered an obscure and difficult-to-maintain programming language.

In fact, there are some misunderstandings in this regard. Language usage is related to the level and conservation of developers. Even with C #, a good high-level object language, some developers can still write poorly-understood language structures. Therefore, developers not only need to learn how to use object programming in advanced languages, but also need to have a deep understanding of the Object Concept and finally apply this concept to different languages, make the language structure modular and object-oriented.

When I was new to Perl, I thought this language could not write an object-oriented language structure. Therefore, I have many complaints. However, as learning goes deeper, Perl can use the Object Concept to build a proper and clear language structure.

I will post the code for the last test first:

use filehandle::filehandler;use filehandle::catchWorkerId;print "\nTEST fildehander base class \n\n";my $filehandler =  filehandler->new("c:\\test2.ini");print $filehandler->getDealedStrings();print "\nTEST fildehander sub-class \n\n";$filehandler = catchWorkerId->new("c:\\test2.ini");print  $filehandler->getDealedStrings();$filehandler->exportToFile("C:\\test2_new.ini");

Filehandler. PM and catchworkerid. PM are two important modules (class ). The filehandler class is the basic action of the parent class and completes File Parsing:

Open a file

Read per line

Parses the string and saves it in the array variable of the class.

Export to an appropriate file

In this way, we can only complete the required changes in the subclass. See the subclass code catchworkid. PM.

package catchWorkerId;@ISA = qw(filehandler);use strict;use warnings;sub convert{   my ($self,$linetext) = @_;  my $pattern = "[a-z0-9]{9}[(]";  my @a;  if(@a=($linetext =~/$pattern/gio))  {    $a[0]=~s/[(]//g;    return (1,$a[0]);   }return (0,"");}1;

Code Description

The subclass code is just a method convert, and its input is a $ linetext string scalar. We will judge the regular expression. If the match is met, true + is returned for the replaced $ linetext, if not, false + Null String is returned.

If we can define C # In a language with strong object types as follows:

bool convert(string linetext, out string convetedlinetext){ //implemenet code}

Since Perl is an interpreted non-type language, we need to implement this function flexibly. Therefore, how to use old languages to implement specific functions under the new concept requires the innovation capability of developers. These things will not be described in the tutorial, developers need to make their own exploration.

As you can see, through the inheritance of classes, we can complete our preset goal by defining only functions in the subclass. Note the following:

@ ISA = QW (filehandler );

It means that the filehandler. PM function is introduced. First, the call function is searched in the catchworkerid. If it is not found, it will continue to be searched in filehandler. PM. In this way, we have completedClasses are inherited in disguise..

 

The implementation of the parent class is provided here: filehandler. PM

package filehandler;use strict;use warnings;#newsub new{  my($class, $filename) = @_;  my $self ={    _filename =>$filename    };  my @ss;  $self->{_dealedStringArray}= \@ss;  return bless($self,$class);}#really open filesub getDealedStrings{  my($self) = @_;  open (FILEHANDLE,$self->{_filename})|| die("can't open file: $self->{_filename}");  #start to deal string line by line!while(<FILEHANDLE>){ $self->handleLine($_);}close (FILEHANDLE);return  @{$self->{_dealedStringArray}} ;}#deal string line by linesub handleLine{  my ($self,$linetext) = @_;  my @result = $self->convert($linetext);  if($result[0]==1)  {  $self->_addstring($result[1]);  }}#You can write a override function in sub classsub convert{  my ($self,$old) = @_;  $old = "CONVERTED STRING:" .$old;  return (1,$old);}#add the dealed string into a array.sub _addstring{  my ($self,$linetext) = @_;  my @a =@{ $self->{_dealedStringArray}};  push(@a, $linetext."\n");  @{$self->{_dealedStringArray}} = @a;}#output converted filesub exportToFile{  my($self,$newFileName) = @_;  open FILEHANDLE ,">$newFileName" || die("can't create file : $newFileName");  for(my $i=0;$i<@{$self->{_dealedStringArray}};$i++)  {    print FILEHANDLE "${$self->{_dealedStringArray}}[$i]";  }  close FILEHANDLE;}1;

Here we will not talk about the concept of Perl object programming in detail. You can refer to it for more information.BackgroundHere, we recommend O 'Reilly-advanced Perl programming. When you look at this instance, you will certainly have some experience.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.