├── base64.txt ├── LICENSE ├── example.txt ├── t └── syntax.t └── README.md /base64.txt: -------------------------------------------------------------------------------- 1 | TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdCwg 2 | c2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0IGRvbG9yZSBtYWdu 3 | YSBhbGlxdWEuIFV0IGVuaW0gYWQgbWluaW0gdmVuaWFtLCBxdWlzIG5vc3RydWQgZXhlcmNpdGF0 4 | aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0IGFsaXF1aXAgZXggZWEgY29tbW9kbyBjb25zZXF1 5 | YXQuIER1aXMgYXV0ZSBpcnVyZSBkb2xvciBpbiByZXByZWhlbmRlcml0IGluIHZvbHVwdGF0ZSB2 6 | ZWxpdCBlc3NlIGNpbGx1bSBkb2xvcmUgZXUgZnVnaWF0IG51bGxhIHBhcmlhdHVyLiBFeGNlcHRl 7 | dXIgc2ludCBvY2NhZWNhdCBjdXBpZGF0YXQgbm9uIHByb2lkZW50LCBzdW50IGluIGN1bHBhIHF1 8 | aSBvZmZpY2lhIGRlc2VydW50IG1vbGxpdCBhbmltIGlkIGVzdCBsYWJvcnVtLg== -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This software is Copyright (c) 2014 by David Farrell. 2 | 3 | This is free software, licensed under: 4 | 5 | The (two-clause) FreeBSD License 6 | 7 | The FreeBSD License 8 | 9 | Redistribution and use in source and binary forms, with or without 10 | modification, are permitted provided that the following conditions are 11 | met: 12 | 13 | 1. Redistributions of source code must retain the above copyright 14 | notice, this list of conditions and the following disclaimer. 15 | 16 | 2. Redistributions in binary form must reproduce the above copyright 17 | notice, this list of conditions and the following disclaimer in the 18 | documentation and/or other materials provided with the 19 | distribution. 20 | 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | -------------------------------------------------------------------------------- /example.txt: -------------------------------------------------------------------------------- 1 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 2 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 3 | 4 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 5 | 6 | 7 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. -------------------------------------------------------------------------------- /t/syntax.t: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl6 2 | use Test; 3 | plan 140; 4 | is qq:x{perl6 -ce 'say "Hello, World!"'}, "Syntax OK\n"; 5 | is q:x{perl6 -ce '$_ ~= "\n"' example.txt}, "Syntax OK\n"; 6 | is q:x{perl6 -ce '$_ ~= "\n" x 4' example.txt}, "Syntax OK\n"; 7 | is q:x{perl6 -ce 'say ""' example.txt}, "Syntax OK\n"; 8 | is q:x{perl6 -ce '.say if /\S/' example.txt}, "Syntax OK\n"; 9 | is q:x{perl6 -ce '.say if .chars' example.txt}, "Syntax OK\n"; 10 | is q:x{perl6 -ce '$*ARGFILES.slurp.subst(/\n+/, "\n\n", :g).say' example.txt}, "Syntax OK\n"; 11 | is q:x{perl6 -ce 'say "{++$} $_"' example.txt}, "Syntax OK\n"; 12 | is q:x{perl6 -ce 'say $*ARGFILES.ins ~ " $_"' example.txt}, "Syntax OK\n"; 13 | is q:x{perl6 -ce '$_ = "{++$} $_" if /\S/' example.txt}, "Syntax OK\n"; 14 | is q:x{perl6 -ce '$_ = $*ARGFILES.ins ~ " $_" if /\S/' example.txt}, "Syntax OK\n"; 15 | is q:x{perl6 -ce 'say lines.elems' example.txt}, "Syntax OK\n"; 16 | is q:x{perl6 -ce 'say lines.Int' example.txt}, "Syntax OK\n"; 17 | is q:x{perl6 -ce 'lines.Int.say' example.txt}, "Syntax OK\n"; 18 | is q:x{perl6 -ce 'lines.grep(/\S/).elems.say' example.txt}, "Syntax OK\n"; 19 | is q:x{perl6 -ce 'lines.grep(/^\s*$/).elems.say' example.txt}, "Syntax OK\n"; 20 | is q:x{perl6 -ce 'say "7 is prime" if 7.Int.is-prime'}, "Syntax OK\n"; 21 | is q:x{perl6 -ce 'say [+] .split("\t")'}, "Syntax OK\n"; 22 | is q:x{perl6 -ce 'say [+] lines.split("\t")'}, "Syntax OK\n"; 23 | is q:x{perl6 -ce '.split("\t").pick(*).join("\t").say'}, "Syntax OK\n"; 24 | is q:x{perl6 -ce '.split("\t").min.say'}, "Syntax OK\n"; 25 | is q:x{perl6 -ce 'lines.split("\t").min.say'}, "Syntax OK\n"; 26 | is q:x{perl6 -ce '.split("\t").max.say'}, "Syntax OK\n"; 27 | is q:x{perl6 -ce 'lines.split("\t").max.say'}, "Syntax OK\n"; 28 | is q:x{perl6 -ce '.split("\t").map(*.abs).join("\t").say'}, "Syntax OK\n"; 29 | is q:x{perl6 -ce '.chars.say' example.txt}, "Syntax OK\n"; 30 | is q:x{perl6 -ce '.words.elems.say' example.txt}, "Syntax OK\n"; 31 | is q:x{perl6 -ce '.split(",").elems.say' example.txt}, "Syntax OK\n"; 32 | is q:x{perl6 -ce 'say lines.split("\t").elems'}, "Syntax OK\n"; 33 | is q:x{perl6 -ce 'say lines.words.elems' example.txt}, "Syntax OK\n"; 34 | is q:x{perl6 -ce 'say lines.split("\t").comb(/pattern/).elems'}, "Syntax OK\n"; 35 | is q:x{perl6 -ce 'say lines.words.comb(/pattern/).elems'}, "Syntax OK\n"; 36 | is q:x{perl6 -ce 'say lines.grep(/in/).elems'}, "Syntax OK\n"; 37 | is q:x{perl6 -ce 'say pi.fmt("%.10f")'}, "Syntax OK\n"; 38 | is q:x{perl6 -ce 'say π'}, "Syntax OK\n"; 39 | is q:x{perl6 -ce 'say e.fmt("%.10f")'}, "Syntax OK\n"; 40 | is q:x{perl6 -ce 'say e'}, "Syntax OK\n"; 41 | is q:x{perl6 -ce 'say time'}, "Syntax OK\n"; 42 | is q:x{perl6 -MDateTime::TimeZone -ce 'say to-timezone("GMT",DateTime.now)'}, "Syntax OK\n"; 43 | is q:x{perl6 -ce 'say DateTime.now'}, "Syntax OK\n"; 44 | is q:x{perl6 -ce 'say DateTime.now.map({$_.hour, $_.minute, $_.second.round}).join(":")'}, "Syntax OK\n"; 45 | is q:x{perl6 -ce 'say DateTime.now.earlier(:1day)'}, "Syntax OK\n"; 46 | is q:x{perl6 -ce 'say DateTime.now.earlier(:14months).earlier(:9days).earlier(:7seconds)'}, "Syntax OK\n"; 47 | is q:x{perl6 -ce 'say [*] 1..5'}, "Syntax OK\n"; 48 | is q:x{perl6 -ce 'say [gcd] @list_of_numbers'}, "Syntax OK\n"; 49 | is q:x{perl6 -ce 'say (20, 35, *%* ... 0)[*-2]'}, "Syntax OK\n"; 50 | is q:x{perl6 -ce 'say 20 lcm 35'}, "Syntax OK\n"; 51 | is q:x{perl6 -ce 'say 20 * 35 / (20 gcd 35)'}, "Syntax OK\n"; 52 | is q:x{perl6 -ce '.say for (5..^15).roll(10)'}, "Syntax OK\n"; 53 | is q:x{perl6 -ce 'say .join for [1..5].permutations'}, "Syntax OK\n"; 54 | is q:x{perl6 -ce '.say for <1 2 3>.combinations'}, "Syntax OK\n"; 55 | is q:x{perl6 -ce 'say :256["127.0.0.1".comb(/\d+/)]'}, "Syntax OK\n"; 56 | is q:x{perl6 -ce 'say +":256[{q/127.0.0.1/.subst(:g,/\./,q/,/)}]"'}, "Syntax OK\n"; 57 | is q:x{perl6 -ce 'say Buf.new(+«"127.0.0.1".split(".")).unpack("N")'}, "Syntax OK\n"; 58 | is q:x{perl6 -ce 'say join ".", @(pack "N", 2130706433)'}, "Syntax OK\n"; 59 | is q:x{perl6 -ce 'say join ".", map { ((2130706433+>(8*$_))+&0xFF) }, (3...0)'}, "Syntax OK\n"; 60 | is q:x{perl6 -ce '.say for "a".."z"'}, "Syntax OK\n"; 61 | is q:x{perl6 -ce '.say for "a".."zz"'}, "Syntax OK\n"; 62 | is q:x{perl6 -ce 'say 255.base(16)'}, "Syntax OK\n"; 63 | is q:x{perl6 -ce 'say sprintf("%x", 255)'}, "Syntax OK\n"; 64 | is q:x{perl6 -ce 'say sprintf("%3i => %2x", $_, $_) for 0..255'}, "Syntax OK\n"; 65 | is q:x{perl6 -ce 'say sprintf("%%%x", 255)'}, "Syntax OK\n"; 66 | is q:x{perl6 -ce 'print roll 10, "a".."z"'}, "Syntax OK\n"; 67 | is q:x{perl6 -ce 'print roll "a".."z": 10'}, "Syntax OK\n"; 68 | is q:x{perl6 -ce 'print roll 15, "0".."z"'}, "Syntax OK\n"; 69 | is q:x{perl6 -ce 'print roll "0".."z": 15'}, "Syntax OK\n"; 70 | is q:x{perl6 -ce 'print "a" x 50'}, "Syntax OK\n"; 71 | is q:x{perl6 -ce '(1..100).grep(* %% 2).say'}, "Syntax OK\n"; 72 | is q:x{perl6 -ce '"storm in a teacup".chars.say'}, "Syntax OK\n"; 73 | is q:x{perl6 -ce 'my @letters = "a".."z"; @letters.Int.say'}, "Syntax OK\n"; 74 | is q:x{perl6 -ce 'tr/A..Za..z/N..ZA..Mn..za..m/' example.txt}, "Syntax OK\n"; 75 | is q:x{perl6 -MMIME::Base64 -ce 'print MIME::Base64.encode-str($_)' example.txt}, "Syntax OK\n"; 76 | is q:x{perl6 -MMIME::Base64 -ce 'print MIME::Base64.decode-str($_)' base64.txt}, "Syntax OK\n"; 77 | is q:x{perl6 -MURI::Encode -ce 'say uri_encode("http://www.google.com")'}, "Syntax OK\n"; 78 | is q:x{perl6 -MURI::Encode -ce 'say uri_decode("http://www.google.com")'}, "Syntax OK\n"; 79 | is q:x{perl6 -MHTML::Entity -ce 'print encode-entities($string)'}, "Syntax OK\n"; 80 | is q:x{perl6 -MHTML::Entity -ce 'print decode-entities($string)'}, "Syntax OK\n"; 81 | is q:x{perl6 -ce '.=uc' example.txt}, "Syntax OK\n"; 82 | is q:x{perl6 -ce 'say .uc' example.txt}, "Syntax OK\n"; 83 | is q:x{perl6 -ce '.=lc' example.txt}, "Syntax OK\n"; 84 | is q:x{perl6 -ce 'say .lc' example.txt}, "Syntax OK\n"; 85 | is q:x{perl6 -ce 'say s/(\w+){}/{$0.uc}/' example.txt}, "Syntax OK\n"; 86 | is q:x{perl6 -ce 'tr/a..zA..Z/A..Za..z/' example.txt}, "Syntax OK\n"; 87 | is q:x{perl6 -ce 'say tr/a..zA..Z/A..Za..z/.after' example.txt}, "Syntax OK\n"; 88 | is q:x{perl6 -ce 'say .wordcase' example.txt}, "Syntax OK\n"; 89 | is q:x{perl6 -ce 'say .trim-leading' example.txt}, "Syntax OK\n"; 90 | is q:x{perl6 -ce 'say .trim-trailing' example.txt}, "Syntax OK\n"; 91 | is q:x{perl6 -ce 'say .trim' example.txt}, "Syntax OK\n"; 92 | is q:x{perl6 -ce 'print .subst(/\n/, "\r\n")' example.txt}, "Syntax OK\n"; 93 | is q:x{perl6 -ce 'print .subst(/\r\n/, "\n")' example.txt}, "Syntax OK\n"; 94 | is q:x{perl6 -ce 's:g/ut/foo/' example.txt}, "Syntax OK\n"; 95 | is q:x{perl6 -ce 's:g/ut/foo/ if /Lorem/' example.txt}, "Syntax OK\n"; 96 | is q:x{perl6 -M JSON::Tiny -ce 'say to-json(lines)' example.txt}, "Syntax OK\n"; 97 | is q:x{perl6 -ce 'say .words.pick(5)' example.txt}, "Syntax OK\n"; 98 | is q:x{perl6 -ce 'my $n=2; say "banana".comb.rotor($n,$n-1).map({[~] @$_})'}, "Syntax OK\n"; 99 | is q:x{perl6 -ce 'my $n=2; say "banana".comb.rotor($n,$n-1).map({[~] @$_}).Set.sort'}, "Syntax OK\n"; 100 | is q:x{perl6 -ce 'my $n=2; say "banana".comb.rotor($n,$n-1).map({[~] @$_}).Bag.sort.join("\n")'}, "Syntax OK\n"; 101 | is q:x{perl6 -ce 'say lines[0].words.map({[~] @$_}).Bag.sort.join("\n")' example.txt}, "Syntax OK\n"; 102 | is q:x{perl6 -ce 'my $a="banana";my $b="anna";say ($a.comb (&) $b.comb)/($a.comb.Set + $b.comb.Set)'}, "Syntax OK\n"; 103 | is q:x{perl6 -ce 'my $a="banana";my $b="anna";say ($a.comb (&) $b.comb) / ($a.comb (|) $b.comb)'}, "Syntax OK\n"; 104 | is q:x{perl6 -ce 'my $a="banana";my $b="anna";say ($a.comb (&) $b.comb) / ($a.comb.Set.elems, $b.comb.Set.elems).min'}, "Syntax OK\n"; 105 | is q:x{perl6 -ce 'my $a="banana";my $b="anna";say ($a.comb (&) $b.comb)/($a.comb.Set.elems.sqrt*$b.comb.Set.elems.sqrt)'}, "Syntax OK\n"; 106 | is q:x{perl6 -ce 'say {}.push: %("banana".comb.pairs).invert'}, "Syntax OK\n"; 107 | is q:x{perl6 -ce '({}.push: %(lines[0].words.pairs).invert).sort.join("\n").say' example.txt}, "Syntax OK\n"; 108 | is q:x{perl6 -ce '.say;exit' example.txt}, "Syntax OK\n"; 109 | is q:x{perl6 -ce 'lines[0].say' example.txt}, "Syntax OK\n"; 110 | is q:x{perl6 -ce 'lines.shift.say' example.txt}, "Syntax OK\n"; 111 | is q:x{perl6 -ce 'exit if ++$ > 10' example.txt}, "Syntax OK\n"; 112 | is q:x{perl6 -ce '.say if ++$ < 11' example.txt}, "Syntax OK\n"; 113 | is q:x{perl6 -ce 'lines.pop.say' example.txt}, "Syntax OK\n"; 114 | is q:x{perl6 -ce '.say for lines[^5]' example.txt}, "Syntax OK\n"; 115 | is q:x{perl6 -ce '/<[aeiou]>/ && .print' example.txt}, "Syntax OK\n"; 116 | is q:x{perl6 -ce '.say if .comb (>=) ' example.txt}, "Syntax OK\n"; 117 | is q:x{perl6 -ce '.say if .comb ⊇ ' example.txt}, "Syntax OK\n"; 118 | is q:x{perl6 -ce '.print if .chars >= 80' example.txt}, "Syntax OK\n"; 119 | is q:x{perl6 -ce '.chars >= 80 && .print' example.txt}, "Syntax OK\n"; 120 | is q:x{perl6 -ce '.print if ++$ == 2' example.txt}, "Syntax OK\n"; 121 | is q:x{perl6 -ce 'next if ++$ == 2' example.txt}, "Syntax OK\n"; 122 | is q:x{perl6 -ce '.print if (1..3).any == ++$' example.txt}, "Syntax OK\n"; 123 | is q:x{perl6 -ce '.print if /^Lorem/../laborum\.$/' example.txt}, "Syntax OK\n"; 124 | is q:x{perl6 -ce 'say lines.max.chars' example.txt}, "Syntax OK\n"; 125 | is q:x{perl6 -ce 'state $l=0; $l = .chars if .chars > $l;END { $l.say }' example.txt}, "Syntax OK\n"; 126 | is q:x{perl6 -ce 'say lines.max' example.txt}, "Syntax OK\n"; 127 | is q:x{perl6 -ce 'my $l=""; for (lines) {$l = $_ if .chars > $l.chars};END { $l.say }' example.txt}, "Syntax OK\n"; 128 | is q:x{perl6 -ce '.say if /\d/' example.txt}, "Syntax OK\n"; 129 | is q:x{perl6 -ce '.say for lines.grep(/\d/)' example.txt}, "Syntax OK\n"; 130 | is q:x{perl6 -ce '/\d/ && .say' example.txt}, "Syntax OK\n"; 131 | is q:x{perl6 -ce 'next if ! $_.match(/\d/)' example.txt}, "Syntax OK\n"; 132 | is q:x{perl6 -ce '.say if /^\d+$/' example.txt}, "Syntax OK\n"; 133 | is q:x{perl6 -ce '.say for lines.grep(/^\d+$/)' example.txt}, "Syntax OK\n"; 134 | is q:x{perl6 -ce '/^\d+$/ && .say' example.txt}, "Syntax OK\n"; 135 | is q:x{perl6 -ce 'next if ! $_.match(/^\d+$/)' example.txt}, "Syntax OK\n"; 136 | is q:x{perl6 -ce '.say if ++$ % 2' example.txt}, "Syntax OK\n"; 137 | is q:x{perl6 -ce '.say if ! (++$ % 2)' example.txt}, "Syntax OK\n"; 138 | #is q:x{perl6 -ce 'state %l;.say if ++%l{$_}==2' example.txt}, "Syntax OK\n"; 139 | #is q:x{perl6 -ce 'state %l;.say if ++%l{$_}==1' example.txt}, "Syntax OK\n"; 140 | is q:x{perl6 -ce '.words[0].say' example.txt}, "Syntax OK\n"; 141 | is q:x{perl6 -ce '.say if 1.rand <= 0.05' /usr/share/dict/words}, "Syntax OK\n"; 142 | is q:x{perl6 -M HTTP::UserAgent -ce 'say HTTP::UserAgent.new.get("google.com").content'}, "Syntax OK\n"; 143 | is q:x{perl6 -M HTTP::Server::Simple -ce 'HTTP::Server::Simple.new.run'}, "Syntax OK\n"; 144 | is q:x{perl6 -ce '.comb(/\w\w/).map({:16($_)}).say'}, "Syntax OK\n"; 145 | is q:x{perl6 -ce '.comb(/\w\w/).map({:16($_)}).say'}, "Syntax OK\n"; 146 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Perl 6 One Liners 2 | ================= 3 | 4 | This book is a work in progress. I hope you find it interesting, maybe even useful! If you would like to contribute, feedback, issues and new or improved regexes are all welcome! 5 | 6 | 7 | AUTHOR 8 | ------ 9 | 10 | David Farrell [PerlTricks.com](http://perltricks.com) 11 | 12 | 13 | VERSION 14 | ------- 15 | 16 | Version 0.01 17 | 18 | 19 | LICENSE 20 | ------- 21 | 22 | FreeBSD - see LICENSE 23 | 24 | 25 | CONTRIBUTORS 26 | ------------ 27 | 28 | * Alexander Moquin 29 | * Bruce Gray 30 | * Cale 31 | * Carl Mäsak 32 | * David H. Adler 33 | * FROGGS 34 | * Helmut Wollmersdorfer 35 | * Hugh Simpson 36 | * japhb 37 | * Larry Wall 38 | * Mathieu Gagnon 39 | * Matt Oates 40 | * Moritz Lenz 41 | * Mouq 42 | * Salve J Nilsen 43 | * Sam S 44 | * Skids 45 | * timotimo 46 | 47 | 48 | THANKS 49 | ------ 50 | 51 | Inspired by Peteris Krumins Perl 5 examples [file](http://www.catonmat.net/download/perl1line.txt). He literally wrote the [book](http://www.nostarch.com/perloneliners) on Perl 5 one liners. 52 | 53 | The wonderful folks on #Perl6 [irc](http://webchat.freenode.net/?channels=perl6&nick=). 54 | 55 | 56 | CONTENTS 57 | -------- 58 | 59 | 1. [Introduction](#introduction) 60 | 2. [Tutorial](#tutorial) 61 | 3. [File Spacing](#file-spacing) 62 | 4. [Line Numbering](#line-numbering) 63 | 5. [Calculations](#calculations) 64 | 6. [String Creation and Array Creation](#string-creation-and-array-creation) 65 | 7. [Text Conversion and Substitution](#text-conversion-and-substitution) 66 | 8. [Text Analysis](#text-analysis) 67 | 9. [Selective Line Printing](#selective-line-printing) 68 | 10. [Data Transformation With Pipes](data-transformation-with-pipes) (in progress) 69 | 11. [WWW](#www) (in progress) 70 | 12. [Converting for Windows](#converting-for-windows) 71 | 72 | 73 | INTRODUCTION 74 | ------------ 75 | 76 | One thing that sets Perl apart from other languages is the ability to write small programs in a single line of code, known as a "one liner". It's often faster to type a program directly into the terminal than to write a throwaway script. And one liners are powerful too; they're fully fledged programs which can load external libraries but also integrate into the terminal. You can pipe data in or out of a one liner. 77 | 78 | Like Perl 5, Perl 6 supports one liners. And just like Perl 6 cleaned up Perl 5's warts elsewhere, the one liner syntax is also better. It's cleaner with fewer special variables and options to memorize. This book provides many useful examples of Perl 6 one liners that can do everything from finding duplicate lines in a file to running a web server. Although Perl 6 has fewer special variables, because of its advanced object oriented syntax most of the one liners are shorter in Perl 6 than their Perl 5 equivalent. 79 | 80 | This book can be read in a number of ways. If you're new to one liners, start with the [tutorial](#tutorial). It walks you through the core concepts of a one liner; don't worry - it's really very simple once you get the hang of it. If you're familiar with Perl, Bash or Sed/Awk, you can probably get stuck in to the examples right away. Feel free to skim and scan the material for whatever piques your interest. If you don't understand some code, try it out in the terminal! Included in this repo is the ubiquitous `example.txt` file which is used in many of the one liners. 81 | 82 | Programming with one liners is just one paradigm that Perl 6 excels in. There's a beauty in the brevity of this code, but whilst you're learning a productive skill, remember that you're also learning the ropes of a powerful new programming language. Check out the [perl6.org](http://perl6.org) website for the official documentation. 83 | 84 | 85 | TUTORIAL 86 | -------- 87 | 88 | To get started with one liners, all you really need to understand is the `-e` option. This tells Perl to execute what follows as a program. For example: 89 | 90 | perl6 -e 'say "Hello, World!"' 91 | 92 | Let's step through this code. `perl6` invokes the Perl 6 program, `-e` tells Perl 6 to execute and `'say "Hello, World!"'` is the program. Every program must be surrounded in single quotes (except on Windows, see [Converting for Windows](#converting-for-windows)). To run the one-liner, just type it into the terminal: 93 | 94 | > perl6 -e 'say "Hello, World!"' 95 | Hello, World! 96 | 97 | If you want to load a file, just add the path to the file after the program code: 98 | 99 | perl6 -e 'for (lines) { say $_ }' /path/to/file.txt 100 | 101 | This program prints every line in `/path/to/file.txt`. You may know that `$_` is the default variable, which in this case is the current line being looped through. `lines` is a list that is automatically created for you whenever you pass a filepath to a one-liner. Now let's re-write that one liner, step-by-step. These are all equivalent: 102 | 103 | perl6 -e 'for (lines) { say $_ }' /path/to/file.txt 104 | perl6 -e 'for (lines) { $_.say }' /path/to/file.txt 105 | perl6 -e 'for (lines) { .say }' /path/to/file.txt 106 | perl6 -e '.say for (lines)' /path/to/file.txt 107 | perl6 -e '.say for lines' /path/to/file.txt 108 | 109 | Just like `$_` is the default variable, methods called on the default variable can omit the variable reference. They become default methods. So `$_.say` becomes `.say`. This brevity pays off with one liners - it's less typing! 110 | 111 | The `-n` option changes the behavior of the program: it executes the code once for every line of the file. So uppercase and print every line of `/path/to/file.txt` you can type: 112 | 113 | perl6 -ne '.uc.say' /path/to/file.txt 114 | 115 | The `-p` option is just like `-n` except that it will automatically print `$_`. So another way we could uppercase a file would be: 116 | 117 | perl6 -pe '$_ = .uc' /path/to/file.txt 118 | 119 | Or two shorter versions that do the same thing: 120 | 121 | perl6 -pe '.=uc' /path/to/file.txt 122 | perl6 -pe .=uc /path/to/file.txt 123 | 124 | In the second example we were able to completely remove the surrounding single quotes. This is a rare scenario, but in the event your one liner has no spaces and no sigils or quotes in it, you can usually remove the outer quotes. 125 | 126 | The `-n` and `-p` options are really useful. There are lots of example one-liners that use them in this book. 127 | 128 | The final thing you should know is how to load a module. This is really powerful as you can extend Perl 6's capabilities by importing external libraries. The `-M` switch stands for load module: 129 | 130 | perl6 -M URI::Encode -e 'say uri_encode("example.com/10 ways to crush it with Perl 6")' 131 | 132 | This: `-M URI::Encode` loads the URI::Encode module, which exports the `uri_encode` subroutine. You can use `-M` more than once if you want to load more than one module: 133 | 134 | perl6 -M URI::Encode -M URI -e '' 135 | 136 | What if you have a local module, that is not installed yet? Easy, just pass use the `-I` switch to include the directory: 137 | 138 | perl6 -I lib -M URI::Encode -e '' 139 | 140 | Now Perl 6 will search for `URI::Encode` in `lib` as well as the standard install locations. 141 | 142 | To get a list of Perl 6 command line switches, use the `-h` option for help: 143 | 144 | perl6 -h 145 | 146 | This prints a nice summary of the available options. 147 | 148 | 149 | FILE SPACING 150 | ------------ 151 | 152 | Double space a file 153 | 154 | perl6 -pe '$_ ~= "\n"' example.txt 155 | 156 | N-space a file (e.g. quadruple space) 157 | 158 | perl6 -pe '$_ ~= "\n" x 4' example.txt 159 | 160 | Add a blank line before every line 161 | 162 | perl6 -pe 'say ""' example.txt 163 | 164 | Remove all blank lines 165 | 166 | perl6 -ne '.say if /\S/' example.txt 167 | perl6 -ne '.say if .chars' example.txt 168 | 169 | Remove all consecutive blank lines, leaving just one 170 | 171 | perl6 -e '$*ARGFILES.slurp.subst(/\n+/, "\n\n", :g).say' example.txt 172 | 173 | 174 | LINE NUMBERING 175 | -------------- 176 | 177 | Number all lines in a file 178 | 179 | perl6 -ne 'say "{++$} $_"' example.txt 180 | perl6 -ne 'say $*ARGFILES.lines.kv ~ " $_"' example.txt 181 | 182 | Number only non-empty lines in a file 183 | 184 | perl6 -pe '$_ = "{++$} $_" if /\S/' example.txt 185 | 186 | Number all lines but print line numbers only for non-empty lines 187 | 188 | perl6 -pe '$_ = $*ARGFILES.lines.kv ~ " $_" if /\S/' example.txt 189 | 190 | Print the total number of lines in a file (emulate wc -l) 191 | 192 | perl6 -e 'say lines.elems' example.txt 193 | perl6 -e 'say lines.Int' example.txt 194 | perl6 -e 'lines.Int.say' example.txt 195 | 196 | Print the number of non-empty lines in a file 197 | 198 | perl6 -e 'lines.grep(/\S/).elems.say' example.txt 199 | 200 | Print the number of empty lines in a file 201 | 202 | perl6 -e 'lines.grep(/^\s*$/).elems.say' example.txt 203 | 204 | 205 | CALCULATIONS 206 | ------------ 207 | 208 | Check if a number is a prime 209 | 210 | perl6 -e 'say "7 is prime" if 7.is-prime' 211 | 212 | Print the sum of all the fields on a line 213 | 214 | perl6 -ne 'say [+] .split("\t")' 215 | 216 | Print the sum of all the fields on all lines 217 | 218 | perl6 -e 'say [+] lines.split("\t")' 219 | 220 | Shuffle all fields on a line 221 | 222 | perl6 -ne '.split("\t").pick(*).join("\t").say' 223 | 224 | Find the lexically minimum element on a line 225 | 226 | perl6 -ne '.split("\t").min.say' 227 | 228 | Find the lexically minimum element over all the lines 229 | 230 | perl6 -e 'lines.split("\t").min.say' 231 | 232 | Find the lexically maximum element on a line 233 | 234 | perl6 -ne '.split("\t").max.say' 235 | 236 | Find the lexically maximum element over all the lines 237 | 238 | perl6 -e 'lines.split("\t").max.say' 239 | 240 | Find the numerically minimum element on a line 241 | 242 | perl6 -ne '.split("\t")».Numeric.min.say' 243 | 244 | Find the numerically maximum element on a line 245 | 246 | perl6 -ne '.split("\t")».Numeric.max.say' 247 | 248 | Replace each field with its absolute value 249 | 250 | perl6 -ne '.split("\t").map(*.abs).join("\t").say' 251 | 252 | Find the total number of letters on each line 253 | 254 | perl6 -ne '.chars.say' example.txt 255 | 256 | Find the total number of words on each line 257 | 258 | perl6 -ne '.words.elems.say' example.txt 259 | 260 | Find the total number of elements on each line, split on a comma 261 | 262 | perl6 -ne '.split(",").elems.say' example.txt 263 | 264 | Find the total number of fields (words) on all lines 265 | 266 | perl6 -e 'say lines.split("\t").elems' #fields 267 | perl6 -e 'say lines.words.elems' example.txt #words 268 | 269 | Print the total number of fields that match a pattern 270 | 271 | perl6 -e 'say lines.split("\t").comb(/pattern/).elems' #fields 272 | perl6 -e 'say lines.words.comb(/pattern/).elems' #words 273 | 274 | Print the total number of lines that match a pattern 275 | 276 | perl6 -e 'say lines.grep(/in/).elems' 277 | 278 | Print the number PI to n decimal places (e.g. 10) 279 | 280 | perl6 -e 'say pi.fmt("%.10f");' 281 | 282 | Print the number PI to 15 decimal places 283 | 284 | perl6 -e 'say π' 285 | 286 | Print the number E to n decimal places (e.g. 10) 287 | 288 | perl6 -e 'say e.fmt("%.10f");' 289 | 290 | Print the number E to 15 decimal places 291 | 292 | perl6 -e 'say e' 293 | 294 | Print UNIX time (seconds since Jan 1, 1970, 00:00:00 UTC) 295 | 296 | perl6 -e 'say time' 297 | 298 | Print GMT (Greenwich Mean Time) and local computer time 299 | 300 | perl6 -MDateTime::TimeZone -e 'say to-timezone("GMT",DateTime.now)' 301 | perl6 -e 'say DateTime.now' 302 | 303 | Print local computer time in H:M:S format 304 | 305 | perl6 -e 'say DateTime.now.map({$_.hour, $_.minute, $_.second.round}).join(":")' 306 | 307 | Print yesterday's date 308 | 309 | perl6 -e 'say DateTime.now.earlier(:1day)' 310 | 311 | Print date 14 months, 9 days and 7 seconds ago 312 | 313 | perl6 -e 'say DateTime.now.earlier(:14months).earlier(:9days).earlier(:7seconds)' 314 | 315 | Prepend timestamps to stdout (GMT, localtime) 316 | 317 | tail -f logfile | perl6 -MDateTime::TimeZone -ne 'say to-timezone("GMT",DateTime.now) ~ "\t$_"' 318 | tail -f logfile | perl6 -ne 'say DateTime.now ~ "\t$_"' 319 | 320 | Calculate factorial of 5 321 | 322 | perl6 -e 'say [*] 1..5' 323 | 324 | Calculate greatest common divisor 325 | 326 | perl6 -e 'say [gcd] @list_of_numbers' 327 | 328 | Calculate GCM of numbers 20 and 35 using Euclid's algorithm 329 | 330 | perl6 -e 'say (20, 35, *%* ... 0)[*-2]' 331 | 332 | Calculate least common multiple (LCM) of 20 and 35 333 | 334 | perl6 -e 'say 20 lcm 35' 335 | 336 | Calculate LCM of 20 and 35 using Euclid's algorithm: `n*m/gcd(n,m)` 337 | 338 | perl6 -e 'say 20 * 35 / (20 gcd 35)' 339 | 340 | Generate 10 random numbers between 5 and 15 (excluding 15) 341 | 342 | perl6 -e '.say for (5..^15).roll(10)' 343 | 344 | Find and print all permutations of a list 345 | 346 | perl6 -e 'say .join for [1..5].permutations' 347 | 348 | Generate the power set 349 | 350 | perl6 -e '.say for <1 2 3>.combinations' 351 | 352 | Convert an IP address to unsigned integer 353 | 354 | perl6 -e 'say :256["127.0.0.1".comb(/\d+/)]' 355 | perl6 -e 'say +":256[{q/127.0.0.1/.subst(:g,/\./,q/,/)}]"' 356 | perl6 -e 'say Buf.new(+«"127.0.0.1".split(".")).unpack("N")' 357 | 358 | Convert an unsigned integer to an IP address 359 | 360 | perl6 -e 'say join ".", @(pack "N", 2130706433)' 361 | perl6 -e 'say join ".", map { ((2130706433+>(8*$_))+&0xFF) }, (3...0)' 362 | 363 | STRING CREATION AND ARRAY CREATION 364 | ---------------------------------- 365 | 366 | Generate and print the alphabet 367 | 368 | perl6 -e '.say for "a".."z"' 369 | 370 | Generate and print all the strings from "a" to "zz" 371 | 372 | perl6 -e '.say for "a".."zz"' 373 | 374 | Convert a integer to hex 375 | 376 | perl6 -e 'say 255.base(16)' 377 | perl6 -e 'say sprintf("%x", 255)' 378 | 379 | Print an int to hex translation table 380 | 381 | perl6 -e 'say sprintf("%3i => %2x", $_, $_) for 0..255' 382 | 383 | Percent encode an integer 384 | 385 | perl6 -e 'say sprintf("%%%x", 255)' 386 | 387 | Generate a random 10 a-z character string 388 | 389 | perl6 -e 'print roll 10, "a".."z"' 390 | perl6 -e 'print roll "a".."z": 10' 391 | 392 | Generate a random 15 ASCII Character password 393 | 394 | perl6 -e 'print roll 15, "0".."z"' 395 | perl6 -e 'print roll "0".."z": 15' 396 | 397 | Create a string of specific length 398 | 399 | perl6 -e 'print "a" x 50' 400 | 401 | Generate and print an array of even numbers from 1 to 100 402 | 403 | perl6 -e '(1..100).grep(* %% 2).say' 404 | 405 | Find the length of the string 406 | 407 | perl6 -e '"storm in a teacup".chars.say' 408 | 409 | Find the number of elements in an array 410 | 411 | perl6 -e 'my @letters = "a".."z"; @letters.Int.say' 412 | 413 | 414 | TEXT CONVERSION AND SUBSTITUTION 415 | -------------------------------- 416 | 417 | ROT 13 a file 418 | 419 | perl6 -pe 'tr/A..Za..z/N..ZA..Mn..za..m/' example.txt 420 | 421 | Base64 encode a string 422 | 423 | perl6 -MMIME::Base64 -ne 'print MIME::Base64.encode-str($_)' example.txt 424 | 425 | Base64 decode a string 426 | 427 | perl6 -MMIME::Base64 -ne 'print MIME::Base64.decode-str($_)' base64.txt 428 | 429 | URL-escape a string 430 | 431 | perl6 -MURI::Encode -le 'say uri_encode($string)' 432 | 433 | URL-unescape a string 434 | 435 | perl6 -MURI::Encode -le 'say uri_decode($string)' 436 | 437 | HTML-encode a string 438 | 439 | perl6 -MHTML::Entity -e 'print encode-entities($string)' 440 | 441 | HTML-decode a string 442 | 443 | perl6 -MHTML::Entity -e 'print decode-entities($string)' 444 | 445 | Convert all text to uppercase 446 | 447 | perl6 -pe '.=uc' example.txt 448 | perl6 -ne 'say .uc' example.txt 449 | 450 | Convert all text to lowercase 451 | 452 | perl6 -pe '.=lc' example.txt 453 | perl6 -ne 'say .lc' example.txt 454 | 455 | Uppercase only the first word of each line 456 | 457 | perl6 -ne 'say s/(\w+){}/{$0.uc}/' example.txt 458 | 459 | Invert the letter case 460 | 461 | perl6 -pe 'tr/a..zA..Z/A..Za..z/' example.txt 462 | perl6 -ne 'say tr/a..zA..Z/A..Za..z/.after' example.txt 463 | 464 | Camel case each line 465 | 466 | perl6 -ne 'say .wordcase' example.txt 467 | 468 | Strip leading whitespace (spaces, tabs) from the beginning of each line 469 | 470 | perl6 -ne 'say .trim-leading' example.txt 471 | 472 | Strip trailing whitespace (space, tabs) from the end of each line 473 | 474 | perl6 -ne 'say .trim-trailing' example.txt 475 | 476 | Strip whitespace from the beginning and end of each line 477 | 478 | perl6 -ne 'say .trim' example.txt 479 | 480 | Convert UNIX newlines to DOS/Windows newlines 481 | 482 | perl6 -ne 'print .subst(/\n/, "\r\n")' example.txt 483 | 484 | Convert DOS/Windows newlines to UNIX newlines 485 | 486 | perl6 -ne 'print .subst(/\r\n/, "\n")' example.txt 487 | 488 | Find and replace all instances of "ut" with "foo" on each line 489 | 490 | perl6 -pe 's:g/ut/foo/' example.txt 491 | 492 | Find and replace all instances of "ut" with "foo" on each line that contains "lorem" 493 | 494 | perl6 -pe 's:g/ut/foo/ if /Lorem/' example.txt 495 | 496 | Convert a file to JSON 497 | 498 | perl6 -M JSON::Tiny -e 'say to-json(lines)' example.txt 499 | 500 | Pick 5 random words from each line of a file 501 | 502 | perl6 -ne 'say .words.pick(5)' example.txt 503 | 504 | 505 | TEXT ANALYSIS 506 | ------------- 507 | 508 | Print n-grams of a string 509 | 510 | perl6 -e 'my $n=2; say "banana".comb.rotor($n => 1 - $n)».join()' 511 | 512 | Print unique n-grams 513 | 514 | perl6 -e 'my $n=2; say "banana".comb.rotor($n => 1 - $n)».join().Set.sort' 515 | 516 | Print occurrence counts of n-grams 517 | 518 | perl6 -e 'my $n=2; say "banana".comb.rotor($n => 1 - $n)».join().Bag.sort.join("\n")' 519 | 520 | Print occurrence counts of words (1-grams) 521 | 522 | perl6 -e 'say lines[0].words.join().Bag.sort.join("\n")' example.txt 523 | 524 | Print Dice similarity coefficient based on sets of 1-grams 525 | 526 | perl6 -e 'my $a="banana".comb;my $b="anna".comb;say ($a (&) $b)/($a.Set + $b.Set)' 527 | 528 | Print Jaccard similarity coefficient based on 1-grams 529 | 530 | perl6 -e 'my $a="banana".comb;my $b="anna".comb;say ($a (&) $b) / ($a (|) $b)' 531 | 532 | Print overlap coefficient based on 1-grams 533 | 534 | perl6 -e 'my $a="banana".comb;my $b="anna".comb;say ($a (&) $b)/($a.Set.elems,$b.Set.elems).min' 535 | 536 | Print cosine similarity based on 1-grams 537 | 538 | perl6 -e 'my $a="banana".comb;my $b="anna".comb;say ($a (&) $b)/($a.Set.elems.sqrt*$b.Set.elems.sqrt)' 539 | 540 | Build an index of characters within a string and print it 541 | 542 | perl6 -e 'say {}.push: %("banana".comb.pairs).invert' 543 | 544 | Build an index of words within a line and print it 545 | 546 | perl6 -e '({}.push: %(lines[0].words.pairs).invert).sort.join("\n").say' example.txt 547 | 548 | 549 | SELECTIVE LINE PRINTING 550 | ----------------------- 551 | 552 | Print the first line of a file (emulate head -1) 553 | 554 | perl6 -ne '.say;exit' example.txt 555 | perl6 -e 'lines[0].say' example.txt 556 | perl6 -e 'lines.shift.say' example.txt 557 | 558 | Print the first 10 lines of a file (emulate head -10) 559 | 560 | perl6 -pe 'exit if ++$ > 10' example.txt 561 | perl6 -ne '.say if ++$ < 11' example.txt 562 | 563 | Print the last line of a file (emulate tail -1) 564 | 565 | perl6 -e 'lines.pop.say' example.txt 566 | 567 | Print the last 5 lines of a file (emulate tail -5) 568 | 569 | perl6 -e '.say for lines[*-5..*]' example.txt 570 | 571 | Print only lines that contain vowels 572 | 573 | perl6 -ne '/<[aeiou]>/ && .print' example.txt 574 | 575 | Print lines that contain all vowels 576 | 577 | perl6 -ne '.say if .comb (>=) ' example.txt 578 | perl6 -ne '.say if .comb ⊇ ' example.txt 579 | 580 | Print lines that are 80 chars or longer 581 | 582 | perl6 -ne '.print if .chars >= 80' example.txt 583 | perl6 -ne '.chars >= 80 && .print' example.txt 584 | 585 | Print only line 2 586 | 587 | perl6 -ne '.print if ++$ == 2' example.txt 588 | 589 | Print all lines except line 2 590 | 591 | perl6 -pe 'next if ++$ == 2' example.txt 592 | 593 | Print all lines 1 to 3 594 | 595 | perl6 -ne '.print if (1..3).any == ++$' example.txt 596 | 597 | Print all lines between two regexes (including lines that match regex) 598 | 599 | perl6 -ne '.print if /^Lorem/../laborum\.$/' example.txt 600 | 601 | Print the length of the longest line 602 | 603 | perl6 -e 'say lines.max.chars' example.txt 604 | perl6 -ne 'state $l=0; $l = .chars if .chars > $l;END { $l.say }' example.txt 605 | 606 | Print the longest line 607 | 608 | perl6 -e 'say lines.max' example.txt 609 | perl6 -e 'my $l=""; for (lines) {$l = $_ if .chars > $l.chars};END { $l.say }' example.txt 610 | 611 | Print all lines that contain a number 612 | 613 | perl6 -ne '.say if /\d/' example.txt 614 | perl6 -e '.say for lines.grep(/\d/)' example.txt 615 | perl6 -ne '/\d/ && .say' example.txt 616 | perl6 -pe 'next if ! $_.match(/\d/)' example.txt 617 | 618 | Find all lines that contain only a number 619 | 620 | perl6 -ne '.say if /^\d+$/' example.txt 621 | perl6 -e '.say for lines.grep(/^\d+$/)' example.txt 622 | perl6 -ne '/^\d+$/ && .say' example.txt 623 | perl6 -pe 'next if ! $_.match(/^\d+$/)' example.txt 624 | 625 | Print every even line 626 | 627 | perl6 -ne '.say if ++$ %% 2' example.txt 628 | 629 | Print every odd line 630 | 631 | perl6 -ne '.say if ++$ !%% 2' example.txt 632 | 633 | Print all lines that repeat 634 | 635 | perl6 -ne 'state %l;.say if ++%l{$_}==2' example.txt 636 | 637 | Print unique lines 638 | 639 | perl6 -ne 'state %l;.say if ++%l{$_}==1' example.txt 640 | 641 | Print the first field (word) of every line (emulate cut -f 1 -d ' ') 642 | 643 | perl6 -ne '.words[0].say' example.txt 644 | 645 | 646 | DATA TRANSFORMATION WITH PIPES 647 | ------------------------------ 648 | 649 | Perl 6 programs integrate straight into the command line. You can pipe data in-to and out-of a one liner by using the pipe `|` character. For piping data in, Perl 6 automatically sets STDIN to `$*IN`. Just like with files, data piped in can be looped through using `-n` and is also available in `lines`. To pipe data out of a one liner just use `print` or `say`. 650 | 651 | JSON-encode a list of all files in the current directory 652 | 653 | ls | perl6 -M JSON::Tiny -e 'say to-json(lines)' 654 | 655 | Print a random sample of approx 5% of lines in a file 656 | 657 | perl6 -ne '.say if 1.rand <= 0.05' /usr/share/dict/words 658 | 659 | Color conversion, HTML to RGB 660 | 661 | echo "#ffff00" | perl6 -ne '.comb(/\w\w/).map({:16($_)}).say' 662 | 663 | Color conversion, RGB to HTML 664 | 665 | echo "#ffff00" | perl6 -ne '.comb(/\w\w/).map({:16($_)}).say' 666 | 667 | WWW 668 | --- 669 | 670 | Download a webpage 671 | 672 | perl6 -M HTTP::UserAgent -e 'say HTTP::UserAgent.new.get("google.com").content' 673 | 674 | Download a webpage and strip out the HTML 675 | 676 | wget -O - "http://perl6.org" | perl6 -ne 's:g/\<.+?\>//.say' 677 | 678 | Download a webpage, strip out and decode the HTML 679 | 680 | wget -O - "http://perl6.org" | perl6 -MHTML::Strip -ne 'strip_html($_).say' 681 | 682 | Launch a simple web server 683 | 684 | perl6 -M HTTP::Server::Simple -e 'HTTP::Server::Simple.new.run' 685 | 686 | 687 | CONVERTING FOR WINDOWS 688 | ---------------------- 689 | 690 | Running one liners on Windows is a piece of cake once you know the rules of the road. One liners work on both cmd.exe and PowerShell. The cardinal rule is: replace the outer single-quotes with double quotes and use the interpolated quoting operator `qq//` for quoting strings inside a one liner. For non-interpolated quoting, you can use single-quotes. Let's look at some examples. 691 | 692 | Here's a simple one liner to print the time: 693 | 694 | perl6 -e 'say DateTime.now' 695 | 696 | To run on Windows, we just replace the single quotes with double quotes: 697 | 698 | perl6 -e "say DateTime.now" 699 | 700 | This one liner appends a newline to every line in a file, using an interpolated string: 701 | 702 | perl6 -pe '$_ ~= "\n"' example.txt 703 | 704 | On Windows this should be written as: 705 | 706 | perl6 -pe "$_ ~= qq/\n/" example.txt 707 | 708 | In this case we want to interpolate `\n` as a newline and not literally add a backslash and an "n" to the line, so we have to use `qq`. But you can usually use single-quotes within a one liner so: 709 | 710 | perl6 -e 'say "Hello, World!"' 711 | 712 | On Windows can be written as: 713 | 714 | perl6 -e "say 'hello, World!'" 715 | 716 | Simple output redirection works like it does on Unix-based systems. This one liner prints an ASCII character index table to a file using `>`: 717 | 718 | perl6 -e "say .chr ~ ' ' ~ $_ for 0..255" > ascii_codes.txt 719 | 720 | When using `>` if the file doesn't exist, it will be created. If the file does exist, it will be overwritten. If you'd rather append to a file, use `>>` instead. 721 | 722 | --------------------------------------------------------------------------------