Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens on this my declaration?

Tags:

oop

perl

I know the title sounds funny, but I found this snippet somewhere:

my MyPackage $p1 = MyPackage->new;

What role does the name of the package serve in front of $p1?

EDIT: I'm running perl 5.10.1.

like image 662
Geo Avatar asked Dec 04 '09 21:12

Geo


People also ask

What does declaration Form mean?

A declaration form is a document that outlines all the information that is relevant and obtainable in a particular situation. In a declaration form, the person filling the form is expected to provide truthful and accurate information as is required.

How is a declaration written?

declaration should date and sign at the signature line and write the place where s/he signed the statement. Declarations do not have to be notarized. The witness is swearing the statements are true under the penalty of perjury.

How do you run a statutory declaration?

Statutory declarations and affidavits are made by a deponent in the physical presence of a commissioner for oaths or a notary public. Affidavits are also sworn or affirmed by deponents, inter alia, as a way of presenting evidence to a court.


2 Answers

It checks for a package with the same name, and, as of now, is tied to the fields pragma which helps check for typos in field names.

For example:

package MyPackage;
use fields qw/ foo bar /;
sub new { fields::new(shift) }

Then if you try to run

use MyPackage;
my MyPackage $p1 = MyPackage->new;
print $p1->{notpresent}, "\n";

you get

No such class field "notpresent" in variable $p1 of type MyPackage at ...
like image 126
Greg Bacon Avatar answered Sep 25 '22 02:09

Greg Bacon


From http://perldoc.perl.org/functions/my.html:

my TYPE EXPR : ATTRS

A my declares the listed variables to be local (lexically) to the enclosing block, file, or eval. If more than one value is listed, the list must be placed in parentheses.

The exact semantics and interface of TYPE and ATTRS are still evolving. TYPE is currently bound to the use of fields pragma, and attributes are handled using the attributes pragma, or starting from Perl 5.8.0 also via the Attribute::Handlers module.

like image 37
Ivan Nevostruev Avatar answered Sep 27 '22 02:09

Ivan Nevostruev