Weitere ähnliche Inhalte Ähnlich wie Perl6 in-production (20) Mehr von Andrew Shitov (20) Kürzlich hochgeladen (20) Perl6 in-production39. my
@keys
=
('SERVER_NAME',
'REMOTE_ADDR',
'HTTP_USER_AGENT');
!
my
$key;
foreach
$key
(@keys){
print
"$key=%ENV{$key}<br
/>n";
}
41. my
@keys
=
('SERVER_NAME',
'REMOTE_ADDR',
'HTTP_USER_AGENT');
!
my
$key;
foreach
$key
(@keys){
print
"$key=%ENV{$key}<br
/>n";
}
46. read_params
(
%ENV{'QUERY_STRING'},
@params_key,
@params_value
);
47. sub
read_params
($query,
@params_key,
@params_value){
my
$pos
=
0;
loop
(my
$c
=
0;
1;
$c++)
{
my
$newpos
=
index
($query,
'&',
$pos);
my
@keyvalue;
my
$pair
=
substr
($query,
$pos,
$newpos
==
-‐1
??
length
($query)
::
$newpos
-‐
$pos);
split_pair
($pair,
@keyvalue);
@params_key[$c]
=
@keyvalue[0];
@params_value[$c]
=
@keyvalue[1];
last
if
$newpos
==
-‐1;
$pos
=
$newpos
+
1;
}
}
48. sub
read_params
($query,
@params_key,
@params_value){
my
$pos
=
0;
loop
(my
$c
=
0;
1;
$c++)
{
my
$newpos
=
index
($query,
'&',
$pos);
my
@keyvalue;
my
$pair
=
substr
($query,
$pos,
$newpos
==
-‐1
??
length
($query)
::
$newpos
-‐
$pos);
split_pair
($pair,
@keyvalue);
@params_key[$c]
=
@keyvalue[0];
@params_value[$c]
=
@keyvalue[1];
last
if
$newpos
==
-‐1;
$pos
=
$newpos
+
1;
}
}
49. sub
split_pair
($pair,
@keyvalue){
my
$pos
=
index
($pair,
'=');
if
($pos
==
-‐1){
@keyvalue[0]
=
$pair;
@keyvalue[1]
=
'';
}
else{
@keyvalue[0]
=
substr
($pair,
0,
$pos);
@keyvalue[1]
=
substr
($pair,
$pos
+
1);
}
}
51. sub
params2hash
(%params,
@params_key,
@params_value){
for
0
..
@params_key
-‐>
$c{
%params{@params_key[$c]}
=
@params_value[$c];
}
}
!
read_params
(%ENV{'QUERY_STRING'},
@params_key,
@params_value);
my
%params;
params2hash
(%params,
@params_key,
@params_value);
61. has
$!crlf
=
"x[0D]x[0A]";
.
.
.
!
print
"Status:
$status$!crlf";
print
"Location:
$uri";
print
"$!crlf$!crlf";
62. has
$!crlf
=
"x[0D]x[0A]";
.
.
.
!
print
"Status:
$status$!crlf";
print
"Location:
$uri";
print
"$!crlf$!crlf";
Compare
https://p6weekly.wordpress.com/2015/11/09/2015-46-
production-today/
64. method
add_param
(
Str
$key,
$value
)
{
if
%.params{$key}
:exists
{
#
RAKUDO:
~~
Scalar
if
%.params{$key}
~~
Str
|
Int
{
my
$old_param
=
%.params{$key};
%!params{$key}
=
[
$old_param,
$value
];
}
elsif
%.params{$key}
~~
Array
{
%!params{$key}.push(
$value
);
}
}
else
{
%!params{$key}
=
$value;
}
}
66. method
add_param
(
Str
$key,
$value
)
{
if
%.params{$key}
:exists
{
#
RAKUDO:
~~
Scalar
if
%.params{$key}
~~
Str
|
Int
{
my
$old_param
=
%.params{$key};
%!params{$key}
=
[
$old_param,
$value
];
}
elsif
%.params{$key}
~~
Array
{
%!params{$key}.push(
$value
);
}
}
else
{
%!params{$key}
=
$value;
}
}
67. if
%*ENV<MODPERL6>
{
my
$r
=
Apache::RequestRec.new();
my
$len
=
$r.read($input,
%*ENV<CONTENT_LENGTH>);
}
else
{
#
Maybe
check
content_length
here
#
and
only
take
that
many
bytes?
$input
=
$*IN.slurp;
}
!
70. grammar
November::URI::Grammar
{
token
TOP
{
^
[<scheme>
':']?
[
'//'
<authority>]?
<path>
['?'
<query>]?
['#'
<fragment>]?
$
};
token
scheme
{
<-‐[:/&?#]>+
};
token
authority
{
<host>
[':'
<port>]?
};
token
host
{
<-‐[/&?#:]>*
};
token
port
{
(d**1..5)
<?{
$0
<
2
**
16
}>
<!before
d>
};
token
path
{
<slash>?
[
<chunk>
'/'?]*
};
token
slash
{
'/'
};
token
chunk
{
<-‐[/?#]>+
};
token
query
{
<-‐[#]>*
};
token
fragment
{
.*
};
}
71. #
Official
regexp
(p5):
#
my($scheme,
$authority,
$path,
$query,
$fragment)
=
#
$uri
=~
m/
#
(?:([^:/?#]+):)?
#
(?://([^/?#]*))?
#
([^?#]*)
#
(?:?([^#]*))?
#
(?:#(.*))?
#
/x;
75. my
$app
=
sub
(%env)
{
return
(
200,
[
Content-‐Type
=>
'text/plain',
Content-‐Length
=>
%env.perl.bytes,
],
[
%env.perl
]
);
};
HTTP status code
76. my
$app
=
sub
(%env)
{
return
(
200,
[
Content-‐Type
=>
'text/plain',
Content-‐Length
=>
%env.perl.bytes,
],
[
%env.perl
]
);
};
HTTP headers
77. my
$app
=
sub
(%env)
{
return
(
200,
[
Content-‐Type
=>
'text/plain',
Content-‐Length
=>
%env.perl.bytes,
],
[
%env.perl
]
);
};
Response body
78. my
$builder
=
Plackdo::Builder.new;
$builder.add_middleware(
Plackdo::Middleware::XFramework.new(
framework
=>
'foobar')
);
$builder.to_app($app);
101. my
$http
=
HTTP::Easy::PSGI.new(
:port(8080)
);
103. my
$app
=
sub
(%env)
{
.
.
.
}
!
$http.handle($app);
104. my
$app
=
sub
(%env)
{
return
[
200,
[
'Content-‐Type'
=>
'text/plain'
],
[
'Hello,
World!’
]
];
}
105. use
HTTP::Easy::PSGI;
my
$http
=
HTTP::Easy::PSGI.new(:port(8080));
!
my
$app
=
sub
(%env)
{
my
$name
=
%env<QUERY_STRING>
||
"World";
return
[
200,
[
'Content-‐Type'
=>
'text/plain'
],
[
"Hello
$name"
]
];
}
!
116. $
perl6
test.pl
!
Entering
the
development
dance
floor:
http://0.0.0.0:3000
[2015-‐11-‐17T13:41:08Z]
Started
HTTP
server.
118. Method
'send'
not
found
for
invocant
of
class
'IO::Socket::INET'
in
method
run
at
/home/ash/p/
rakudo-‐star-‐2015.09/install/share/
perl6/lib/HTTP/Easy.pm6:193
in
sub
baile
at
/home/ash/p/
rakudo-‐star-‐2015.09/install/share/
perl6/lib/Bailador.pm:153
in
block
<unit>
at
test.pl:8
122. use
Bailador;
!
get
'/'
=>
sub
{
return
"Hin";
}
!
get
'/hello/:name'
=>
sub
($name)
{
return
"Hi,
$namen";
}
!
baile;
123. use
Bailador;
!
get
'/'
=>
sub
{
return
"Hin";
}
!
get
'/hello/:name'
=>
sub
($name)
{
return
"Hi,
$namen";
}
!
baile;
126. ===SORRY!===
Error
while
compiling
test1.pl
Variable
'$name'
is
not
declared
at
test1.pl:7
!
-‐-‐-‐-‐-‐-‐>
get
'/hello/:name'
=>
sub(⏏$name)
129. Bailador::Request.new(env
=>
{:HTTP_ACCEPT("*/
*"),
:HTTP_HOST("0.0.0.0:3000"),
:HTTP
_USER_AGENT("curl/
7.35.0"),
:PATH_INFO("/"),
:QUERY_STRI
NG(""),
:REQUEST_METHOD("GET"),
:REQUE
ST_URI("/"),
:SERVER_NAME("0.0.0.0"),
:SERVER_PORT(3000),
:SERVER_PROTOCOL("
HTTP/1.1"),
"p6sgi.encoding"
=>
"UTF-‐8",
"p6sgi.errors"
=>
IO::Handle.new(path
=>
IO::Special.new(what
=>
"<STDERR>"),
ins
=>
0,
chomp
=>
Bool::True),
131. class
Bailador::App
{
.
.
.
method
request
{
$.context.request
}
method
response
{
$.context.response
}
.
.
.
}
132. get
/.*/
=>
sub
{
return
request.env<REQUEST_URI>
~
"n";
}
133. get
/.*/
=>
sub
{
return
request.env<REQUEST_URI>
~
"n";
}
135. get
'/'
=>
sub
{
template
't.tt',
{
name
=>
'Name'
}
}
136. get
'/'
=>
sub
{
template
't.tt',
{
name
=>
'Name'
}
}
137. %
my
($h)
=
@_;
<html>
<head>
<title>
Hi,
<%=
$h<name>
%>
</title>
</head>
<body>
<h1>Hello
<%=
$h<name>
%></h1>
</body>
</html>
138. %
my
($h)
=
@_;
<html>
<head>
<title>
Hi,
<%=
$h<name>
%>
</title>
</head>
<body>
<h1>Hello
<%=
$h<name>
%></h1>
</body>
</html>
139. Use
of
uninitialized
value
$_location
of
type
Any
in
string
context
Any
of
.^name,
.perl,
.gist,
or
.say
can
stringify
undefined
things,
if
needed.
in
method
template
at
/home/ash/p/rakudo-‐
star-‐2015.09/install/share/perl6/
site/lib/Bailador/App.pm:14
Failed
to
open
file
/views/t.tt:
no
such
file
or
directory
in
method
template
at
140. my
$_location;
!
.
.
.
!
$!renderer.render(
slurp(
"$_location/views/$tmpl"
),
@params
);
lib/Bailador/App.pm
141. my
$_location;
!
.
.
.
!
$!renderer.render(
slurp(
"$_location/views/$tmpl"
),
@params
);
lib/Bailador/App.pm
142. my
$_location
=
'.';
!
.
.
.
!
$!renderer.render(
slurp(
"$_location/views/$tmpl"
),
@params
);
lib/Bailador/App.pm
147. use
DBIish;
my
$dbh
=
DBIish.connect(
'mysql',
:host<example.com>,
:port(3306),
:database<$dbname>,
:user<$dbuser>,
:password<$dbpassword>);
148. my
$sth
=
$dbh.prepare(
"select
now()"
);
!
$sth.execute();
150. my
$arr
=
$sth.fetchall_arrayref();
!
for
$arr
-‐>
$x
{
say
$x;
}