P005 SLUŽBY POČÍTAČOVÝCH SÍTÍObsah |
#!/usr/bin/perl -w print "Content type: text/plain\n\n"; print "Simple script.\n";Práva pro provádění:
chmod +x ab.cgiHTML výstup (text/html):
#!/usr/bin/perl -w print "Content type: text/html\n\n"; print <<EOF; <HTML><HEAD><TITLE> Script ab.cgi </TITLE></HEAD> <BODY> This is HTML body generated by script </BODY></HTML> EOFPočítadlo:
#!/usr/bin/perl $LOCK_EX = 2; $LOCK_UN = 8; $counter_file = "/home/user/public_html/counter_file"; print "Content-type: text/plain", "\n\n"; open (FILE, "+< $counter_file") || die "Error opening counter file.\n"; flock (FILE, $LOCK_EX); $count = <FILE>; seek (FILE, 0, SEEK_SET); print FILE ++$count; close (FILE); print "You are visitor number $count\n";Package, moduly, reference, objekty
use Curses; use Curses qw(initscr endwin);Reference:
$i = 10; $j = $i; print "$$j\n"; $$j += 16; print "$$j\n";
10 26
SCALAR(0x1001af8c)Adresa funkcí:
sub func1 { print "Func1 called\n"; } sub func2 { print "Func2 called\n"; } $ref = \&func1; &$ref(); $ref = \&func2; &$ref(); Func1 called Func2 calledAnonymní funkce:
sub func1 { print "Func1 called\n"; } $ref = \&func1; &$ref(); $ref = sub { print "Anonymous function\n"; }; &$ref();Konstruktor třídy:
package Widget; sub new { my $arg = shift; my $class = (ref $arg) || $arg; my $self = { }; bless $self, $class; $self->init( @_ ); $self; }Volání:
my $w = new Widget(); $w->resize(20, 5);Volání odkazem na objekt:
$w = Widget->new(); $x = $w->new();Always use -w. Try to use strict; Remember that you can add no strict qw(...); to individual blocks of code which need less strictness. Always use -w. Always use -w!
$q = new CGI; $name = $q->param('name'); $q->header; $q->header('image/jpeg');Formulář:
#!/usr/bin/perl -w use CGI; $q = new CGI; print $q->header, $q->start_html("Form"); print $q->startform, "Your name", "<BR>", $q->textfield('name'), "<P>"; print $q->radio_group('-name' => 'siblings', '-values' => ['none', "brother", "sister", "more of any" ], '-default' => "none"); print $q->submit('Action', 'Yes'); print "Variables:
", $q->dump; print $q->endform; print $q->end_html;
use LWP::Simple; print get "http://www/";Převod na text:
use LWP::Simple; use HTML::Parse; print parse_html(get "http://www/")->format;Jak stáhnout perl:
$ perl -MLWP::Simple -e ' getstore "ftp://sunsite.mff.cuni.cz/Languages/ Perl/CPAN/src/latest.tar.gz", "perl.tar.gz"; 'Modul LWP::UserAgent
use LWP::UserAgent; $client = new LWP::UserAgent(); $client->agent("Microsoft Exploder 0.99"); $request = new HTTP::Request('GET', 'http://www.fi.muni.cz/'); $request->header("Accept" => "text/html"); $answer = $client->request($request); print $answer->content if $answer->is_success();Další moduly:
http://w4.lns.cornell.edu/~pvhp/ptk/ptkTOC.html /~pvhp/ptk/doc/Widgety (přípravky):
#!/usr/bin/perl -w use strict; use Tk; my $main = new MainWindow; $main->Label(-text => "Hello, world")->pack(); $main->Button(-text => "End", -command => \&exit)->pack(); MainLoop;
$main->Button(-text => "End", -command => sub {print "Button pressed\n"; exit } )->pack();
Následující: JavaScript Obsah |
Jan Pazdziora adelton@informatics.muni.cz