Perl Notes
Some Notes on Perl scripting are given below.
The
Perl Home Page is a useful starting point for further Perl studies
Page Listing:
- Perl for Windows
- Regular Expressions
- Simple Search
- Simple Search and Replace
- Split Command
- List Files In Directory
- Import CSV file into MySQL DB
- SQL Select Statement Example
Perl for Windows
You can download Active Perl for Windows. Just follow the installation prompts.
To ensure it's installed correctly, open a Command Prompt and type:
      perl -v
Information about the Perl verion should be displayed
If not, ensure the PATH to the Perl directory is included in your PATH environment variable
      Go to -> Start -> Control Panel -> System and Security -> System -> Advanced System Settings ->
      Environment Variables
      and append the path to the Perl directory to the end of the PATH environment variable, And Try:
            perl -v     again
Eclipse is a very good text editing environment for creating and testing Perl scripts. Install Eclipse Classic.
Then follow the instructions for installing:-
EPIC - Eclipse Perl Integration
In windows 7, Perl packages (or modules) can be added by:
      Go to -> Start -> "in the "Search Programs and Files" edit box, Type Perl
      And select "Perl Package Manager" -> browse for the desired package
* Home
Regular Expressions
A regular expression is a pattern of characters that can be matched against when searching for content within strings. This task can be done in many scripting environments - most notably within Perl, UNIX scripting or as part of UNIX commands such as grep. sed and awk.
Also see online Regular Expression Library.
A small sample of commonly used operators that can be used within regular expressions, in Perl, are given below.
\b |
Boundary for a word |
\d |
Matches one digit |
\s |
Matches white space |
\S |
A capital "S" - match non white space |
\w |
Matches one alpha-numeric characters of a word |
[A-Z]{2,} |
2 or more upper case characters |
[A-Z]{2,6} |
2 to 6 of upper case characters |
[0-9] |
Match any number |
[^0-9] |
Match anything EXCEPT the specified characters |
x{5,} |
Five or more x's |
x{5} |
Five x's |
^ |
Anchors to the beginning of a string |
$ |
Anchors to the end of a string |
* |
ZERO or more of the immediately previous character |
+ |
ONE or more of the immediately previous character |
. |
Matches any single character except new line |
? |
Match ZERO or One of the immediately previous character |
*? |
Match ZERO or more of the immediately previous character - few as possible |
+? |
Match ONE or more of the immediately previous character - few as possible |
* Home
Simple Search
This script searches a file for text (in this example the word (dog), and if found, outputs the line containing the text to a new file :
This script searches a file for text (in this example the word (dog), and if found, outputs the search results (dog) to a new file. Note the inner brackets ( ) around the search criteria.
* Home
Simple Search and Replace
* Home
Split Command
An example of using the split command to place the elements of a "comma seperated values" (csv) file into the elements of an array
* Home
List Files In Directory
This example includes two subroutines. The subroutine "main" calls the subroutine "get_files", and prints a list of files with the file extension of ".txt" from the provided directory
* Home
Import CSV file into MySQL DB
An example of reading the rows from a CSV file and inserting them into a MySQL database table
* Home
SQL Select Statement Example
An example of querying a database table