.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 .\" .\" Standard preamble: .\" ======================================================================== .de Sh \" Subsection heading .br .if t .Sp .ne 5 .PP \fB\\$1\fR .PP .. .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. | will give a .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' .\" expand to `' in nroff, nothing in troff, for use with C<>. .tr \(*W-|\(bv\*(Tr .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .\" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .hy 0 .if n .na .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Class::Constant 3" .TH Class::Constant 3 "2008-01-07" "perl v5.8.8" "User Contributed Perl Documentation" .SH "NAME" Class::Constant \- Build constant classes .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Class::Constant NORTH, EAST, SOUTH, WEST; .Ve .PP .Vb 5 \& use Class::Constant \& NORTH => "north", \& EAST => "east", \& SOUTH => "south", \& WEST => "west; .Ve .PP .Vb 5 \& use Class::Constant \& NORTH => { x => 0, y => -1 }, \& EAST => { x => -1, y => 0 }, \& SOUTH => { x => 0, y => 1 }, \& WEST => { x => 1, y => 0 }; .Ve .PP .Vb 9 \& use Class::Constant \& NORTH => "north", \& { x => 0, y => -1 }, \& EAST => "east", \& { x => -1, y => 0 }, \& SOUTH => "south", \& { x => 0, y => 1 }, \& WEST => "west", \& { x => 1, y => 0 }; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Class::Constant allows you declaratively created so-called \*(L"constant classes\*(R". These are very much like enumerated types (as close as a typeless language like Perl can get, at least). .PP The classes generated by this module are modeled closely after Java's \*(L"typesafe enumeration\*(R" pattern, but with some added spice to make them more useful to Perl programs. .Sh "\s-1SIMPLE\s0 \s-1USAGE\s0" .IX Subsection "SIMPLE USAGE" The simplese usage of Class::Constant is to use it to define a set of values for a user-defined \*(L"type\*(R". Consider a class that defines the four main compass points: .PP .Vb 1 \& package Direction; .Ve .PP .Vb 1 \& use Class::Constant NORTH, EAST, SOUTH, WEST; .Ve .PP This generates four constants which can be assigned to some variable: .PP .Vb 1 \& my $facing = Direction::NORTH; .Ve .PP There are two major differences between Class::Constant constants and constants created by the constant pragma: .IP "\(bu" 4 Class::Constant constants have no inherent value, and as such only compare equal to themselves (but see \*(L"\s-1ORDINAL\s0 \s-1VALUES\s0\*(R", eg: .Sp .Vb 3 \& if ($facing == Direction::EAST) { \& print "you are facing east\en"; \& } .Ve .IP "\(bu" 4 Class::Constant constants are actually objects blessed into the package that created them, so they have a \*(L"type\*(R", of sorts: .Sp .Vb 3 \& if ($facing->isa("Direction")) { \& ... \& } .Ve .PP Neither of these distinctions are particularly useful in this simple usage, but are useful when using the more advanced features of this module, described below. .Sh "\s-1CONSTANT\s0 \s-1VALUES\s0" .IX Subsection "CONSTANT VALUES" Althought constants don't have a value as such, real values can be attached to them to be used when appropriate. .PP \fIStringification\fR .IX Subsection "Stringification" .PP Constants can be declared with a string that will be returned when the constant is stringified (eg by \f(CW\*(C`print\*(C'\fR). For example: .PP .Vb 5 \& use Class::Constant \& NORTH => "north", \& EAST => "east", \& SOUTH => "south", \& WEST => "west"; .Ve .PP This makes the following possible: .PP .Vb 1 \& print "you are facing $facing\en"; .Ve .PP \fINamed sub-constants\fR .IX Subsection "Named sub-constants" .PP You can also declare other constant values that are associated with a constant: .PP .Vb 5 \& use Class::Constant \& NORTH => { x => 0, y => -1 }, \& EAST => { x => -1, y => 0 }, \& SOUTH => { x => 0, y => 1 }, \& WEST => { x => 1, y => 0 }; .Ve .PP These sub-constants are accessed via \f(CW\*(C`get_*\*(C'\fR methods called on the constant object: .PP .Vb 1 \& move_player($facing->get_x, $facing->get_y); .Ve .PP \fICombining the two\fR .IX Subsection "Combining the two" .PP Of course both a string value and named sub-constants can be declared at the same time: .PP .Vb 9 \& use Class::Constant \& NORTH => "north", \& { x => 0, y => -1 }, \& EAST => "east", \& { x => -1, y => 0 }, \& SOUTH => "south", \& { x => 0, y => 1 }, \& WEST => "west", \& { x => 1, y => 0 }; .Ve .Sh "\s-1ORDINAL\s0 \s-1VALUES\s0" .IX Subsection "ORDINAL VALUES" Each constant has an internal value which is generated by Class::Constant as it creates the constants. These ordinal values are unique to a package, and are assigned sequentially to each constant create in that package. For example, in our Direction packages, the constants would receive ordinal values as follows: .PP .Vb 4 \& NORTH 0 \& EAST 2 \& SOUTH 1 \& WEST 3 .Ve .PP The ordinal value for a constant can be retrieved by calling the \f(CW\*(C`get_ordinal\*(C'\fR method on a constant object: .PP .Vb 1 \& my $ordinal = Direction::EAST->get_ordinal; .Ve .PP You can also retrieve a constant by its ordinal value using the class method \&\f(CW\*(C`by_ordinal\*(C'\fR .PP .Vb 1 \& my $west = Direction->by_ordinal(3); .Ve .PP These two methods are typically used together to fetch the \*(L"next\*(R" or \*(L"previous\*(R" constant in the sequence, eg: .PP .Vb 4 \& sub turn_left { \& my ($facing) = @_; \& return Direction->by_ordinal(($facing->get_ordinal - 1) % 4); \& } .Ve .Sh "\s-1OVERLOADING\s0" .IX Subsection "OVERLOADING" Constant objects are blessed into the package in which they were declared. The Class::Constant \f(CW\*(C`import\*(C'\fR method also updates the packages' \f(CW@ISA\fR to make constant objects subclass Class::Constant::Object .PP Class::Constant::Object has \f(CW\*(C`as_string\*(C'\fR and \f(CW\*(C`equals\*(C'\fR methods, and also sets up overloading for the \f(CW""\fR (stringification) and \f(CW\*(C`==\*(C'\fR and \f(CW\*(C`!=\*(C'\fR (equality) operators to use these methods. If you override these methods in your package, then Class::Constant::Object will arrange to call your methods instead. .SH "DIAGNOSTICS" .IX Header "DIAGNOSTICS" .ie n .IP """Can't locate constant with ordinal ""%s"" in package ""%s""""" 4 .el .IP "\f(CWCan't locate constant with ordinal ``%s'' in package ``%s''\fR" 4 .IX Item "Can't locate constant with ordinal ""%s"" in package ""%s""" The value passed to \f(CW\*(C`by_ordinal\*(C'\fR does not corespond to any constant in the named package. This usually means the value you've specified is greater than the number of declared constants. .ie n .IP """Can't locate named constant ""%s"" for ""%s""""" 4 .el .IP "\f(CWCan't locate named constant ``%s'' for ``%s''\fR" 4 .IX Item "Can't locate named constant ""%s"" for ""%s""" A named constant associated with a declared constant was not found. It was probably not defined; check your declarations. .SH "AUTHOR" .IX Header "AUTHOR" Robert Norris (rob@cataclysm.cx) .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright (c) 2006 Robert Norris. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.