home *** CD-ROM | disk | FTP | other *** search
-
- <HTML>
- <HEAD>
- <TITLE>fields - compile-time class fields</TITLE>
- <LINK REL="stylesheet" HREF="../Active.css" TYPE="text/css">
- <LINK REV="made" HREF="mailto:">
- </HEAD>
-
- <BODY>
- <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
- <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
- <STRONG><P CLASS=block> fields - compile-time class fields</P></STRONG>
- </TD></TR>
- </TABLE>
-
- <A NAME="__index__"></A>
- <!-- INDEX BEGIN -->
-
- <UL>
-
- <LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
-
- <LI><A HREF="#synopsis">SYNOPSIS</A></LI>
- <LI><A HREF="#description">DESCRIPTION</A></LI>
- <LI><A HREF="#see also">SEE ALSO</A></LI>
- </UL>
- <!-- INDEX END -->
-
- <HR>
- <P>
- <H1><A NAME="name">NAME</A></H1>
- <P>fields - compile-time class fields</P>
- <P>
- <HR>
- <H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
- <UL>
- <LI>Linux</LI>
- <LI>Solaris</LI>
- <LI>Windows</LI>
- </UL>
- <HR>
- <H1><A NAME="synopsis">SYNOPSIS</A></H1>
- <PRE>
- {
- package Foo;
- use fields qw(foo bar _Foo_private);
- sub new {
- my Foo $self = shift;
- unless (ref $self) {
- $self = fields::new($self);
- $self->{_Foo_private} = "this is Foo's secret";
- }
- $self->{foo} = 10;
- $self->{bar} = 20;
- return $self;
- }
- }</PRE>
- <PRE>
- my Foo $var = Foo::->new;
- $var->{foo} = 42;</PRE>
- <PRE>
- # this will generate a compile-time error
- $var->{zap} = 42;</PRE>
- <PRE>
- # subclassing
- {
- package Bar;
- use base 'Foo';
- use fields qw(baz _Bar_private); # not shared with Foo
- sub new {
- my $class = shift;
- my $self = fields::new($class);
- $self->SUPER::new(); # init base fields
- $self->{baz} = 10; # init own fields
- $self->{_Bar_private} = "this is Bar's secret";
- return $self;
- }
- }</PRE>
- <P>
- <HR>
- <H1><A NAME="description">DESCRIPTION</A></H1>
- <P>The <CODE>fields</CODE> pragma enables compile-time verified class fields.</P>
- <P>NOTE: The current implementation keeps the declared fields in the %FIELDS
- hash of the calling package, but this may change in future versions.
- Do <STRONG>not</STRONG> update the %FIELDS hash directly, because it must be created
- at compile-time for it to be fully useful, as is done by this pragma.</P>
- <P>If a typed lexical variable holding a reference is used to access a
- hash element and a package with the same name as the type has declared
- class fields using this pragma, then the operation is turned into an
- array access at compile time.</P>
- <P>The related <CODE>base</CODE> pragma will combine fields from base classes and any
- fields declared using the <CODE>fields</CODE> pragma. This enables field
- inheritance to work properly.</P>
- <P>Field names that start with an underscore character are made private to
- the class and are not visible to subclasses. Inherited fields can be
- overridden but will generate a warning if used together with the <CODE>-w</CODE>
- switch.</P>
- <P>The effect of all this is that you can have objects with named fields
- which are as compact and as fast arrays to access. This only works
- as long as the objects are accessed through properly typed variables.
- If the objects are not typed, access is only checked at run time.</P>
- <P>The following functions are supported:</P>
- <DL>
- <DT><STRONG><A NAME="item_new">new</A></STRONG><BR>
- <DD>
- fields::new() creates and blesses a pseudo-hash comprised of the fields
- declared using the <CODE>fields</CODE> pragma into the specified class.
- This makes it possible to write a constructor like this:
- <PRE>
- package Critter::Sounds;
- use fields qw(cat dog bird);</PRE>
- <PRE>
- sub new {
- my Critter::Sounds $self = shift;
- $self = fields::new($self) unless ref $self;
- $self->{cat} = 'meow'; # scalar element
- @$self{'dog','bird'} = ('bark','tweet'); # slice
- return $self;
- }</PRE>
- <P></P>
- <DT><STRONG><A NAME="item_phash">phash</A></STRONG><BR>
- <DD>
- fields::phash() can be used to create and initialize a plain (unblessed)
- pseudo-hash. This function should always be used instead of creating
- pseudo-hashes directly.
- <P>If the first argument is a reference to an array, the pseudo-hash will
- be created with keys from that array. If a second argument is supplied,
- it must also be a reference to an array whose elements will be used as
- the values. If the second array contains less elements than the first,
- the trailing elements of the pseudo-hash will not be initialized.
- This makes it particularly useful for creating a pseudo-hash from
- subroutine arguments:</P>
- <PRE>
- sub dogtag {
- my $tag = fields::phash([qw(name rank ser_num)], [@_]);
- }</PRE>
- <P>fields::phash() also accepts a list of key-value pairs that will
- be used to construct the pseudo hash. Examples:</P>
- <PRE>
- my $tag = fields::phash(name => "Joe",
- rank => "captain",
- ser_num => 42);</PRE>
- <PRE>
- my $pseudohash = fields::phash(%args);</PRE>
- <P></P></DL>
- <P>
- <HR>
- <H1><A NAME="see also">SEE ALSO</A></H1>
- <P><A HREF="../lib/base.html">the base manpage</A>,
- <A HREF="../lib/Pod/perlref.html#pseudohashes: using an array as a hash">Pseudo-hashes: Using an array as a hash in the perlref manpage</A></P>
- <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
- <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
- <STRONG><P CLASS=block> fields - compile-time class fields</P></STRONG>
- </TD></TR>
- </TABLE>
-
- </BODY>
-
- </HTML>
-