33
loading...
This website collects cookies to deliver better user experience
grep
, sed
, and awk
, Perl incorporated regular expressions into the language as few other languages have , with binding operators of =~
and !~
enabling easy matching and substitutions against expressions, and pre-compilation of regexps into their own type of value. Perl then added the ability to separate regexps by whitespace to improve readability, use different delimiters to avoid the leaning-toothpick syndrome of escaping slash (/
) characters with backslashes (\
), and name your capture groups and backreferences when substituting or extracting strings.$
(dollar sign).@
(at sign). %
(percent sign).$look
@like
%this
. Individual elements of arrays or hashes are scalars, so they $look[0]
$like{'this'}
. (That’s the first element of the @look
array counting from zero, and the element in the %like
hash with a key of 'this'
.)@this[1, 2, 3]
, and a slice of a hash looks like @that{'one', 'two', 'three'}
. You could write it out long-hand like ($this[1], $this[2], $this[3])
and ($that{'one'}, $that{'two'}, $that{'three'}
but slices are much easier. Plus you can even specify one or more ranges of elements with the ..
operator, so @this[0 .. 9]
would give you the first ten elements of @this
, or @this[0 .. 4, 6 .. 9]
would give you nine with the one at index 5
missing. Handy, that.$
; if it’s a list of values, it’s preceded with a @
; and if it’s a hash of key-value pairs, it’s preceded with a %
. You never have to be confused about the contents of a variable because the name will tell you what’s inside.(
parentheses )
[
square brackets ]
{
curly braces }
.$addresses = [
{ 'name' => 'John Doe',
'address' => '123 Any Street',
'city' => 'Anytown',
'state' => 'TX',
},
{ 'name' => 'Mary Smith',
'address' => '100 Other Avenue',
'city' => 'Whateverville',
'state' => 'PA',
},
];
=>
is just a way to show correspondence between a hash key and its value, and is just a funny way to write a comma (,
). And like some other programming languages, it’s OK to have trailing commas in a list as we do for the 'state'
entries above; it makes it easier to add more entries later.)say $$addresses[1]{'name'};
->
to dereference our array and hash elements:say $addresses->[1]->{'name'};
say for $addresses->[1]->@{'name', 'city'};
Mary Smith
Whateverville
'name'
and 'city'
out of…1
) referenced in…$addresses
.perldsc
documentation page, a references tutorial as the perlreftut
page, and finally a detailed guide to references and nested data structures as the perlref
page.sh
) or Bourne-again shell (bash
), so it has many special variable names using punctuation. There’s @_
for the array of arguments passed to a subroutine, $$
for the process number the current program is using in the operating system, and so on. Some of these are so common in Perl programs they are written without commentary, but for the others there is always the English
module, enabling you to substitute in friendly (or at least more awk
-like) names.use English;
at the top of your program, you can say:$LIST_SEPARATOR
instead of $"
$PROCESS_ID
or $PID
instead of $$
@{^CAPTURE}
array instead of the numbered regular expression capture variables like $1
, $2
, and $3
English
names alike, are documented on the perlvar
documentation page.English
equivalents is up to the developer, and some have more familiarity with and assume their readers understand the punctuation variety. Other less-friendly developers engage in “code golf,” attempting to express their programs in as few keystrokes as possible.perlstyle
documentation page admonishes, “Perl is designed to give you several ways to do anything, so consider picking the most readable one.” Developers can (and should) also use the perlcritic
tool and its included policies to encourage best practices, such as prohibiting all but a few common punctuation variables.There are only two kinds of languages: the ones people complain about and the ones nobody uses.
Bjarne Stroustrup, designer of the C++ programming language
33