1. Trang chủ
  2. » Công Nghệ Thông Tin

Peter krumins - perl one liners

168 136 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

Đây là quyển sách tiếng anh về lĩnh vực công nghệ thông tin cho sinh viên và những ai có đam mê. Quyển sách này trình về lý thuyết ,phương pháp lập trình cho ngôn ngữ C và C++.

ADVANCE PRAISE FOR Perl One-liners “One of the slogans used by Perl is ‘Easy things should be easy and hard things should be possible.’ This book illustrates just how easy things can be— and how much can be done with so little code.” —DaviD Precious, contributor to the Perl Dancer Project anD various cPan moDules “By reading this book you can make a step toward becoming the local computer wizard, even without learning how to program.” —Gabor szabo, founDer anD eDitor of the Perl Weekly newsletter “A set of exercises for deepening your understanding of Perl.” —john D. cook, sinGular value consultinG “The author is enthusiastic about the material and uses an easy writing style. Highly recommended.” —thriG ( jeremy mates), internet Plumber “These one-liners are great. Simple. Clear. Concise.” —jonathan scott Duff, Perl Guru “A quick read full of useful command-line Perl programs.” —chris feDDe, systems enGineer anD Perl enthusiast “Handy for anyone who does a lot of one-off text processing: system administrators, coders, or anyone with large amounts of data they need shifted, filtered, or interpreted.” —jim Davis, Perl DeveloPer [...]... filename as the last argument to the one- liners When I do that, the one- liners operate on the contents of that file If I didn’t pass a filename to the one- liners, they’d operate on the data from the standard input From now on I won’t specify a file at the end of the one- liners, but you can always add it back if you want to run the one- liners on files When writing one- liners, it’s a good idea to quickly... least one nonwhitespace character is double-spaced 2.3 Triple-space a file You can also triple-space a file simply by outputting more newlines at the end of each line: perl -pe '$\ = "\n\n"' Or perl -pe '$_ = "\n\n"' Or perl -pe 's/$/\n\n/' These one- liners are similar to the first one- liner in this chapter, except that two newlines are appended after each line Spacing 11 2.4 N-space a file perl -pe... B Using Perl One- Liners on Windows B.1 B.2 78 79 79 79 80 80 80 81 81 105 Perl on Windows 105 Bash on Windows 106 Contents in Detail B.3 B.4 B.5 Perl One- Liners in Windows Bash Perl One- Liners in the Windows Command Prompt Converting One- Liners. .. chapter, we look at various one- liners that change line and word spacing, performing such tasks as double- and triple-spacing lines in a file, removing blank lines, and double-spacing words You’ll also learn about various command-line arguments, such as -p, -e, -n, and special variables, such as $_ and $\ 2.1 Double-space a file perl -pe '$\ = "\n"' file This one- liner double-spaces a file I need to explain... you’re sure the one- liner works, you can pass one or more filenames at the end Again, don’t forget about Perl s handy documentation system, perldoc Just type perldoc perlrun at the command line to display information about how to run Perl and all the command-line arguments 10 Chapter 2 2.2 Double-space a file, excluding the blank lines perl -pe '$_ = "\n" unless /^$/' This one- liner double-spaces all lines... which explains variables; perldoc perlop, which explains operators; and perldoc perlfunc, which explains functions Perl one- liners let you accomplish many tasks quickly You’ll find over 130 one- liners in this book Read them, try them, and soon enough you’ll be the local shell wizard ( Just don’t tell your friends—unless you want competition.) Enjoy! Introduction to Perl One- Liners 5 2 Sp a c i n g In... run one- liners on whole files You can also combine the previous two one- liners to create one that numbers only the repeated lines: perl -ne 'print "$ $_" if $a{$_}++' Another thing you can do is sum the numbers in each line using the sum function from the List::Util CPAN module CPAN (Comprehensive Perl Archive Network; http://www.cpan.org/) is an archive of over 100,000 Introduction to Perl One- Liners. .. In this one- liner, the code is print unless /^$/, so the entire Perl program becomes while () { print unless /^$/ } Unraveling this a bit further, you get this: while () { print $_ unless $_ =~ /^$/ } This one- liner prints all nonblank lines (You saw the /^$/ regular expression in one- liner 2.2 on page 11.) This one- liner also removes all blank lines: perl -lne 'print if length' This one- liner uses... they’re triple-spaced 16 Chapter 2 3 Numbering In this chapter, we’ll look at various one- liners for numbering lines and words, and you’ll get to know the $ special variable You’ll also learn about Perl golfing, a “sport” that involves writing the shortest Perl program to get a task done 3.1 Number all lines in a file perl -pe '$_ = "$ $_"' As I explained in one- liner 2.1 (page 7), -p tells Perl to assume... you’re running Perl 5.10 or later, you can use the say operator The say operator acts much like print, but it always adds a newline at the end of the line In Perl 5.10, this same one- liner can be written like this: perl -nE 'say' file 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 The -n argument . Cataloging-in-Publication Data Krumins, Peteris. Perl one-liners : 130 programs that get things done / by Peteris Krumins. pages cm Summary: "Snappy Perl. USA First printing 17 16 15 14 13 1 2 3 4 5 6 7 8 9 ISBN-10: 1-5 932 7-5 20-X ISBN-13: 97 8-1 -5 932 7-5 2 0-4 Publisher: William Pollock Production Editor: Riley

Ngày đăng: 19/03/2014, 14:12

w