← Perl EnglishChapter 01 of 13

Introduction to Perl

## Learning Objectives - Understand Perl history and philosophy - Set up Perl development environment - Write and run your first Perl program - Understand Perl syntax basics ## What is Perl? Perl is a high-level, interpreted programming language originally developed by Larry Wall in 1987 as a Unix scripting language for text processing. ```perl print "Hello, World!\n"; ``` ## Key Features - **Text Processing**: Built-in regular expressions and pattern matching - **Practical Extraction**: Excellent for report generation and data manipulation - **Cross-Platform**: Runs on Unix, Linux, Windows, macOS - **Dynamic Typing**: No variable type declarations required - **Interpreted**: No compilation step needed - **CGI Programming**: Historically popular for web development ## Installing Perl ### Linux/macOS Perl is usually pre-installed. Verify: ```bash perl --version ``` ### Windows Download from or ### Verify Installation ```bash perl --version ``` ## Your First Program ### Hello World ```perl #!/usr/bin/env perl use strict; use warnings; print "Hello, World!\n"; ``` ### Running Perl ```bash perl hello.pl # Run as script perl -e 'print "Hi"' # Run one-liner ``` ## Perl Program Structure ```perl #!/usr/bin/env perl use strict; # Enforce variable declarations use warnings; # Enable helpful warnings # Main code here print "Content-Type: text/plain\n\n"; print "Hello!\n"; ``` ## Comments ```perl # Single-line comment =pod Multi-line comment Using POD syntax =cut print "Hello"; # End-of-line comment ``` ## print Function ```perl # Print with newline print "Hello\n"; # Print multiple items print "Name: ", "Alice", "\n"; # Print without newline print "Loading "; print "complete"; ``` ## Scalar Variables ```perl use strict; use warnings; my $name = "Alice"; my $age = 30; my $price = 19.99; print "Name: $name, Age: $age\n"; print 'Single quotes: $name\n'; # No interpolation ``` ## Command Line Arguments ```perl #!/usr/bin/env perl use strict; use warnings; my $name = $ARGV[0] // "World"; print "Hello, $name!\n"; ``` ```bash perl hello.pl Alice # Output: Hello, Alice! ``` ## IDEs and Editors ### Popular Editors - **VS Code** with Perl extension - **Padre** - Perl IDE - **Emacs** with perl-mode - **Vim** with perl-support ## Perl Versions ```bash perl --version # Check installed version ``` | Version | Year | Feature | |---------|------|---------| | Perl 5 | 1994 | Main development branch | | Perl 5.8 | 2002 | Unicode improvements | | Perl 5.10 | 2007 | Smart match operator | | Perl 5.14 | 2011 | Unicode 6.0 | | Perl 5.16 | 2012 | Threading improvements | | Perl 5.24 | 2015 | Performance improvements | | Perl 5.30 | 2019 | New features | | Perl 5.34 | 2021 | Modern syntax | | Perl 5.36 | 2022 | New features | ## One-Liners ```bash perl -e 'print "Hello\n"' # Print perl -n -e 'print $_' file.txt # Print lines perl -p -e 's/foo/bar/' file.txt # Replace in place perl -0777 -e 's/foo/bar/sg' file # Slurp file ``` ## Special Variables ```perl $_ # Default variable $. # Current line number $/ # Input record separator $\ # Output record separator $! # System error $$ # Process ID @ARGV # Command line arguments %ENV # Environment variables ``` ## Summary - Perl is a text-processing language with powerful regex support - Use `use strict` and `use warnings` in every program - `print` outputs to stdout - Variables start with `$` (scalars), `@` (arrays), `%` (hashes) - `perl` command runs scripts; `-e` flag for one-liners - Comments use `#` for single line

Comments

Comments powered by Giscus

To enable comments, add your Giscus embed code here.

Learn more about Giscus →