Sonntag, 5. Juli 2009

PERL: some perl stuff 2


$arg=shift(@ARGV);
while(defined $arg) { ... }

$h{"bla"}="bla";
@val=values(%h);

<=> cmp -1 less 0 equal 1 greater

while(true) {} until(false){}

if() unless()

local($v) are local for temporary value of a gloabal variable

# modifying parameters
$a=1;
inc($a); # 2
sub inc {
$_[0]+=1;
}

@a=(1,2,3..);
mod(*a);
sub mod {
local (*la)=@_;
@la=reverse(@la);
}

print((split " ",$_)[0]."\n") while(<>);
# split im listenkontext, erster element

perl -ane 'print $F[0];' # analog
# -a autosplit, (split $_ into @F
# -n while loop

while(<>) { print if /head/; }


# cgi html
#!/usr/bin/perl -wT
print "Content-type: text/plain \n\n";
# -T taint Modus, Benutzereingaben in Quarantaene

HTML::Template;
use strict;
use warnings;
HTML::Embperl;
HTML::Mason;

Template;
HTML::Template;
DBI;
CGI;
CGI::Carp qw(fatalsToBrowser);

$q=new CGI;
foreach($q->param) { .. # param GET/POST parameters

$template=new HTML::Template(filename=>'addressbuch_kontakt.html');
$template->param(personen=>\@_pers)


#db
$dbh->selectrow_hashref('SELECT ..');
selectrow_array( .... );

if(exists $bla)
if( not exiss $bla)


#splice: removes and replaces elements in an array
splice(ARRAY, OFFSET, LENGTH, LIST);


%SIG # signal handles hash
sub handler { ... }
$SIG{'INT'}='handler';
$SIG{'INT'}='DEFAULT';


srand(time() | $$); # zeit doer pid einlesen
$l=int(rand(10));

if/ [^123]0 / # 10 20 30

x?? schaltet greedinessaus
\b word bounding
alternation /Ab|cd/ /(ma|pa)+/

$1 $2 ergebnisse der gruppierung
\1 \2 suchmuster
(?: ..) turn off capturing -> $1 void


V filehandle
$v scalar
@v array
%v hash
&v subroutine
*v everything named v

$s.$s2;
$s.=$s2; #concate
$sx4; #4 mal


s::mp('bla');
packages s;
sub mp {
print ("@_\n");
}

use File::Basename;
#look for File/Basename.pm


jeder Modul muss mit 1; beenden

BEGIN { require "bla.p"; }

pragmas: use integer; no integer;

calling methods (OOP)
$obj->meth($param);
meth $obj $param;

sub meth {
my $self=shift; # refernz auf obj
...
}

# constructor
sub new {
my $self={}; # hash anonymous referenz
$self->{ATTRIB1}=undef;
...
# make Referenz
bless($self);
return $self;
}

#Destructor
sub DESTROY {
...
}

###
sub u {
my $self=shift;
if(@_) {
$self->{U}=shift;
}
return $self->{U};
}
$o->('bla');
print $o->u,"\n";

#!!not
print "$o->u\n";
###


use Cwd;
$cur_dir=cwd;

use File::Basename;
(...)=fileparse(..);

use Config;
%Config


$perldoc -l copy
druckt den Pfad aus File::Find



www.pewrl.com/CPAN
HTML::LinkExtor, URI::URL
head() links()

Keine Kommentare:

Kommentar veröffentlichen