PerlNotesForProfessionals.pdf

(1086 KB) Pobierz
Notes for Professionals
Perl
Perl
Notes for Professionals
®
of professional hints and tricks
90+ pages
GoalKicker.com
Free Programming Books
Disclaimer
This is an unocial free book created for educational purposes and is
not aliated with ocial Perl
®
group(s) or company(s).
All trademarks and registered trademarks are
the property of their respective owners
Contents
About
................................................................................................................................................................................... 1
Chapter 1: Getting started with Perl Language
............................................................................................ 2
Section 1.1: Getting started with Perl
............................................................................................................................ 2
Chapter 2: Comments
................................................................................................................................................. 4
Section 2.1: Single-line comments
................................................................................................................................ 4
Section 2.2: Multi-line comments
................................................................................................................................. 4
Chapter 3: Variables
.................................................................................................................................................... 5
Section 3.1: Scalars
........................................................................................................................................................ 5
Section 3.2: Array References
...................................................................................................................................... 5
Section 3.3: Scalar References
..................................................................................................................................... 6
Section 3.4: Arrays
......................................................................................................................................................... 7
Section 3.5: Typeglobs, typeglob refs, filehandles and constants
........................................................................... 8
Section 3.6: Sigils
............................................................................................................................................................ 9
Section 3.7: Hash References
..................................................................................................................................... 10
Section 3.8: Hashes
..................................................................................................................................................... 12
Chapter 4: Interpolation in Perl
.......................................................................................................................... 15
Section 4.1: What is interpolated
................................................................................................................................ 15
Section 4.2: Basic interpolation
.................................................................................................................................. 16
Chapter 5: True and false
...................................................................................................................................... 18
Section 5.1: List of true and false values
................................................................................................................... 18
Chapter 6: Dates and Time
.................................................................................................................................... 19
Section 6.1: Date formatting
....................................................................................................................................... 19
Section 6.2: Create new DateTime
............................................................................................................................ 19
Section 6.3: Working with elements of datetime
..................................................................................................... 19
Section 6.4: Calculate code execution time
.............................................................................................................. 20
Chapter 7: Control Statements
........................................................................................................................... 21
Section 7.1: Conditionals
.............................................................................................................................................. 21
Section 7.2: Loops
....................................................................................................................................................... 21
Chapter 8: Subroutines
............................................................................................................................................ 23
Section 8.1: Creating subroutines
............................................................................................................................... 23
Section 8.2: Subroutines
............................................................................................................................................. 24
Section 8.3: Subroutine arguments are passed by reference (except those in signatures)
............................... 25
Chapter 9: Debug Output
....................................................................................................................................... 27
Section 9.1: Dumping with Style
................................................................................................................................. 27
Section 9.2: Dumping data-structures
...................................................................................................................... 28
Section 9.3: Data::Show
............................................................................................................................................... 28
Section 9.4: Dumping array list
.................................................................................................................................. 29
Chapter 10: Lists
.......................................................................................................................................................... 31
Section 10.1: Array as list
............................................................................................................................................. 31
Section 10.2: Assigning a list to a hash
...................................................................................................................... 31
Section 10.3: Lists can be passed into subroutines
.................................................................................................. 31
Section 10.4: Return list from subroutine
.................................................................................................................. 32
Section 10.5: Hash as list
............................................................................................................................................. 33
Section 10.6: Using arrayref to pass array to sub
................................................................................................... 33
Chapter 11: Sorting
..................................................................................................................................................... 34
Section 11.1: Basic Lexical Sort
.................................................................................................................................... 34
Section 11.2: The Schwartzian Transform
................................................................................................................. 34
Section 11.3: Case Insensitive Sort
.............................................................................................................................. 35
Section 11.4: Numeric Sort
........................................................................................................................................... 35
Section 11.5: Reverse Sort
........................................................................................................................................... 35
Chapter 12: File I/O (reading and writing files)
........................................................................................... 36
Section 12.1: Opening A FileHandle for Reading
...................................................................................................... 36
Section 12.2: Reading from a file
............................................................................................................................... 36
Section 12.3: Write to a file
.......................................................................................................................................... 37
Section 12.4: "use autodie" and you won't need to check file open/close failures
.............................................. 37
Section 12.5: Rewind a filehandle
............................................................................................................................... 38
Section 12.6: Reading and Writing gzip compressed files
....................................................................................... 38
Section 12.7: Setting the default Encoding for IO
..................................................................................................... 39
Chapter 13: Reading a file's content into a variable
................................................................................ 40
Section 13.1: Path::Tiny
................................................................................................................................................. 40
Section 13.2: The manual way
.................................................................................................................................... 40
Section 13.3: File::Slurp
................................................................................................................................................. 40
Section 13.4: File::Slurper
............................................................................................................................................. 41
Section 13.5: Slurping a file into an array variable
................................................................................................... 41
Section 13.6: Slurp file in one-liner
.............................................................................................................................. 41
Chapter 14: Strings and quoting methods
.................................................................................................... 42
Section 14.1: String Literal Quoting
............................................................................................................................. 42
Section 14.2: Double-quoting
...................................................................................................................................... 42
Section 14.3: Heredocs
................................................................................................................................................ 43
Section 14.4: Removing trailing newlines
.................................................................................................................. 44
Chapter 15: Split a string on unquoted separators
................................................................................... 46
Section 15.1: parse_line()
............................................................................................................................................ 46
Section 15.2: Text::CSV or Text::CSV_XS
.................................................................................................................... 46
Chapter 16: Object-oriented Perl
........................................................................................................................ 47
Section 16.1: Defining classes in modern Perl
........................................................................................................... 47
Section 16.2: Creating Objects
.................................................................................................................................... 47
Section 16.3: Defining Classes
.................................................................................................................................... 48
Section 16.4: Inheritance and methods resolution
................................................................................................... 49
Section 16.5: Class and Object Methods
.................................................................................................................... 51
Section 16.6: Roles
....................................................................................................................................................... 52
Chapter 17: Exception handling
........................................................................................................................... 54
Section 17.1: eval and die
............................................................................................................................................ 54
Chapter 18: Regular Expressions
........................................................................................................................ 55
Section 18.1: Replace a string using regular expressions
........................................................................................ 55
Section 18.2: Matching strings
.................................................................................................................................... 55
Section 18.3: Parsing a string with a regex
............................................................................................................... 55
Section 18.4: Usage of \Q and \E in pattern matching
........................................................................................... 56
Chapter 19: XML Parsing
.......................................................................................................................................... 57
Section 19.1: Parsing with XML::Twig
.......................................................................................................................... 57
Section 19.2: Consuming XML with XML::Rabbit
....................................................................................................... 58
Section 19.3: Parsing with XML::LibXML
..................................................................................................................... 60
Chapter 20: Unicode
.................................................................................................................................................. 62
Section 20.1: The utf8 pragma: using Unicode in your sources
............................................................................. 62
Section 20.2: Handling invalid UTF-8
........................................................................................................................ 62
Section 20.3: Command line switches for one-liners
............................................................................................... 63
Section 20.4: Standard I/O
......................................................................................................................................... 64
Section 20.5: File handles
........................................................................................................................................... 64
Section 20.6: Create filenames
.................................................................................................................................. 65
Section 20.7: Read filenames
..................................................................................................................................... 65
Chapter 21: Perl one-liners
..................................................................................................................................... 68
Section 21.1: Upload file into mojolicious
................................................................................................................... 68
Section 21.2: Execute some Perl code from command line
.................................................................................... 68
Section 21.3: Using double-quoted strings in Windows one-liners
......................................................................... 68
Section 21.4: Print lines matching a pattern (PCRE grep)
....................................................................................... 68
Section 21.5: Replace a substring with another (PCRE sed)
................................................................................... 69
Section 21.6: Print only certain fields
......................................................................................................................... 69
Section 21.7: Print lines 5 to 10
.................................................................................................................................... 69
Section 21.8: Edit file in-place
..................................................................................................................................... 69
Section 21.9: Reading the whole file as a string
....................................................................................................... 69
Chapter 22: Randomness
........................................................................................................................................ 70
Section 22.1: Accessing an array element at random
............................................................................................. 70
Section 22.2: Generate a random integer between 0 and 9
.................................................................................. 70
Chapter 23: Special variables
............................................................................................................................... 71
Section 23.1: Special variables in perl:
....................................................................................................................... 71
Chapter 24: Packages and modules
................................................................................................................. 72
Section 24.1: Using a module
..................................................................................................................................... 72
Section 24.2: Using a module inside a directory
...................................................................................................... 72
Section 24.3: Loading a module at runtime
.............................................................................................................. 73
Section 24.4: CPAN.pm
................................................................................................................................................ 73
Section 24.5: List all installed modules
...................................................................................................................... 74
Section 24.6: Executing the contents of another file
............................................................................................... 74
Chapter 25: Install Perl modules via CPAN
.................................................................................................... 75
Section 25.1: cpanminus, the lightweight configuration-free replacement for cpan
........................................... 75
Section 25.2: Installing modules manually
............................................................................................................... 75
Section 25.3: Run Perl CPAN in your terminal (Mac and Linux) or command prompt (Windows)
.................... 76
Chapter 26: Easy way to check installed modules on Mac and Ubuntu
......................................... 78
Section 26.1: Use perldoc to check the Perl package install path
.......................................................................... 78
Section 26.2: Check installed perl modules via terminal
......................................................................................... 78
Section 26.3: How to check Perl corelist modules
.................................................................................................... 78
Chapter 27: Pack and unpack
.............................................................................................................................. 79
Section 27.1: Manually Converting C Structs to Pack Syntax
.................................................................................. 79
Section 27.2: Constructing an IPv4 header
............................................................................................................... 80
Chapter 28: Perl commands for Windows Excel with Win32::OLE module
.................................... 82
Section 28.1: Opening and Saving Excel/Workbooks
.............................................................................................. 82
Section 28.2: Manipulation of Worksheets
............................................................................................................... 82
Section 28.3: Manipulation of cells
............................................................................................................................ 83
Section 28.4: Manipulation of Rows / Columns
....................................................................................................... 84
Chapter 29: Simple interaction with database via DBI module
.......................................................... 85
Section 29.1: DBI module
............................................................................................................................................ 85
Chapter 30: Perl Testing
......................................................................................................................................... 87
Section 30.1: Perl Unit Testing Example
.................................................................................................................... 87
Chapter 31: Dancer
..................................................................................................................................................... 89
Section 31.1: Easiest example
...................................................................................................................................... 89
Chapter 32: Attributed Text
................................................................................................................................... 90
Section 32.1: Printing colored Text
............................................................................................................................. 90
Chapter 33: GUI Applications in Perl
.................................................................................................................. 91
Section 33.1: GTK Application
..................................................................................................................................... 91
Chapter 34: Memory usage optimization
...................................................................................................... 92
Section 34.1: Reading files: foreach vs. while
............................................................................................................ 92
Section 34.2: Processing long lists
............................................................................................................................. 92
Chapter 35: Perl script debugging
..................................................................................................................... 93
Section 35.1: Run script in debug mode
.................................................................................................................... 93
Section 35.2: Use a nonstandard debugger
............................................................................................................. 93
Chapter 36: Perlbrew
................................................................................................................................................ 94
Section 36.1: Setup perlbrew for the first time
.......................................................................................................... 94
Chapter 37: Installation of Perl
........................................................................................................................... 95
Section 37.1: Linux
........................................................................................................................................................ 95
Section 37.2: OS X
........................................................................................................................................................ 95
Section 37.3: Windows
................................................................................................................................................. 96
Chapter 38: Compile Perl cpan module sapnwrfc from source code
.............................................. 97
Section 38.1: Simple example to test the RFC connection
....................................................................................... 97
Chapter 39: Best Practices
..................................................................................................................................... 98
Section 39.1: Using Perl::Critic
..................................................................................................................................... 98
Credits
............................................................................................................................................................................ 102
You may also like
...................................................................................................................................................... 104
Zgłoś jeśli naruszono regulamin