...which in my experience is certainly the "Pathologically Eclectic Rubbish
Lister"
Quickstart
Syntax:
http://perldoc.perl.org/perlsyn.html
Terminate all lines with semicolon (;) but not after blocks which are in
{}'s.
One = is assignment two = (==) is
comparison, but it compares the numerical
value when used with strings (e.g."abc" is converted to 0, "123" is converted
to 123), and eq is really string comparison. Same with <, >,
and lt, gt and with <=, >= and le, ge.
!= (numbers) is ne (string values). Wait till you get to <=> and cmp!
Comment out a sequence of lines by starting one with =pod and end
the comment with =cut. POD is "Plain Old
Documentation"1.
Comment out a single line with the pound sign (#). as in
print "hello"; # will print
#print "hello"; # will not print
Data:
http://perldoc.perl.org/perldata.html
References to variables start with $ if they are a single value of any type
(called a scalar, $ does NOT mean string) or @ if they are an array (or a
list) or % if they are a hash aka indexed array (% has nothing to do with
integers). $#array is index of last entry or approx # of entries in an Array.
keys(%hash) is count of keys in hash. No declarations are required.
Type casting is automatic. Preface the first reference to a variable with
my if you want it to be local.
Don't step on
the predefined variables! Examples
-
@hexdigits = ('a','b','c','d','e','f');$hexdigit = $hexdigits[$digit-10];
# Find a hex digit.
-
$foo and @foo are two different variables and
$foo[1] is a part of @foo, not a part of $foo.
-
$hexdigits[2] is not 'b', it is 'c'; the 3th element
of the array @hexdigits. Array indexes are zero based.
-
@hexdigits[3,4,5] is an array made from the 4 through 6
elements of the array @hexdigits. Called a slice
-
%daysin = ("Jan"=>31, "Feb"=>28, "Mar"=>31, "Apr"=>30,
"May"=>31, "Jun"=>30, "Jul"=>31, "Aug"=>31, "Sep"=>30,
"Oct"=>31, "Nov"=>30, "Dec"=>31 ); #makes a hash with keys for the
month abbr and values for the # of days.
$daysin{'Jan'} is the 'Jan' value from the hash %daysin. Anything in
the {} is considered a string so $daysin{Jan} is also valid.
-
$daysin{Feb}=29 #Corrects for leapyear.
-
If @stuff = split("-", "A-b-C") then 2 == $#stuff and "A"
eq $stuff[0]
-
foreach $days (@days{keys %days}) ilterates all the values in the
hash %days (could be done better with an array unless you also need random
access)
-
foreach $days (keys %hash) ilterates all the keys in the hash. e.g.
foreach $key (keys %query) { print "\n\t<INPUT TYPE=HIDDEN NAME=\"$key\"
VALUE=\"$query{$key}\">" }
-
$daysin{Feb}="" sets the Feb entry in daysin to nothing, the Feb
entry is still present.
-
delete $daysin{Feb} removes the Feb entry from daysin
-
%daysin=() clears daysin
Values are replaced in a string constant enclosed with double quotes but
not in one with single quotes.
-
"Hello $name" is "Hello James" if $name eq James.
-
'Hello $name' is "Hello $name"
-
This is overriden by the escape or \ so "Hello
\$name" is "Hello $name" no matter what $name is.
-
If $foo='hi'; print "$foo ho"; prints "hi ho" but print
"$fooho"; fails, and print "$foo\ho"; prints "hho" (where \h
is a backspace) or it prints "hiho" but throws a warning about an unknown
escape. print "${foo}ho"; prints out "hiho" with no warnings.
A scalar value is interpreted as TRUE in the Boolean sense if it is not the
null string or the number 0 (or its string equivalent, ``0''). There are
actually two varieties of null strings (sometimes referred to as ``empty''
strings), a defined one and an undefined one. The defined version is just
a string of length zero, such as "". The undefined version is the value that
indicates that there is no real value for something, such as when there was
an error, or at end of file, or when you refer to an uninitialized variable
or element of an array or hash.
Parameters are optional (or rather there are default parameters). You can
say while (<*.c>) { print; } and the names of all the .c files
in the current directory will be printed. These names got from
<*.c> to print via a thing refferred to as
$_ If you
don't specify a variable to hold a return value or supply a parameter value,
it uses $_
The array @ARGV contains the command-line arguments intended for the script.
$#ARGV is generally the number of arguments minus one, because $ARGV[0] is
the first argument, not the program's command name itself.
Half of everything in Perl is regular expressions.
They look like gibberish. I have yet to find a concise, complete, and
understandable reference.
-
http://perldoc.perl.org/perlre.html
is the best so far. But first read this next one....
-
http://perl.plover.com/Regex/article.html
(cached
20010216093727)
and spend some time on it... it made it make a bit more sense for me....
-
To filter an array of values removing matching entries: @out = grep($_
!~ /regex/, @in); or allowing only matching entries: @out
= grep($_ ~ /regex/, @in);
-
To do replacements and splits without regular expressions (e.g.
interpreting "\temp" as "\temp" rather than as a tab char followed by "emp")
use \Q before the search text and \E after it. (e.g. s/\Q\temp\E/\win\temp/g)
All better? Nope; now you can't have a \E in your string! But you can have
a \E in a scaler that is used in place of it. (e.g. $t="(XXX) 123-456 \Emp.";
s/\Q$t\E/$emp_phone/g )
-
Trim whitespace: for ($variable) { s/^\s+//; s/\s+$//; }
Statements: Note that the subscriped numbers show the order
of execution in cases were that might be confusing.
-
declaration;
(not needed 99% of the time, just refer the variable or whatever.)
-
expression;
-
do { statements1 } until
condition2; #condition
evaulated AFTER statement
-
statement foreach list;
-
if (condition) {statements;}
[[ elseif
(condition)
{statements;} ]] [ else {statements;}
]
-
[ label: ] while
(condition1)
{statements2;} [ continue
{statements3;} ]
-
do { statements1 } while
condition2; #condition
evaulated AFTER statement
-
[ label: ] for (setup1;
condition2;
statement4) {statement3;}
-
[ label: ] foreach [ ilterator ]
{statements;} (list) {statements;} [
continue {statements;} ]
-
statement2 if
condition1;
-
statement2 unless
condition1;
-
statement2 while
condition1; #condition
evaulated BEFORE statement
-
statement2 until
condition1; #condition
evaulated BEFORE statement
-
sub name [
(prototype) ] [ : attributes ]
[ {statements;} ]
-
[ label: ] {statements;} continue
{statements;}
http://search.cpan.org/dist/perl/pod/perlsyn.pod
Note that if, while, for, foreach are apparently not considered functions
but rather modifiers for statements. In fact, the "statements" in the language
can be extended, overridden, and augemented with the because a block of
statements (in {} or a single statement) can be passed to a
sub as a parameter. Sort of like passing a function
but without any name to the function.
Shell to the os with a command enclosed in backticks (grave accents). e.g.
`dir` will return each line of output as an element of an array and the status
of the command as a scaler.
Functions:
http://perldoc.perl.org/index-functions.html
-
recurse {filehandle}
"pathandfile";
-
<filehandle>
-
open(filehandle,
"pipe
pathandfile");
-
binmode(filehandle);
-
pack / unpack
-
split(regexdelimeter, string);
-
length(string)
-
sort list; #sorts in standard string comparison order
-
sort {comparison} list; #comparison must evaluate to
-1, 0 or 1 based on the relative values of two parameters: $a and $b. see
<=> and
cmp
-
time #returns the number of seconds since the epoch. Great for unique file
names.
-
localtime() #returns an array of time related info from the value it is given
or a ctime(3) string.
localtime() <=> localtime(time)
gmtime() #does the same but the values are for the Greenwich time zone and
it does not return $isDST
$now_string = localtime; # e.g., "Thu Oct 13 04:54:34 2004"
($sec,$min,$hour,$dayofmonth,$month,$year,$dayofweek,$dayofyear,$isDST)
= localtime(time);
$year += 1900; #years since 1970 on a PC, since 1904 on a MAC.
$month++;$dayofyear++; #everything else is 0 based... err, except
$dayofmonth
$weekday=('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')[$dayofweek];
$YYYYMMDD=sprintf("%0.4d-%0.2d-%0.2d",$year,$month,$dayofmonth); # e.g.
"2004-10-13"
$MMDDYY=sprintf("%0.2d/%0.2d/%0.2d",$month,$dayofmonth,$year % 100);
# e.g. "10/13/04"
Encoding URLs (thanks to
http://glennf.com/writing/hexadecimal.url.encoding.html
)
sub URLEncode {
my $theURL = $_[0];
$theURL =~ s/([\W])/"%" . uc(sprintf("%2.2x",ord(1ドル)))/eg;
return $theURL;
}
Functions which can process a list of parameters seperated by commas can
be completely fooled by parenta
Win32 stuff, Web Services
A sample mailto.pl script
A sample http get script
a sample bbs_forum.pl script
See also:
Questions:
-
It's not "PERL", it's
"Perl". The name of the interpreter for the language is
"perl".
James
Newton replies: Oooook! Thanks for the
info.+
+
file: /Techref/language/perl/index.htm,
15KB, , updated: 2021年3月27日 03:21, local time: 2025年9月1日 00:59,
©2025 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Questions?<A HREF="http://techref.massmind.org/techref/language/perl/index.htm"> PERL</A>
Did you find what you needed?
Welcome to massmind.org!
Welcome to techref.massmind.org!
.