473,399 Members | 3,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

understand this code..

Hi,

While i was going through this code, I couldn't understand what these pieces of code are going to do ... Can anyone help me understand it ??


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl 
  2.  
  3. package Person;
  4.  
  5. sub new
  6. {
  7.     my $class = shift;
  8.     my $self = {
  9.         _firstName => shift,
  10.         _lastName  => shift,
  11.         _ssn       => shift,
  12.     };
  13.     # Print all the values just for clarification.
  14.     print "First Name is $self->{_firstName}\n";
  15.     print "Last Name is $self->{_lastName}\n";
  16.     print "SSN is $self->{_ssn}\n";
  17.     bless $self, $class;
  18.     return $self;
  19. }
  20. sub setFirstName {
  21.     my ( $self, $firstName ) = @_;
  22.     $self->{_firstName} = $firstName if defined($firstName);
  23.     return $self->{_firstName};
  24. }
  25.  
  26. sub getFirstName {
  27.     my( $self ) = @_;
  28.     return $self->{_firstName};
  29. }
  30. 1;
  31.  
  32.  
I didn't understand why the following codes are there in defined functions... what is it trying to do ?

Expand|Select|Wrap|Line Numbers
  1. my ( $self, $firstName ) = @_;
  2.  
and

Expand|Select|Wrap|Line Numbers
  1.  my( $self ) = @_;
  2.  
Mar 19 '11 #1
3 1738
numberwhun
3,509 Expert Mod 2GB
To find out what the @_ is, you are going to want to read the documentation on Special Variables up on Perldoc.
Mar 19 '11 #2
miller
1,089 Expert 1GB
You're working with a class definition, so all methods accept the object reference as their first parameter.

Just read perldoc perltoot for a introductory tutorial on object oriented programming in perl.

- Miller
Mar 19 '11 #3
rovf
41
The parameter array @_ is an *alias* for the actual parameter list, which means that if you would assign to some element of @_, and the corresponding parameter is an lvalue, this lvalue would be changed.

Except in the rare cases where you want this to happen, good practice is to assign the parameters to local variables, which also has the advantage of having "understandable" names ($self is easier to understand than $_[0]).
Mar 21 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: SAN CAZIANO | last post by:
can you help me please because I can't understand why the code doesn't seems to function very well: in onkeypress it must verify if insert number or string value in the fiels, but it doesn't...
4
by: Leny | last post by:
Hi, I'd like to colaborate with an open source project that have been in development for the last four years. There is almost no documentation at all, and it's extremely hard even to get a...
14
by: Michael McCarthy | last post by:
This code is supposed to create a streamreader object from a stream object and I suppose use encoding if requested (enc isn't null)... The second bit is someone elses code... /// code block #1...
0
by: Namratha Shah \(Nasha\) | last post by:
Hey Guys, Today we are going to look at Code Access Security. Code access security is a feature of .NET that manages code depending on its trust level. If the CLS trusts the code enough to...
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
5
by: Antoine | last post by:
Hi I have a novice question. I am writing some code, and several routines in ..NET. So far very succesfully using several sample to work from, though am I jumping in by doing this and I really need...
10
by: vfunc | last post by:
OK, after reading some C++ books and writing some programs that cover the core, where do I go from there, to say be able to read some open source code ? How do you unravel other peoples code when...
14
by: hsharsha | last post by:
Consider the below code snippet: #include <iostream> using namespace::std; class myclass { public: myclass() { cout << "constructor" << endl;
7
by: David Delony | last post by:
Since the best way to learn how to program well is to read other people's programs, which open source projects written in C stand out as examples of well-written code? -- There's no place...
2
by: Boyyini | last post by:
Hello again. Im making a text-based dungeon-adventure game. And I came across a strange "error" in my code. ">" Means "The stuff on this side is bigger > The stuff on this side is smaller"...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.