garymgordon
Programmer
Here is the code I am trying to understand.
package Employee;
1;
sub new
{
my $object = {};
return bless $object;
}
If you can, please explain the following:
1) They say a constructor must be named "new" . Can you explain why and how this works. They say that the "new" subroutine defines a hash class object, but I don't really understand what they mean.
Does it have to named "new" ? Or could it be "new" plus another name like:
sub new nameOfSubroutine instead of just sub new
2) What is happening with:
RE: my $object = {};
for example, what is "my" doing?
what is $object ? Can this be named something else ... and what purpose is $object serving?
What is the { } ?? could it be anything else. (Please explain my options at this point.)
RE: return bless $object;
What is "bless" and how is it being used here?
What is being returned from "$object" ?
Where is the "return" being returned to??
One more question at this time.
They also say that the "first element of the array (element 0) contains the name of the class. In this case the string "Employee".
What do they mean by the above. I don't understand the how and why that the first element is the name of the class.
Plus, I don't understand how these two following subroutines are working together. (Maybe they aren't) But, I am just confused.
Your help would be greatly appreciated.
Here is the example they gave.
package Employee;
1;
sub new
{
my $object = {};
$object -> {"name"} = $_[1];
$object -> {"ssn"} = $_[2];
$object -> {"job_classification"} = $_[3];
$object -> {"salary"} = $_[4];
return bless $object;
}
sub calculateBonus
{
my $self = shift;
if ($self -> {"job_classification"} =~ m/Manager/)
{
return ($self -> {"salary"} * 0.10);
}
elsif ($self -> {"job_classification"} =~ m/Peon/)
{
return ($self -> {"salary"} * 0.05);
}
}
Thanks,
Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
package Employee;
1;
sub new
{
my $object = {};
return bless $object;
}
If you can, please explain the following:
1) They say a constructor must be named "new" . Can you explain why and how this works. They say that the "new" subroutine defines a hash class object, but I don't really understand what they mean.
Does it have to named "new" ? Or could it be "new" plus another name like:
sub new nameOfSubroutine instead of just sub new
2) What is happening with:
RE: my $object = {};
for example, what is "my" doing?
what is $object ? Can this be named something else ... and what purpose is $object serving?
What is the { } ?? could it be anything else. (Please explain my options at this point.)
RE: return bless $object;
What is "bless" and how is it being used here?
What is being returned from "$object" ?
Where is the "return" being returned to??
One more question at this time.
They also say that the "first element of the array (element 0) contains the name of the class. In this case the string "Employee".
What do they mean by the above. I don't understand the how and why that the first element is the name of the class.
Plus, I don't understand how these two following subroutines are working together. (Maybe they aren't) But, I am just confused.
Your help would be greatly appreciated.
Here is the example they gave.
package Employee;
1;
sub new
{
my $object = {};
$object -> {"name"} = $_[1];
$object -> {"ssn"} = $_[2];
$object -> {"job_classification"} = $_[3];
$object -> {"salary"} = $_[4];
return bless $object;
}
sub calculateBonus
{
my $self = shift;
if ($self -> {"job_classification"} =~ m/Manager/)
{
return ($self -> {"salary"} * 0.10);
}
elsif ($self -> {"job_classification"} =~ m/Peon/)
{
return ($self -> {"salary"} * 0.05);
}
}
Thanks,
Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer