Tutorials/Perl Tutorial

From ThorstensHome
(Redirected from Perl Tutorial)
Jump to: navigation, search

What I had to learn about perl:

Contents

Programming constructs

for

#!/usr/bin/perl
print scalar(localtime());
getc();
for ($i=0; $i<100000000; $i++){};

String addition

You can add strings together with a ".".

#!/usr/bin/perl
print scalar(localtime());
getc();
print scalar(localtime()."\n");
for ($i=0; $i<100000000; $i++){};
print scalar(localtime());

open files

For writing

open(FILE, ">"."$filename"); 
print(FILE "hello world");

functions with parameters

In any function (sub) you can access the parameters only via

$_[0]

for the first parameter

$_[1]

for the second parameter and so on.

preg_replace

Preg_replace in perl works like this:

 $url =~ s\http://\\g;

$url then contains what was in before, but without http://

I want to...

access the date

tstaerk@ls3122:~> cat test.pl
#!/usr/bin/perl
print localtime();

tstaerk@ls3122:~> ./test.pl
6461625510941751
tstaerk@ls3122:~> cat test.pl
#!/usr/bin/perl
print scalar(localtime());

tstaerk@ls3122:~> ./test.pl
Thu Jun 25 16:53:08 2009

See also