perl 5.0 alpha 8
[p5sagit/p5-mst-13.2.git] / Quick
1 #!/usr/local/bin/perl5
2 #
3 # This document is in the public domain.
4 #
5 # The purpose is to document by example some of the new Perl5 features.
6 # It also functions as a mini test suite; you can extracted the
7 # expected output using:
8 #     perl -ne 'm/.*prints ``(.*)..$/ && print $1,"\n";'
9 # There are a couple of places that print out internal address so it's
10 # not perfect yet, those should be fixed.
11 #
12 # Thanks to the following for their input:
13 #     Johan.Vromans@NL.net
14 #     Daniel Faken <absinthe@viva.chem.washington.edu>
15 #     Tom Christiansen <tchrist@wraeththu.cs.colorado.edu>
16 #     Dean Roehrich <roehrich@ferrari.cray.com>
17 #     Larry Wall <lwall@netlabs.com>
18 #
19 # TODO when I get perl5a6 to play with
20 #       *foo = \&func;                  # replaces only function (etc)
21 #       AUTOLOAD { ...; }               # called if method not found
22 #       goto &func;                     # goto's a function
23 #       require FOOBAR;                 # loads FOOBAR.pm
24 #       @ISA
25 #
26 #       import()/@EXPORT/etc
27
28 #   my
29         # static scoping
30         sub samp1 { print $z,"\n"; }
31         sub samp2 { my($z) = "world"; &samp1; }
32         $z = "hello"; &samp2;           # prints ``hello''
33
34 #   package;
35         # for catching non-local variable references
36         sub samp3 {
37             my $x = shift;              # local() would work also
38             package;                    # empty package
39             $main::count += $x;         # this is ok.
40             # $y = 1;                   # compile time error
41         }
42
43 #   =>
44         # works like comma (,); use for key/value pairs
45         # sometimes used to disambiguate the final expression in a block
46         # might someday supply warnings if you get out of sync
47         %foo = ( abc => foo );
48         print $foo{abc},"\n";           # prints ``foo''
49
50 #   ::
51         # works like tick (') (use of ' is deprecated in perl5)
52         print $main::foo{abc},"\n";     # prints ``foo''
53
54 #   bless ref;
55         # Bless takes a reference and returns an "object"
56         $oref = bless \$scalar;
57
58 #   ->
59         # dereferences an "object"
60         $x = { def => bar };            # $x is ref to anonymous hash
61         print $x->{def},"\n";           # prints ``bar''
62
63         # method derefs must be bless'ed
64         {
65             package sample;
66             sub samp4 { my($this) = shift; print $this->{def},"\n"; }
67             sub samp5 { print "samp5: @_\n"; }
68             $main::y = bless $main::x;  # $x is ref, $y is "object"
69         }
70         $y->samp4();                    # prints ``bar''
71
72         # indirect object calls
73         samp5 $y arglist;               # prints ``samp5: sample=HASH(0xa85e0) arglist''
74
75         # static method calls (often used for constructors, see below)
76         samp5 sample arglist;           # prints ``samp5: sample arglist''
77
78 #   function calls without &
79         sub samp6 { print "look ma\n"; }
80         samp6;                          # prints ``look ma''
81
82 #   ref
83         # returns "object" type
84         {
85             package OBJ1;
86             $x = bless \$y;             # returns "object" $x in "class" OBJ1
87             print ref $x,"\n";          # prints ``OBJ1''
88         }
89
90         # and non-references return undef.
91         $z = 1;
92         print "non-ref\n" if !defined(ref $z);          # prints ``non-ref''
93
94         # ref's to "builtins" return type
95         print ref \$ascalar,"\n";               # prints ``SCALAR''
96         print ref \@array,"\n";                 # prints ``ARRAY''
97         print ref \%hash,"\n";                  # prints ``HASH''
98         sub func { print shift,"\n"; }
99         print ref \&func,"\n";                  # prints ``CODE''
100         print ref \\$scalar,"\n";               # prints ``REF''
101
102 #   tie
103         # bind a variable to a package with magic functions:
104         #     new, fetch, store, delete, firstkey, nextkey (XXX: others???)
105         # Usage: tie variable, PackageName, ARGLIST
106         {
107             package TIEPACK;
108             sub new { print "NEW: @_\n"; my($class, $x) = @_; bless \$x }
109             sub fetch { print "fetch @_\n"; my($this) = @_; ${$this} }
110             sub store { print "store @_\n"; my($this, $x) = @_; ${$this} = $x }
111             sub DESTROY { print "DESTROY @_\n" }
112         }
113         tie $h, TIEPACK, "black_tie";   # prints ``NEW: TIEPACK black_tie''
114         print $h, "\n";                 # prints ``fetch TIEPACK=SCALAR(0x882a0)''
115                                         # prints ``black_tie''
116         $h = 'bar';                     # prints ``store TIEPACK=SCALAR(0x882a0) bar''
117         untie $h;                       # DESTROY (XXX: broken in perl5a5???)
118
119 #   References and Anonymous data-structures
120         $sref = \$scalar;               # $$sref is scalar
121         $aref = \@array;                # @$aref is array
122         $href = \%hash;                 # %$href is hash table
123         $fref = \&func;                 # &$fref is function
124         $refref = \$fref;               # ref to ref to function
125         &$$refref("call the function"); # prints ``call the function''
126
127         %hash = ( abc => foo );         # hash (just like perl4)
128         print $hash{abc},"\n";          # prints ``foo''
129         $ref = { abc => bar };          # reference to anon hash
130         print $ref->{abc},"\n";         # prints ``bar''
131
132         @ary = ( 0, 1, 2 );             # array (just like perl4)
133         print $ary[1],"\n";             # prints ``1''
134         $ref = [ 3, 4, 5 ];             # reference to anon array
135         print $ref->[1],"\n";           # prints ``4''
136
137 #   Nested data-structures
138         @foo = ( 0, { name => foobar }, 2, 3 );         # $#foo == 3
139         $aref = [ 0, { name => foobar }, 2, 3 ];        # ref to anon array
140         $href = {                                       # ref to hash of arrays
141             John => [ Mary, Pat, Blanch ],
142             Paul => [ Sally, Jill, Jane ],
143             Mark => [ Ann, Bob, Dawn ],
144         };
145         print $href->{Paul}->[0], "\n";                 # prints ``Sally''
146         print $href->{Paul}[0],"\n";                    # shorthand version, prints ``Sally''
147
148 #   Multiple Inheritence (get rich quick :-)
149         {
150             package OBJ2; sub abc { print "abc\n"; }
151             package OBJ3; sub def { print "def\n"; }
152             package OBJ4; @ISA = ("OBJ2", "OBJ3");
153             $x = bless { foo => bar };
154             $x->abc;                                    # prints ``abc''
155             $x->def;                                    # prints ``def''
156         }
157
158 #   Packages, Classes, Objects, Methods, Constructors, Destructors, etc.
159         # XXX: I'll add more explinations/samples about the above here
160         {
161             package OBJ5;
162             sub new { print "NEW: @_\n"; my($x) = "empty"; bless \$x }
163             sub DESTROY { print "DESTROY\n" }
164             sub output { my($this) = shift; print "value = $$this\n"; }
165         }
166         # Constructors are often written as static method calls:
167         $x = new OBJ5;          # prints ``NEW: OBJ5''
168         $x->output;             # prints ``value = empty''
169         # The destructor is responsible for calling any base class destructors.
170         undef $x;