, ,
undef,
,
undef.
,
,
:
@a = (0, $a, 5, "123", @b);
@b
, , ..
@a.
:
($a, $b, $c, @e) = ($b, $a, @g);
$a $b , $c
@g, @e.
:
@a[0, 1, 10];
:
($a, $b, $c, $d)[0, 1];
, ,
: localtime , time
( 1.01.1970), ,
"Fri Sep 15 11:12:13 2000", (,
, , , , , , ,
):
($day, $month, $year) = (localtime(time))[3, 4, 5];
, , ,
, 1900
. , 2000 100 (- ,
, 2000
1.01.100 1.01.19100).
"," => :
("one", 1, "two", 2, "three", 3) (one => 1, two
=>2, three => 3), .
push,
( ) , pop,
.
unshift shift.
@_.
reverse -
. sort
, .
, Perl ,
. ,
, .
, ( ),
, -- , %:
%a = (one => 1, two =>2, three => 3);
$a{1} (
"one").
key ,
values -- . each ,
- .
delete:
delete $a{1};
,
, . Perl.
,
:
$ref1 = \$var;
$ref2 = \@array;
$ref3 = \%hash;
, ,
:
$$ref1;
${$ref1};
$ref2->[12];
${$ref2}[12];
$$ref2[12];
$ref3->{$key};
${$ref3}{$key};
$$ref3{$key};
, ->,
.
:
$aref = [1, 2, 3];
$href = {1 => One, 2 => Two, 3 => Three};
(, ).
, .
,
.
:
if($a>$max) {$max = $a}
if:
if( 1)
{...}
elsif( 2)
{...}
elsif( 3)
{...}
...
else
{...}
if(!) unless(),
if(){}
if .
&& ||:
if(){} &&
, unless (){} -- ||
.
.
, :
while(!){} until(){},
-.
while .
for, ,
foreach,
foreach $i(@)
{}
$i
. , ,
, :
foreach $i(sort keys %a)
{
print $a{$i}."\n";
}
each --
:
while(($key, $value) = each(%a))
{
}
, -,
last, , next,
, redo,
. :
OUTER: while( 1)
{
INNER: while( 2)
{
if( 3)
{
next OUTER; #
}
}
}
:
sub f
{
}
f();
return,
,
.
@_,
: $_[0] .., shift, pop ..
, .
, ,
:
sub f
{
$a++;
}
$a = 1;
f();
print $a; # 2
, ,
my:
sub f
{
my $a;
$a++;
}
$a = 1;
f();
print $a; # 1
,
:
my($param1, $param2) = @_;
use strict
my .
my local.
: my
, local ,
.
, ,
.
sub by_num
{
return $a <=> $b;
}
foreach $i(sort by_num keys %a)
{
print $a{$i}."\n";
}
by_num , $a $b,
, .
, :
foreach $i(sort {$a <=> $b} keys %a)
{
print $a{$i}."\n";
}
:
sub func{...}
$fref1 = \&func;
$fref2 = sub {...}; #
$fref1->( ).
-
<STDIN>:
$a = <STDIN>; #
#
# (, , $/)
# undef,
@a = <STDIN>; # ( --
# Ctrl-Z);
#
while(<STDIN>) {...} #
# $_.
print,
-- printf,
. print ,
$_.
, ,
<>:
while(<>){...}
, <>
.
-
. STDIN, STDOUT, STDERR
, ,
, DATA, ,
__END__ ,
.
open:
open(FILE1, "filename"); #
open(FILE2 ">filename"); #
open(FILE3, ">>filename"); #
open "",
:
open (FILE, "filename") || die "cannot open file: $!";
die , $!
.
:
close (FILE);
- ,
-- ,
.
<> :
@a = <FILE>; # C .
# ,
#
print.
:
print FILE "some text\n";
"-e": if(-e
$filename){...}. ,
(-r), (-w),
(-d) (-f) ..
Perl DBM-,
DBM-. Unix-
DBM
. DBM,
, , ..
DBM- dbmopen, DBM-
dbmclose:
dbmopen(%A, "basename", 0644)
dbmclose(%A);
dbmopen ,
, .
, undef.
perldoc
AnyDBM_File.<u></u>
Unix,
Unix-, grep,
awk, sed, (vi, emacs), . .
, ,
. , ,
, . ,
.
$_,
=~ !~:
//; # , $_
# ""
$s =~ //; # , $s
# ""
$s !~ //; # , $s ""
, ,
s/1/2/;
, :
//i; #
s/$s//g; #
,
.
. -- (".").
, .
,
:
/[]/;
,
. (
\-), "^", ,
-- ,
.
:
\d [0-9]
\w [a-zA-Z0-9_]
\s [ \r\t\n\f]
\D [^0-9]
\W [^a-zA-Z0-9_]
\S [^ \r\t\n\f]
, . -- "*"
"+".
, -- .
"?" ,
, ,
, .
/ab*/; # , , ab, abb, abbb ..
/ab+/; # ab, abb, abbb ..
/ab?/; # , ab
/ab{2}/; # abb
/ab{2,4}/; # abb, abbb, abbbb
/ab{2,}/; # abb, abbb, abbbb ..
:
"", "" . "" ,
,
:
$s = 'abbbbb';
$s =~ s/a.*b/c/; # "".
"" "",
:
$s = 'abbbbb';
$s =~ s/a.*?b/c/; # "cbbbb".
"" ,
-- , /a*/
, * -- 0 .
,
, . ..
, -- ,
.
,
, , ,
$1, $2, $3 ..:
$s = 'abbbbb';
$s =~ s/a(.*)b/c/; # $1 "bbbb"
-- \1, \2
.. :
$s = 'abbbbb';
$s =~ s/a(.*)b/\1c/; # "bbbbc"
:
/1|2|3/
:
/(a|b)c/;
,
/(?:a|b)c/;
: /^a.*b$/
, a b, \b
, .
,
, ,
.
, ,
\Q...\E,
:
/\Q$var\E/
tr
:
$s =~ tr/abcde/ABCDE/;
/e s
Perl-:
$s =~ s/(.)/sprintf("[%s]", $1)/eg;
( ,
sprintf).
-- , , DBM-,
, . Perl
split. -- ,
, . ,
, ,
split:
$s = 'abc:def:ghi';
@a = split(/:/, $s); # @a -- "abc",
"def", "ghi".
, $_.
, /\s+/.
-- join -- ,
-, :
$s = join(":", @a);
, , Perl,
,
.
, , ,
.
main, package
. , ,
$ߏ::ߏ.
,
. ,
, .
use
require. use (
Perl ,
),
require .
Perl
++,
.
Perl :
$record = {
NAME => 'record1',
FIELD1 => 'value1',
FIELD2 => 'value2',
FIELD3 => 'value3',
};
print $record->{FIELD1};
$records{$record->{NAME}} = $record;
$records{"record1"}->{FIELD1} = 1;
Perl , -- (
), bless
.
-,
.
sub new
{
my $class = shift; #
my $self = {}; #
bless($self, $class); #
$self->{FIELD1} = "value1";#
$self->{FIELD2} = "value2";
return $self;
}
Class,
use Class;
$object1 = Class::new("Class");
$object2 = Class->new();
$object3 = new Class;
.
, Class,
, ,
:
sub f
{
my $self = shift;
$self->{FIELD1} = shift;
}
, Perl- , ++
.
, web-
web- Perl
, --
. Perl -- "
" (There's more than one way to do it, TMTOWTDI).
,
, ,
, , ,
-. -- --
, ,
, .
.
,
param1=value1¶m2=value2...¶mN=valueN,
QUERY_STRING,
, CONTENT_LENGTH
. , ,
REQUEST_METHOD.
Perl
%ENV,
read.
(
):
if($ENV{"REQUEST_METHOD"} eq 'POST')
{
read(STDIN, $query, $ENV{'CONTENT_LENGTH'});
}
else
{
$query = $ENV{'QUERY_STRING'};
}
:
@params = split(/&/, $query);
@params param1=value1.
,
:
foreach $p(@params)
{
($name, $value) = split(/=/, $);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$in{$name} = $value;
}
hex
" ->", pack --
, "
->".
%in, $in{'param1'}.
,
, .. ,
.
, .
, HTTP-.
, --
, . ,
-- Location:
print "Location: <u>http://newurl/text.html\n\n"</u>;
,
, , html-:
print "Content-type: text/html\n\n";
print '<html><head><title>Ok</title></head><body>Results:
<br>...</body></html>';
HTTP- --
, , cookie, ..
,
CGI- , ,
.
, ,
(
Perl 5.004) CGI.pm:
use CGI;
$Query = new CGI;
$val1 = $Query->param('param1'); #
$cookie1 = $Query->cookie('cookie1'); # cookie
# cookie:
$newcookie = $Query->cookie(
-name=>'new', #
-value=>'value', #
-expires=>"+1y", #
-domain=>'www.mydomain.ru' #
);
HTTP-:
print $Query->header(
-type=>'text/html',
-cookie=>$newcookie,
-Pragma=>"no-cache"
# ...
);
CGI.pm html-,
..
perldoc CGI.
web- Perl
-- HTTP-, ,
. ,
.
1. .
,
.
use Socket;
#
$forminfo = 'param1=val1¶m2=val2';
# :
# TCP
$proto = getprotobyname('tcp');
#
socket(Socket_Handle, PF_INET, SOCK_STREAM, $proto);
#
$port = 80;
$host = "www.somehost.com";
$sin = sockaddr_in($port,inet_aton($host));
#
connect(Socket_Handle,$sin) || die ("Cannot connect");
# ,
# GET
send Socket_Handle,"GET /cgi-bin/env.cgi?$forminfo HTTP/1.0\n",0;
# HTTP-
send Socket_Handle,"User-Agent: my agent\n",0;
send Socket_Handle,"SomeHeader: my header\n",0;
send Socket_Handle,"\n",0;
#
# , .
while (<Socket_Handle>)
{
print $_;
}
close (Socket_Handle);
%XX, XX -- .
:
$value=~s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
2. -- IO::Socket.
POST:
use IO::Socket;
$forminfo = 'param1=val1¶m2=val2';
$host = "www.somehost.com";
$socket = IO::Socket::INET->new(Proto => "tcp",
PeerAddr => $host,
PeerPort => "http(80)")
or die ("Cannot connect");
$socket->autoflush(1);
$length = length($forminfo)+1;
$submit = "POST $path HTTP/1.1\n".
"Content-type: application/x-www-form-urlencoded\n".
"Content-length: $length\n".
"Host: $host\n\n"
"$forminfo\n";
print $socket $submit;
while(<$socket>)
{
print;
}
close $remote;
3. , --
libwww-perl, LWP. LWP, ,
Perl, ,
CPAN <u>http://www.linpro.no/lwp/.</u>
, LWP (
perldoc
< >):
LWP::UserAgent
LWP::Simple
HTTP::Request
HTTP::Response
HTTP::Headers
HTTP::Cookies
LWP::Simple
GET:
use LWP::Simple;
$content = get('http://somehost/text.html');
LWP::UserAgent -- web.
-- ,
:
#
$UserAgent = new LWP::UserAgent;
# ""
$UserAgent->agent("MoZilla 9.0 (non-compatible; MSIE 9.3; PalmOS)");
#
$UserAgent->proxy('http', $proxy);
HTTP::Request , HTTP::Headers
-- :
#
$Headers = new HTTP::Headers(Referer => $referer);
#
$Request = new HTTP::Request(POST => $url, $Headers);
# ,
$Request->content_type('application/x-www-form-urlencoded');
#
$Request ->content($forminfo);
request,
HTTP::Response:
$Response = $UserAgent->request($Request);
if($Response->is_success) #
{
# ,
$text = $Response->content;
}
cookie HTTP::Cookie
cookie_jar,
Cookie:
my $ookieJar = new HTTP::Cookies;
$UserAgent->cookie_jar($CookieJar);
cookie
$CookieJar->load($cookiefilename);
$CookieJar->save($cookiefilename);
set_cookie.
, ,
HTTP- Authorization.
( BASIC) "_:",
basic- base64.
MIME::Base64:
use MIME::Base64;
$authorization = MIME::Base64::encode_base64("$name:$password");
$Request->header(Authorization => "BASIC $authorization");
Perl
DBI,
.
(, )
, mySQL.
DBI:
use DBI;
:
my $dbh = DBI->connect('DBI:mysql:hostname:base:port', 'user,
'password, { RaiseError => 1, AutoCommit => 1});
$dbh -- , ,
DBI:mysql:hostname:base:port -- ,
, , , , ,
sql-, user/password -- ,
, .
:
$dbh->disconnect();
. ,
, do,
SQL-:
$dbh->do("insert into mytable values (1,1)");
,
:
1. prepare:
my $sth = $dbh->prepare ("select * from mytable where field1>1");
2. :
$sth->execute();
3. . :
fetchrow_array
fetchrow_hash
fetchrow_arrayref
fetchrow_hashref
, , ,
, .
,
, false:
while(my $hash_ref = $sth->fetchrow_hashref)
{
foreach my $fieldname(keys %$hash_ref)
{
print "$fieldname: $hash_ref->{$fieldname }\n";
}
print "\n";
}
4. :
$sth->finish();
$dbh->quote(),
.
, :
$sth = $dbh->prepare("select * from mytable where field1=?");
$sth->bind_param(1, " ");
$sth->execute();
$sth = $dbh->prepare("select * from mytable where field1=?");
$sth->execute(" ");
quote ,
.
.
prepare,
execute , .
1.
.
Perl, ,
-.
.
Perl , ,
, ,
( , , ..),
(, , ..).
.
use strict;
, -- ;
: , ,
, , , ;
;
dbm-;
, ( ) .
2. web-
.
CGI- Perl.
.
CGI-,
web-.
.
CGI.pm;
,
,
cp866->win1251.
3. web-, mySQL
.
Perl- .
.
CGI- ,
dbm-, mySQL.
.
,
dbm-, mySQL;
;
, (,
..).
4.
.
,
http.
.
Perl ,
dbm- ,
CGI- http
.
.
LWP;
%XX, XX -- ,
cp866->win1251.
., . Perl DBI/. . -- :
-, 2000. -- 400 .
., . Perl. / .
-- .: "", 2000. -- 736 .
. /. . -- .:
" " "Channel Trading Ltd.", 1999, --
384 .
., . Web-/. . -- .:
BHV, 1997. -- 368 .
., . Perl/. . -- .:
BHV, 1998. -- 320 .