garymgordon
Programmer
I am seeing a piece of code - in the creation of a small Perl module as follows:
package Circle;
1;
sub circumference
{
$d = shift;
return 3.14 * $d;
}
sub area
$r = shift;
return 3.14 * $r * $r;
}
Then, the perl file is as follows:
#!/usr/bin/perl
require Circle;
print ("Circumference = "
;
print Circle::circumference(10), "\n";
print ("Area = "
;
print Circle::area(5), "\n";
My questions regarding the above package is:
1) What is SHIFT doing in the above situation? It said something in my book such as : Subroutines often start by copying their arguments into lexical variables, and shift can be used for this.
In the circumference example, ... if 10 is being passed into the subroutine 'circumference' ... how is it being assigned to the $d variable?
I don't fully understand what is happening or what they mean.
2) What exactly are LEXICAL Variables ... and LEXICAL Scope???
3) Does every Module need to start out with 1; ? And if not, why not ... and what are my options?
Thanks,
Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
package Circle;
1;
sub circumference
{
$d = shift;
return 3.14 * $d;
}
sub area
$r = shift;
return 3.14 * $r * $r;
}
Then, the perl file is as follows:
#!/usr/bin/perl
require Circle;
print ("Circumference = "
print Circle::circumference(10), "\n";
print ("Area = "
print Circle::area(5), "\n";
My questions regarding the above package is:
1) What is SHIFT doing in the above situation? It said something in my book such as : Subroutines often start by copying their arguments into lexical variables, and shift can be used for this.
In the circumference example, ... if 10 is being passed into the subroutine 'circumference' ... how is it being assigned to the $d variable?
I don't fully understand what is happening or what they mean.
2) What exactly are LEXICAL Variables ... and LEXICAL Scope???
3) Does every Module need to start out with 1; ? And if not, why not ... and what are my options?
Thanks,
Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer