Last active
August 29, 2015 14:04
-
-
Save possatti/ee271e74455ca325dae4 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/perl | |
while(<STDIN>){ # Puts each line at $_ , which can be used in the loop. | |
chomp; #Implicitly uses $_, if no parameter is specified. | |
print "hello $_\n"; # Uses $_ explicitly in a string. | |
} | |
## It is possible to do a fast little trick on the command line: | |
# perl -pe '#Transform $_#' | |
## -n : surround you code with 'while (<>) { #your code# }' . | |
## -p : surround you code with 'while (<>) { #your code# }' and print $_ each loop. | |
## -e : execute code. | |
## -a : split $_ words to the variable @F . | |
## This way, perl can be used kind like sed. | |
## | |
## See example below, which filters the words, printing only the first word of each sentence: | |
# $ perl -pe 's/\s*(\w+).*/$1/;' << EOF | |
# > There is a pie. | |
# > No pie. | |
# > Where pie? | |
# > EOF | |
# There | |
# No | |
# Where | |
## | |
## Go check https://www.cs.cf.ac.uk/Dave/PERL/node164.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment