Variable Types
Perl Variable Types Table
Var Type |
Leading Character |
Example |
Description |
Scalar |
$ |
$day |
Single value number or string |
Array |
@ |
@weekdays |
List of numbers or strings keyed by number |
Hash |
% |
%months |
List of numbers or strings keyed by string |
Subroutine |
& |
&DoSort |
A callable procedure or function |
Typeglob |
* |
*count |
Everything called count |
Variable Examples
@weekdays = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
print @weekdays;
$arraysize = scalar(@weekdays);
for($i = 0; $i < $arraysize; $i ++) {
printf("\n%d: @weekdays[$i]", $i);
}
printf("\n\n");
foreach my $j (0..($arraysize-1)) {
print "\n$j: @weekdays[$j]";
last if ($j > 3);
}
print "\n\n";
$day = $weekdays[2];
print "$day\n";
%months1 = ("First", "January", "Second", "February", "Third", "March", "Fourth", "April", "Fifth", "May", "Sixth", "June", "Seventh", "July", "Eighth", "August", "Ninth", "September", "Tenth", "October", "Eleventh", "November", "Twelfth", "December");
$m1 = $months1{"Ninth"};
print "\n$m1\n\n";
%months2 = (
"First" => "January",
  "Second" => "February",
 "Third" => "March",
 "Fourth" => "April",
 "Fifth" => "May",
 "Sixth" => "June",
 "Seventh" => "July",
 "Eighth" => "August",
 "Ninth" => "September",
 "Tenth" => "October",
 "Eleventh" => "November",
 "Twelfth" => "December",
);
$m2 = $months2{"Ninth"};
print "$m2\n\n";
@vals = values %months2;
@keys = keys %months2;
print "\nValues: @vals\n";
print "\nKeys: @keys\n";
Back to Top
Match & Replace Optional Modifiers
Modifier |
Function |
c |
Prepares (with g) for continuation after failed \g match |
g |
Matches as many times as possible |
i |
Search is case insensitive |
o |
Interpolates variable only once |
m |
Treats string as multiple lines; ^ and $ will match at newline characters \n |
s |
Treats the string as a single line; . will match newline characters |
x |
Allows for white space and comments |
Back to Top
Perl Basic Metacharacters
The Perl Metacharacters, which are used in the "pattern" section of the m/pattern/ and s/pattern/replacement/ operators, involve the following characters:
+ ? . * ^ $ ( ) [ { | \
Patterns are interpolated as double quoted strings. The standard string escapes have their usual interpolated meanings except for \b, which matches word boundries. Although in a character class, \b goes back to matching a backspace.
Basic Perl Metacharaters Used In Search/Replace Patterns
Symbol |
Meaning |
. |
Matches any character except for newlines. In single line mode (/s), matches newlines as well. |
(...) |
Groups a series of pattern elements into a single element. The string of the group is captured into $1, $2, etc. corresponding to the left ( of the respective () pairs. |
^ |
Matches the beginning of the element. In multiline mode (/m), matches after every newline character. |
$ |
Matches the end of the line, or before a final newline character. Also matches before every newline charater in multiline mode (/m). |
[...] |
Specifies a Character Class. [^...] negates the Class. |
..|..|.. |
Matches alternatives from left to right until one succeeds. |
* |
Match 0 or more times (match as many as possible). |
? |
Match 0 or 1 times (match as many as possible). |
+ |
Match 1 or more times (match as many as possible). |
{num} |
Match exactly num times. |
{min,} |
Match at least min times (match as many as possible). |
{min,max} |
Match at least min times, but not more than max times (match as many as possible). |
*? |
Match 0 or more times (match as few as possible). |
?? |
Match 0 or 1 times (match as few as possible). |
+? |
Match 1 or more times (match as few as possible). |
{min,}? |
Match at least min times (match as few as possible). |
{min,max}? |
Match at least min times, but not more than max times (match as few as possible). |
Back to Top
Perl Special Metacharacters
The special meaning of non-alphanumeric characters can be escaped with a \. However, most alphanumeric characters gain a specail meaning with the \.
Special Perl Metacharaters Used In Search/Replace Patterns
Symbol |
Meaning |
Meaning Applies to Character Classes |
\0 |
Match the Null character (NUL). |
Yes |
\NNN |
Match the character given by NNN in Octal up to \377. |
Yes |
\N |
Match Nth previously captured string. |
Yes |
\x{NNNN} |
Match the character given by NNNN in hexadecimal. |
Yes |
\a |
Match the alarm character (BEL). |
Yes |
\A |
Match at the beginning of a string. |
No |
\b |
Match at a word boundry (except in a charater class, in which \b goes back to matching a backspace). |
Different Meaning |
\B |
Match when not at a work boundry. |
No |
\cX |
Match the control character Control-X, where X is a keyboard character. |
Yes |
\C |
Match one byte. |
No |
\d |
Match any digit character 0-9. |
Yes |
\D |
Match any non-digit character. |
Yes |
\e |
Match the ASCII Escape character ESC. |
Yes |
\E |
End lower case \L or upper case \U character, or metaquote \Q translation. |
No |
\f |
Match the Form Feed character ESC. |
Yes |
\G |
Match at end-of-match position of previous m//g. |
No |
\l |
Lower case the next charater only. |
No |
\L |
Lower case until \E. |
No |
\n |
Match the newline character NL. |
Yes |
\Q |
Quote metacharacters until \E. |
No |
\r |
Match the Return character CR. |
Yes |
\s |
Match any whitespace character. |
Yes |
\S |
Match any non-whitespace character. |
Yes |
\t |
Match the tab character HT. |
Yes |
\u |
Title case next character only. |
No |
\U |
Upper case until \E. |
No |
\w |
Match any alphanumeric word character plus "_". |
Yes |
\W |
Match any non-word character. |
Yes |
\z |
Match at end of string only. |
No |
\Z |
Match at end of string, or at optional newline. |
No |
Back to Top
Standard Perl Charater Class Metasymbols
Metasymbol/Character Class Equivalence Table
Metasymbol |
Description |
Character Class |
\d |
Digit |
[0-9] |
\D |
Non-digit |
[^0-9] |
\s |
Whitespace |
[ \t\n\r\f] |
\S |
Non-Whitespace |
[^ \t\n\r\f] |
\w |
Alpha-Numeric Word Characters |
[a-zA-Z0-9_] |
\W |
Non Alpha-Numeric Word Characters |
[^a-zA-Z0-9_] |
Back to Top
Perl Special Variables
Perl Special Variables Table (This Is Not A Complete List)
Scalar Variable |
Alternative |
Description |
$_ |
$ARG |
Default Input, Output, and Pattern Matching Variable |
$. |
$INPUT_LINE_NUMBER |
Current Input Line Number of Last File Handle that was Read From |
$/ |
$INPUT_RECORD_SEPARATOR |
String Separating Input Records, Default: Newline |
$! |
$OS_ERROR |
Information About Current Error |
$0 |
$PROGRAM_NAME |
Name of File Being Executed |
$^V |
$PERL_VERSION |
Version of Perl Being Used |
$& |
$MATCH |
String that matched after using the match operator: expr =~ m/pattern/ |
$` |
$PREMATCH |
String preceeding the match after using the match operator: expr =~ m/pattern/ |
$' |
$POSTMATCH |
String following the match after using the match operator: expr =~ m/pattern/ |
$+ |
$LAST_PAREN_MATCH |
Last sub-expression that matched after using the match operator: expr =~ m/pattern/ |
$1,$2,etc. |
None |
String of Subpatterns Corresponding to the Left '(' of the Respective () Pairs, the Subpattern is Captured Into $1, $2, etc. |
Array Variable |
Alternative |
Description |
@ARGV |
None |
Contains Command Line Arguments Entered When the Script Was Executed |
@_ |
@ARG |
Parameter Array for Subroutines |
@- |
@LAST_MATCH_START |
Contains Offsets From the Beginning for Successful Sub-Matches |
@+ |
@LAST_MATCH_END |
Contains Offsets From the End for Successful Sub-Matches |
Back to Top
General Examples
open(TEXTFILE, "file.txt") or die "Cannot open file: $!\n";
open(TEXTFILE, "<file.txt") or die "Cannot open file: $!\n";
open(TEXTFILE, ">file.txt") or die "Cannot open file: $!\n";
open(TEXTFILE, ">>file.txt") or die "Cannot open file: $!\n";
while ($line = <TEXTFILE>) {
...
...
...
}
Back to Top
use strict;
use warnings;
my($file_old, $file_new, $donotprint) = @ARGV;
die "$file_old name must have at least three chars, $!" if length($file_old) < 3;
die "$file_new name must have at least three chars, $!" if length($file_new) < 3;
open (IN, "<$file_old") or die "Can't open $file_old, $!";
open (OUT, ">$file_new") or die "Can't open $file_new, $!";
while(my $line = <IN>) {
if($line !~ m/$donotprint\s+?/o) {
  print "At Line Number $.: $line";
  print OUT "$line";
}
else {
  print "At Line Number $.: Won't print to new file->\"$donotprint\" Match Offset: @-\n"
  }
}
close IN;
close OUT;
Back to Top
use strict;
use warnings;
&hello_sub ( @ARGV );
sub hello_sub {
my $name = shift @_;
print "Hello, $name\n";
while ( @_ ) {
  $name = shift @_;
  print "Hello again, $name\n";
  }
}
Back to Top
use strict;
use warnings;
my $file_in = $ARGV[0];
die "$file_in must have at least three chars, $!" if length($file_in) < 3;
open (IN, "<$file_in") or die "Can't open $file_in, $!";
my @strings;
print"$file_in\n";
print"Impedance,Voltage, Current\n";
while(my $line = <IN>) {
@strings = split(/\s+/, $line);
my $value = defined($strings[1]) ? $strings[1] : "";
if($value eq "A"){
  print "$strings[3],$strings[4],$strings[5]\n";
}
}
close IN;
Back to Top
use strict;
use warnings;
my $file_in = $ARGV[0];
die "$file_in must have at least three chars, $!" if length($file_in) < 3;
open (IN, "<$file_in") or die "Can't open $file_in, $!";
my $string;
while(my $line = <IN>) {
$line =~ m/.{8}$/g;
$string = $&;
print "$&\n";
}
close IN;
Back to Top
References
Back to Top
|