my $x = "foo"; # This is a Perl assignment
print $x, "\n"; # Print out "foo" and newline

$x = "foo; # INVALID: a " character is missing

#!/usr/bin/perl
use warnings;
use strict;
print "Hello World\n";

Semicolon seems to be missing
syntax error

#!/usr/bin/perl
use strict; # important pragma
use warnings; # another important pragma
print "What is your username? "; # print out the question
my $username; # "declare" the variable
$username = <STDIN>; # ask for the username
chomp($username); # remove "new line"
print "Hello, $username.\n"; # print out the greeting
# Now we have said hello to our user

'i\o'; # The string 'i\o'

'xxx\'xxx'; # xxx, a single-quote character, and then xxx

'I don\'t think so.'; # Note the ' inside is escaped with \
'Need a \\ (backslash) or \?'; # The \\ gives us \, as does \
'You can do this: \\'; # A single backslash at the end
'Three \\\'s: "\\\\\"'; # There are three \ chars between ""

'Time to
start anew.'; # Represents the single string composed of:
# 'Time to' followed by a newline, followed by
# 'start anew.'

'You cannot do this: \'; # INVALID: the ending \ cannot be alone
'It is 5 o'clock!' # INVALID: the ' in o'clock should be escaped
'Three \'s: \\\\\'; # INVALID: the final \ escapes the ', thus
# the literal is not terminated
'This is my string; # INVALID: missing close quote

(Might be a runaway multi-line '' string starting on line X)
Bareword found where operator expected
Bareword "foo" not allowed while "strict subs" in use

#!/usr/bin/perl
use strict;
use warnings;
'Three \\\'s: "\\\\\"'; # There are three \ chars between ""
'xxx\'xxx'; # xxx, a single-quote character, and then xxx
'Time to
start anew.';

#!/usr/bin/perl
use strict;
use warnings;
print 'Three \\\'s: "\\\\\"'; # Print first string
print 'xxx\'xxx'; # Print the second
print 'Time to
start anew.
'; # Print last string, with a newline at the end

Three \'s: "\\\"xxx'xxxTime to
start anew.

#!/usr/bin/perl
use strict;
use warnings;
print "A backslash: \\\n";
print "Tab follows:\tover here\n";
print "Ring! \a\n";
print "Please pay bkuhn\@ebb.org \$20.\n";

A backslash: \
Tab follows: over here
Ring!
Please pay bkuhn@ebb.org $20.

#!/usr/bin/perl
use strict;
use warnings;
print "A backslash: \134\n";
print "Tab follows:\11over here\n";
print "Ring! \7\n";
print "Please pay bkuhn\100ebb.org \04420.\n";

#!/usr/bin/perl
use strict;
use warnings;
print "A backslash: \x5C\n";
print "Tab follows:\x09over here\n";
print "Ring! \x07\n";
print "Please pay bkuhn\x40ebb.org \x2420.\n";

42; # The number 42
12.5; # A floating point number, twelve and a half
101873.000; # 101,873
.005 # five thousandths
5E-3; # same number as previous line
23e-100; # 23 times 10 to the power of -100 (very small)
2.3E-99; # The same number as the line above!
23e6; # 23,000,000
23_000_000; # The same number as line above
# The underscores are for readability only

#!/usr/bin/perl
use strict;
use warnings;
print 2E-4, ' ', 9.77E-5, " ", 100.00, " ", 10_181_973, ' ', 9.87E9,
" ", 86.7E14, "\n";

0.0002 9.77e-05 100 10181973 9870000000 8.67e+15

use strict;
my $stuff = "My data"; # Assigns "My data" to variable $stuff
$stuff = 3.5e-4; # $stuff is no longer set to "My data";
# it is now 0.00035
my $things = $stuff; # $things is now 0.00035, also.

use strict;
my $friend = 'Joe';
my $greeting = "Howdy, $friend!";
# $greeting contains "Howdy, Joe!"
my $cost = 20.52;
my $statement = "Please pay \$$cost.\n";
# $statement contains "Please pay $20.52.\n"
my $debt = "$greeting $statement";
# $debt contains "Howdy, Joe! Please pay $20.52.\n"

#!/usr/bin/perl
use strict;
use warnings;
my $owner = 'Elizabeth';
my $dog = 'Rex';
my $amount = 12.5;
my $what = 'dog food';
print "${owner}'s dog, $dog, ate $amount pounds of $what.\n";

Elizabeth's dog, Rex, ate 12.5 pounds of dog food.

use strict;
my $this_data = "Something";
my $that_data = "Something Else ";
print "_$this_data_, or $that_datawill do\n"; # INVALID: actually refers
# to the scalars $this_data_
# and $that_datawill
print "_${this_data}_, or ${that_data}will do\n";
# CORRECT: refers to $this_data and $that_data,
# using curly braces to make it clear

use strict;
my $sweetNothing;

#!/usr/bin/perl
use strict;
use warnings;
my $hasValue = "Hello";
my $hasNoValue;
print "$hasValue $hasNoValue\n";

Use of uninitialized value at line 8.
Hello

#!/usr/bin/perl
use strict;
use warnings;
my $startUndefined;
my $startDefined = "This one is defined";
print "defined \$startUndefined == ",
defined $startUndefined,
", defined \$startDefined == ",
defined $startDefined, "\n";
$startUndefined = $startDefined;
$startDefined = undef;
print "defined \$startUndefined == ",
defined $startUndefined,
", defined \$startDefined == ",
defined $startDefined, "\n";

defined $startUndefined == , defined $startDefined == 1
defined $startUndefined == 1, defined $startDefined ==

use strict;
my $x = 5 * 2 + 3; # $x is 13
my $y = 2 * $x / 4; # $y is 6.5
my $z = (2 ** 6) ** 2; # $z is 4096
my $a = ($z - 96) * 2; # $a is 8000
my $b = $x % 5; # 3, 13 modulo 5

use strict;
my $a = 5; my $b = 500;
$a < $b; # evaluates to 1
$a >= $b; # evaluates to ""
$a <=> $b; # evaluates to -1
my $c = "hello"; my $d = "there";
$d cmp $c; # evaluates to 1
$d ge $c; # evaluates to 1
$c cmp "hello"; # evaluates to ""

use strict;
my $abc = 5;
my $efg = $abc-- + 5; # $abc is now 4, but $efg is 10
my $hij = ++$efg - --$abc; # $efg is 11, $abc is 3, $hij is 8

use strict;
my $greet = "Hi! ";
my $longGreet = $greet x 3; # $longGreet is "Hi! Hi! Hi! "
my $hi = $longGreet . "Paul."; # $hi is "Hi! Hi! Hi! Paul."

use strict;
my $greet = "Hi! ";
$greet .= "Everyone\n";
$greet = $greet . "Everyone\n"; # Does the same operation
# as the line above

use strict;
my $str = "Howdy, ";
my $name = "Joe.\n";
print $str, $name; # Prints out: Howdy, Joe.<NEWLINE>
my $f = 3e-1;
printf "%2.3f\n", $f; # Prints out: 0.300<NEWLINE>

(); # this list has no elements; the empty list
qw//; # another empty list
("a", "b", "c",
1, 2, 3); # a list with six elements
qw/hello world
how are you today/; # another list with six elements

(1 .. 100); # a list of 100 elements: the numbers from 1 to 100
('A' .. 'Z'); # a list of 26 elements: the uppercase letters From A to Z
('01' .. '31'); # a list of 31 elements: all possible days of a month
# with leading zeros on the single digit days

use strict;
my @stuff = qw/a b c/; # @stuff a three element list
my @things = (1, 2, 3, 4); # @things is a four element list
my $oneThing = "all alone";
my @allOfIt = (@stuff, $oneThing,
@things); # @allOfIt has 8 elements!

use strict;
my @someStuff = qw/Hello and
welcome/; # @someStuff: an array of 3 elements
$#someStuff = 0; # @someStuff now is simply ("Hello")
$someStuff[1] = "Joe"; # Now @someStuff is ("Hello", "Joe")
$#someStuff = -1; # @someStuff is now empty
@someStuff = (); # does same thing as previous line

use strict;
my @stuff = qw/everybody wants a rock/;
my @rock = @stuff[1 .. $#stuff]; # @rock is qw/wants a rock/
my @want = @stuff[ 0 .. 1]; # @want is qw/everybody wants/
@rock = @stuff[0, $#stuff]; # @rock is qw/everybody rock/

use strict;
my @stack;
push(@stack, 7, 6, "go"); # @stack is now qw/7 6 go/
my $action = pop @stack; # $action is "go", @stack is (7, 6)
my $value = pop(@stack) +
pop(@stack); # value is 6 + 7 = 13, @stack is empty

use strict;
my @queue;
unshift (@queue, "Customer 1"); # @queue is now ("Customer 1")
unshift (@queue, "Customer 2"); # @queue is now ("Customer 2" "Customer 1")
unshift (@queue, "Customer 3");
# @queue is now ("Customer 3" "Customer 2" "Customer 1")
my $item = pop(@queue); # @queue is now ("Customer 3" "Customer 2")
print "Servicing $item\n"; # prints: Servicing Customer 1\n
$item = pop(@queue); # @queue is now ("Customer 3")
print "Servicing $item\n"; # prints: Servicing Customer 2\n

use strict;
my @notAqueue;
unshift(@notAqueue, "Customer 0", "Customer 1");
# @queue is now ("Customer 0", "Customer 1")
unshift (@notAqueue, "Customer 2");
# @queue is now ("Customer 2", "Customer 0", "Customer 1")

use strict;
my @things = qw/a few of my favorite/;
my $count = @things; # $count is 5
my @moreThings = @things; # @moreThings is same as @things

use strict;
my @saying = qw/these are a few of my favorite/;
my $statement = "@saying things.\n";
# $statement is "these are a few of my favorite things.\n"
my $stuff = "@saying[0 .. 1] @saying[$#saying - 1, $#saying] things.\n"
# $stuff is "these are my favorite things.\n"

use strict;
{
my $var;
Statement;
Statement;
Statement;
}

use strict;
if (expression) {
Expression_True_Statement;
Expression_True_Statement;
Expression_True_Statement;
} elsif (another_expression) {
Expression_Elseif_Statement;
Expression_Elseif_Statement;
Expression_Elseif_Statement;
} else {
Else_Statement;
Else_Statement;
Else_Statement;
}

use strict;
while (expression) {
While_Statement;
While_Statement;
While_Statement;
}

use strict;
do {
DoWhile_Statement;
DoWhile_Statement;
DoWhile_Statement;
} while (expression);

use strict;
for(Initial_Statement; expression; Increment_Statement) {
For_Statement;
For_Statement;
For_Statement;
}

use strict;
Initial_Statement;
while (expression) {
For_Statement;
For_Statement;
For_Statement;
Increment_Statement;
}

use strict;
my @collection = qw/hat shoes shirts shorts/;
foreach my $item (@collection) {
print "$item\n";
}

use strict;
my %table;
$table{'schmoe'} = 'joe';
$table{7.5} = 2.6;

print "$table{'schmoe'}\n"; # outputs "joe\n"
--$table{7.5}; # $table{7.5} now contains 1.6

my @tableListed = %table; # @tableListed is qw/schmoe joe 7.5 1.6/

use strict;
my %table = qw/schmoe joe 7.5 1.6/;

use strict;
my %table = qw/schmoe joe smith john simpson bart/;
my @lastNames = keys %table; # @lastNames is: qw/schmoe smith simpson/
my @firstNames = values %table; # @firstNames is: qw/joe john bart/

use strict;
my %table = qw/schmoe joe smith john simpson bart/;
my($key, $value); # Declare two variables at once
while ( ($key, $value) = each(%table) ) {
# Do some processing on $key and $value
}

use strict;
my %table = qw/schmoe joe smith john simpson bart/;
foreach my $key (keys %table) {
# Do some processing on $key and $table{$key}
}

use strict;
my %table = qw/schmoe joe smith john simpson bart/;
my @friends = @table{'schmoe', 'smith'}; # @friends has qw/joe john/

use strict;
while ( defined($currentLine = <STDIN>) ) {
if ($currentLine =~ /^(J|R)MS speaks:/) {
print $currentLine;
}
}

(0|1|2|3|4|5|6|7|8|9)

use strict;
sub HowdyEveryone {
print "Hello everyone.\nWhere do you want to go with Perl today?\n";
}

&HowdyEveryone;

use strict;
sub HowdyEveryone {
return "Hello everyone.\nWhere do you want to go with Perl today?\n";
}
print &HowdyEveryone;

use strict;
sub HowdyEveryone {
my($name1, $name2) = @_;
return "Hello $name1 and $name2.\n" .
"Where do you want to go with Perl today?\n";
}
print &HowdyEveryone("bart", "lisa");

use strict;
sub HowdyEveryone {
my($greeting, @names) = @_;
my $returnString;
foreach my $name (@names) {
$returnString .= "$greeting, $name!\n";
}
return $returnString .
"Where do you want to go with Perl today?\n";
}
print &HowdyEveryone("Howdy", "bart", "lisa", "homer", "marge", "maggie");

Copyright © 2000 Free Software Foundation, Inc.
59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.

 Copyright (C) year your name.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.1
or any later version published by the Free Software Foundation;
with the Invariant Sections being list their titles, with the
Front-Cover Texts being list, and with the Back-Cover Texts being list.
A copy of the license is included in the section entitled ``GNU
Free Documentation License''.