Skip to content

Instantly share code, notes, and snippets.

@possatti
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save possatti/ee271e74455ca325dae4 to your computer and use it in GitHub Desktop.
Save possatti/ee271e74455ca325dae4 to your computer and use it in GitHub Desktop.
#!/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