- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Defining Class Methods in Perl
Other object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and they provide accessor methods to modify object data. Perl does not have private variables but we can still use the concept of helper methods to manipulate object data.
Lets define a helper method to get person’s first name −
sub getFirstName { return $self->{_firstName}; }
Another helper function to set person’s first name −
sub setFirstName { my ( $self, $firstName ) = @_; $self->{_firstName} = $firstName if defined($firstName); return $self->{_firstName}; }
Now lets have a look into complete example: Keep Person package and helper functions into Person.pm file.
#!/usr/bin/perl package Person; sub new { my $class = shift; my $self = { _firstName => shift, _lastName => shift, _ssn => shift, }; # Print all the values just for clarification. print "First Name is $self->{_firstName}\n"; print "Last Name is $self->{_lastName}\n"; print "SSN is $self->{_ssn}\n"; bless $self, $class; return $self; } sub setFirstName { my ( $self, $firstName ) = @_; $self->{_firstName} = $firstName if defined($firstName); return $self->{_firstName}; } sub getFirstName { my( $self ) = @_; return $self->{_firstName}; } 1;
Now let's make use of Person object in employee.pl file as follows −
#!/usr/bin/perl use Person; $object = new Person( "Mohammad", "Saleem", 23234345); # Get first name which is set using constructor. $firstName = $object->getFirstName(); print "Before Setting First Name is : $firstName\n"; # Now Set first name using helper function. $object->setFirstName( "Mohd." ); # Now get first name set by helper function. $firstName = $object->getFirstName(); print "Before Setting First Name is : $firstName\n";
When we execute above program, it produces the following result −
First Name is Mohammad Last Name is Saleem SSN is 23234345 Before Setting First Name is : Mohammad Before Setting First Name is : Mohd.
- Related Articles
- Using GET Methods in Perl
- Using POST Methods in Perl
- Defining generic method inside Non-generic class in Java
- What are Object and Class in Perl?
- BitSet class methods in Java
- Math class methods in C#
- Range class methods in Ruby
- Explain Class Methods in Coffeescript
- Math class methods in Java Programming
- Methods of StringBuffer class in Java.
- Methods of StringBuilder class in Java.
- Methods of StringTokenizer class in Java.
- Useful Methods in Float Class in Ruby
- What is Regex class and its class methods in C#?
- Methods of the Thread Class

Advertisements