Skip to content

Instantly share code, notes, and snippets.

@opendevnet
Last active December 16, 2015 22:09
Show Gist options
  • Save opendevnet/5504709 to your computer and use it in GitHub Desktop.
Save opendevnet/5504709 to your computer and use it in GitHub Desktop.
# Converting to both perl5 and perl6 to see differences and similarities.
# Note that if you have a perl > 5.14.* the -E switch enables
# say, unicode, etc. which makes perl5 a bit more like perl6
# for oneliners anyway. Is using the -p switch cheating?
#------------------------------------------------------------
# 1. Print the third column of the line in a file.
# a.
perl6 -n -e '.split(" ")[2].say' file.txt
# How to get this to print only non Nil values; or to print ""
# when the 3rd column is empty; or print "No third column"
# instead of Nil ...
# This prints column three if it exists:
perl6 -n -e '.say if .split("")[2]' file.txt
# This tells us what happened:
perl6 -n -e 'if .split("")[2] {.say} else {say "No third column"}
# a ternary construct could be used instead:
perl6 -n -e '.split("")[2] ?? say $_ !! say "" '
#DRYer:
perl6 -n -e 'say .split("")[2] ?? $_ !! "" ' file.txt
# ...
#------------------------------------------------------------
# 2. Sum up the lines in a file:
# a. They teach you this in beginner perl books!
perl5 -E 'say scalar(()=<>)' file.txt
# b. This is quite different can I make it more similar?
perl6 -e 'say $*ARGFILES.lines.elems' file.txt
# c. It seems if file.txt presents as input (i.e. "<") and not
# just an ARG, then $*IN works too.
perl6 -e 'say $*IN.lines.elems' < file.txt
# d. OTOH if you call lines() as a function instead of as a
# method on a file object you get this nice correct "idiom":
perl6 -e 'say lines().elems' file.txt
# Since running scalar sub on an empty list scalar(()=<>)
# is a bit of a "trick" in perl5 syntax, it's more fair to
# compare something like:
perl5 -nE '}{say $.' file.txt
# Is the butterly operator ("}{") a perl5-ism?
#------------------------------------------------------------
# 3. Print an empty line after each line
# a. First the obvious way in perl5 then a perl6ish way in perl5 ...
perl5 -ne 'print "\n" ' file.txt
perl5 -nE 'say "$_"' file.txt
# b. Since the -p option prints lines and "say" adds a newline
@ this works. Why can't -p and -e be combined with perl6?
perl6 -p -e 'say ' file.txt
#------------------------------------------------------------
# 4. Number the lines
# a. They teach you this in beginner perl books too!!
perl5 -pe '$_ = "$. $_"' file.txt
# b. Wanted to ++ the variable and use "state" to "localize" it in one pass.
# without using "my $num; ". But I took too long and then saw grondilu's
# solution. I've mostly copied it :-\
perl6 -e 'say (state $num)++, " $_" for $*IN.lines' < file.txt
# The one I like best was from PerlJam on IRC:
perl6 -e 'say (my $)++ ~ " $_" for lines()' < file.txt
# This one is very close to perl5
perl6 -p -e '$_ = "{(state $)++} $_"' file.txt
# The variable can be nameless and the match matches everything
# coming from lines().
#------------------------------------------------------------
# 5. Print everything between two lines:
# a.
perl5 -ne 'print if /START/../END/' file.txt
# b.
perl5 -nE 'say if /START/../END/' file.txt
# c.
perl6 -e 'say $_ if /^ START/ ff /^END/ for lines' file.txt
# d.
perl6 -e '.say if /START/ ff /END/ for lines' file.txt
# e.
perl6 -e '.say if /START/ ff /END/ for lines' file.txt
# I like that b. and d. are similar since I may remember how and
# why they differ. In perl6, "say" wants to refer to something
# explicitly (even if it is $_) or to be called as a method
# on the results (as in e. where ".say" is invoked on
# the results of the 'if /START/ ff /END/ for lines' statement.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment