Permalink
Browse files

A few more cookbook working files

  • Loading branch information...
1 parent c4c6c45 commit 4e55ed5bd3c808509cde91f1863c6931f2541c31 @scottp scottp committed Jul 4, 2009
@@ -0,0 +1,19 @@
+use v6;
+
+=begin pod
+=head1 Specifying a list in your program
+
+You want to include a list in your program. This is how to initialize arrays.
+
+=end pod
+
+# comma separated list of elements
+my @a = ('alpha', 'beta', 'gamma');
+say @a[1];
+
+# angle brackes to autoquote items
+my @a = <alpha beta gamma>;
+
+for @a -> $e {
+ say $e;
+}
@@ -0,0 +1,25 @@
+use v6;
+
+=begin pod
+=head1 Printing a list with commas
+
+You'd like to print out a list containing an unknown number of elements,
+placing an "and" before the last element and commas between each element if
+there are more than two.
+
+=end pod
+
+my @a = <alpha beta gamma>;
+
+say commify_series(['foo']);
+say commify_series(<this that>);
+say commify_series(@a);
+
+sub commify_series(@list) {
+ given @list.elems {
+ when 0 { return '' };
+ when 1 { return @list[0] };
+ when 2 { return join " and ", @list };
+ default { return join(", ", @list[0 .. $_ -2]) ~ " and @list[*-1]" };
+ };
+}
@@ -0,0 +1,17 @@
+use v6;
+
+=begin pod
+=head1 Iterating Over an Array
+
+You want to iterate over the elements in an array
+
+=end pod
+
+
+my @a = <94 13 97 95 12 13 74 10 47 4 62 47 75 36 25 35 0 71 56 50 72 39 30 93>;
+
+for @a -> $e {
+ say $e;
+}
+
+say $_ for @a;
@@ -0,0 +1,26 @@
+use v6;
+
+=begin pod
+=head1 Traversing a hash
+
+You want to perform an action on each entry (i.e., each pair) in a hash.
+
+=end pod
+
+my %hash = (
+ 'one' => 'un',
+ 'two' => 'deux',
+ 'three' => 'trois'
+);
+
+for %hash.kv -> $key, $value {
+ say "The word '$key' is '$value' in French.";
+}
+
+for %hash.keys -> $key {
+ say "$key => %hash{$key}";
+}
+
+for %hash {
+ say $_; # hmm ...
+}
@@ -0,0 +1,38 @@
+use v6;
+
+=begin pod
+=head1 opening a file
+
+You want to read or write a file from Perl.
+
+=end pod
+
+my $path = "07-01opening_file.pl";
+
+my $input = open($path, :r)
+ or die "Could not open $path for reading $!\n";
+
+my $filename = "test_file";
+my $output = open($filename, :w)
+ or die "Could not open $filename for writing $!\n";
+
+
+# File-access modes
+# If you want to use any of the mode parameters you have to put parenthese () after the open.
+# :r read, this is the defaul open mode
+# :w write, automatically creating non existing files and emptying existing files
+# :a append, keep the file intact and enable to write at the end of it
+# :rw read and write
+# :r, :w
+# :ra read and append
+# :r, :a
+# write and read (overwriting a a file) will we have this?
+
+# Closing the file
+# $input.close orelse die $!;
+# close($input);
+
+$input.close or die $!;
+close($output);
+
+say "ok last";
@@ -0,0 +1,19 @@
+use v6;
+
+=begin pod
+=head1 Constructing an object
+
+=head2 Problem
+
+You want to create a way for your users to generate new objects
+
+=head2 Solution
+
+Merely declare the class. Constructors are provided for you automatically.
+
+=end pod
+
+class Foo {}
+
+my $foo = Foo.new;
+say $foo.isa('Foo') ?? "Yes" !! "No";

0 comments on commit 4e55ed5

Please sign in to comment.