Last active
November 1, 2015 16:14
-
-
Save MadcapJake/2c78ea7585143298c66a to your computer and use it in GitHub Desktop.
Translate English to Pig Latin with ease!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| given @*ARGS[0] { | |
| when @_ eq '-p' or @_ eq '--phrase' { | |
| say translate(@*ARGS[1]); | |
| } | |
| when @_ eq '-f' or @_ eq '--file' { | |
| say translate(@*ARGS[1].IO.slurp); | |
| } | |
| default { | |
| say 'You must provide either "-p"/"--phrase" and a phrase or "-f"/"--file" and a filepath.'; | |
| exit(1); | |
| } | |
| } | |
| sub translate(Str:D $english) { [~] $english.split(/<:!Letter>+/, :all).map: &translate-word } | |
| sub translate-word($word) { | |
| # 'y' is left in the consonants because | |
| # in English it is always a consonant when | |
| # at the beginning of a word. | |
| my $vowels = /<[aeiouAEIOU]>/; | |
| my $consonants = /<:Letter-[aeiouAEIOU]>+/; | |
| given $word { | |
| when $word.match($vowels) and $/.from == 0 { | |
| $word ~ 'yay'; | |
| } | |
| when $word.match($consonants) and $/.from == 0 { | |
| samecase( | |
| $word.substr($/.to) ~ $word.substr($/.from, ($/.to - $/.from)) ~ 'ay', | |
| $word | |
| ); | |
| } | |
| default { $word } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How about using
MAINmulti subs like the following:If you called the program with incorrect arguments, or no arguments you would get the following in STDERR
( If you pass in a
--helpit would get printed to STDOUT )