An example using the enum type
enum type is much more complex in Perl 6 than in some other languages, and the details are found in its type description.
This short document will give a simple example of its use as is the usual practice in C-like languages.
Say we have a program that needs to write to various directories; we want a function that, given a directory name, tests it for (1) its existence and (2) whether it can be written to by the user of the program; this implies that there are three possible states from the user perspective: either you can write (
CanWrite), or there is no directory (
NoDir) or the directory exists, but you cannot write (
NoWrite). The results of the test will determine what actions the program takes next.
<CanWrite NoDir NoWrite>;sub check-dir-status( --> DirStat)# test each of three directories by a non-root usermy ='/tmp', # normally writable by any user'/', # writable only by root'~/tmp'; # a non-existent dir in the user's home dirfor -># output# status of dir '/tmp': CanWrite# user can write to dir: /tmp# status of dir '/': NoWrite# status of dir '~/tmp': NoDir