http://www.perlmonks.org?node_id=456696

These examples were taken from the Pugs (t and examples ) directories and I thought It would be useful as a quick reference guide . These examples are not exhaustive or complete but should help as a reference

Arrays

#!/usr/bin/pugs use v6; my @array = (); say @array.end; # prints -1 (empty array) @array = (1..43); # array from 1 to 42 . say " now array size is " ~ @array.end; my $get_last_element = pop @array; say "removed last element $get_last_element "; say " now array size is " ~ @array.end; say "getting first element " ~ shift @array; say " now array size is " ~ @array.end; unshift @array, 'foo';#adding first element say @array[0]; push @array, 'bar';#adding bar to last element say @array[@array.end]; my @arr = <1 2 3 4>; # say "another way of getting the size " ~ +@arr;#getting the si +ze splice @arr; say " size after splice " ~ +@arr;#getting the size my @a = <red, green, blue>; say " splice() in scalar context returns last element of array + " ~ splice(@a, 1, 2); @a = (2..10); splice(@a,0,0,0,1);## adding 0 an 1 to the beginning splice(@a,5,1,'x');## Replacing the 5th element with x say @a; splice(@a, @a, 0, 12, 13);##append 12 and 13 to the array say @a; say @a.exists(0); ## exists(positive index) on arrays (0) say @a.keys;# without spaces say ~@a.keys; # print with spaces ~ say ~@a.values; @array = <a b c d>; my @kv = @array.kv; say @kv;# prints 0a1b2c3d @array = <d c b a>; say @array.sort;# prints abcd @array = <a b c d>; my @pairs = @array.pairs; say @pairs;#prints 0 a1 b2 c3 d say @pairs[0].key;# print 0 say @pairs[0].value;# print a @array.delete(2);#deletes c @array = <a b c d>; @array.delete(0, 3);# deletes 'a' and 'd' say @array; @array = <a b c d>; @array.delete(-2);#deletes c say @array; @array = <a b c d e f>; @array.delete(2, -3, -1);# deletes "c d f" say @array; ## Grep my @list = (1 .. 10); my @result = grep { ($_ % 2) }, @list;## grep say @result;# prints 1 3 5 7 9 @result = @list.grep():{ ($_ % 2) };## another way to do it ## Map @result = map { $_ * 2 } @list; @result = @list.map():{ $_ * 2 };## another way to do it say @result;# @array = <a b b c d e b b b b f b>; # @array.uniq @array.max @array.min; to do in pugs


Hashes


#!/usr/bin/pugs use v6; my %hash = (a => 1, b => 2, c => 3, d => 4); say %hash;# prints a 1b 2c 3d 4 say ~%hash.keys.sort;#print a b c d say sort(keys(%hash)); #p5 style say ~%hash.values.sort;#print 1 2 3 4 say sort(values(%hash)); #p5 style say "num of elements in hash is " ~ +%hash;# prints 4 say %hash.pick.key;# pick any key say %hash.pick.value;#pick any value # print all key and value for (%hash.kv) -> ($key,$value) { say "hash key is $key val is $value "; } my @pairs; @pairs = %hash.pairs.sort; say @pairs;#prints a 1b 2c 3d 4 say @pairs[2].key;# prints c say @pairs[2].value;# prints 3 %hash.delete("a");# delete key a say %hash.exists("b");#prints 1 %hash.exists("x");


Functions


#!/usr/bin/pugs use v6; ############################################################ ###Perl5ish subroutine declarations#### tmp(3,'asd',4); moditest(100); sub tmp { say ~@_; } ## @_ is rw to do in pugs sub moditest (*@_ is rw) { say "val is @_[0]"; #@_[0] = 99 ; #say "modified val is @_[0]"; } ############################################################ ### Multi Sub### foo ('string_passed'); foo (4); foo($*ERR); # type based dispatching multi sub foo (Int $bar) { say "Int " ~ $bar } multi sub foo (Str $bar) { say "Str " ~ $bar } multi sub foo (IO $fh) {say "IO" } ############################################################ ###Pointy Sub### my ($sub, $got); $got = ''; $sub = -> $x { $got = "x $x" }; say $sub.(123); $sub = -> ($x) { $got = "x $x" }.(456).say; my $str = ''; ### Output of the below ##start outer ## before ## pointy still sees outer's &outer ##calling outerinner ret ##str is inner ## End Output say " calling outer" ~ outer(); say "str is " ~ $str; sub outer { say "start outer"; my $s = -> $x { say " pointy still sees outer\'s $?SUBNAME"; $str ~= 'inner'; return 'inner ret'; }; say " before "; $s.(); $str ~= 'outer'; say " In Outer $?SUBNAME"; return 'outer ret'; } ############################################################ ###Optional Parameters ### ## Optional positional parameters are specified after all the ## required parameters and each is marked with a ? before the paramete +r my_substr('Kiran'); my_substr("foobar",len=>3); sub my_substr ($str, ?$from = 100, ?$len = Inf) { say "str is $str from is $from len is $len"; } say "calling " ~ xml_tag('sometag'); sub xml_tag ($tag, ?$endtag = matching_tag($tag) ) { say "tag is $tag endtag is $endtag"; } sub matching_tag ($tmp) { return "GOtta $tmp"; } ############################################################ ###Named Parameters ### #Named parameters follow any required or optional parameters in the si +gnature. # They are marked by a + before the parameter. my ($text,$case,$justify) = formalize('title', case=>'upper'); say "text is $text case is $case justify is $justify "; ($text,$case,$justify) = formalize('head', justify=>'right',case=>'lo +wer'); say "text is $text case is $case justify is $justify "; ($text,$case,$justify) = formalize('head', :justify<middle>,:case<any +case>);# another way say "text is $text case is $case justify is $justify "; sub formalize($text, +$case, +$justify) returns List { return($text,$case,$justify); } ############################################################ ###Slurrrp ### #Slurpy parameters follow any required or optional parameters. #They are marked by a * before the parameter: my @arr = 1..3; # array stores three scalars #bar(@arr); # error: only one arg bar(@arr); # ok sub bar(*$x, *$y, *$z) { say "x $x"; say "y $y "; say "z $z"; }


Update: added links as per dragonchild suggestion