Perl One-Liners Quotes
Perl One-Liners: 130 Programs That Get Things Done
by
Peteris Krumins35 ratings, 3.89 average rating, 2 reviews
Open Preview
Perl One-Liners Quotes
Showing 1-8 of 8
“Check whether a word appears in a string twice /(word).*\1/ This regular expression matches a word followed by something or nothing at all, followed by the same word. Here, (word) captures the word in group 1, and \1 refers to the contents of group 1, which is the same as writing /(word).*word/. For example, silly things are silly matches /(silly).*\1/, but silly things are boring doesn’t because silly is not repeated in the string.”
― Perl One-Liners: 130 Programs That Get Things Done
― Perl One-Liners: 130 Programs That Get Things Done
“For example, to print the lines 13, 19, 88, 290, and 999, you do this: perl -ne 'print if $. == 13 || $. == 19 || $. == 88 || $. == 290 || $. == 999' If you want to print more lines, you can put them in a separate array and then test whether $. is in this array: perl -ne ' @lines = (13, 19, 88, 290, 999, 1400, 2000); print if grep { $_ == $. } @lines ' This one-liner uses grep to test if the current line $. is in the @lines array. If the current line number is found in the @lines array, the grep function returns a list of one element that contains the current line number and this list evaluates to true.”
― Perl One-Liners: 130 Programs That Get Things Done
― Perl One-Liners: 130 Programs That Get Things Done
“To base64-encode the whole file, use this: perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' file Here, the -0777 argument together with -n causes Perl to slurp the whole file into the $_ variable. Next, the file is base64-encoded and printed. (If Perl didn’t slurp the entire file, it would be encoded line by line, and you’d end up with a mess.)”
― Perl One-Liners: 130 Programs That Get Things Done
― Perl One-Liners: 130 Programs That Get Things Done
“Calculate the factorial perl -MMath::BigInt -le 'print Math::BigInt->new(5)->bfac()' This one-liner uses the bfac() function from the Math::BigInt module in the Perl core. (In other words, you don’t need to install it.) The Math::BigInt->new(5) construction creates a new Math::BigInt object with a value of 5, after which the bfac() method is called on the newly created object to calculate the factorial of 5.”
― Perl One-Liners: 130 Programs That Get Things Done
― Perl One-Liners: 130 Programs That Get Things Done
“$. contains the current line number of the input. The result is that each line has its line number prepended.”
― Perl One-Liners: 130 Programs That Get Things Done
― Perl One-Liners: 130 Programs That Get Things Done
“perl -lpe 's// /g' Here you match seemingly nothing and replace it with a single space. The nothingness actually means “match between characters,” with the result that you insert a space between all characters. (The matching includes the beginning and end of the text.)”
― Perl One-Liners: 130 Programs That Get Things Done
― Perl One-Liners: 130 Programs That Get Things Done
“perl -00 -pe '' This one-liner is really tricky, isn’t it? First, it doesn’t have any code! The -e is empty. Next, it has a silly -00 command-line option that turns paragraph slurp mode on, meaning Perl reads text paragraph by paragraph, rather than line by line. (A paragraph is text between two or more newlines.) The paragraph is put into $_, and the -p option prints it out.”
― Perl One-Liners: 130 Programs That Get Things Done
― Perl One-Liners: 130 Programs That Get Things Done
“The -E command-line argument works exactly the same way as the -e command-line argument, but it also enables Perl 5.10 features, including the say operator.”
― Perl One-Liners: 130 Programs That Get Things Done
― Perl One-Liners: 130 Programs That Get Things Done
