Difference between revisions of "Tutorials/Perl Tutorial"

From ThorstensHome
Jump to: navigation, search
 
(6 intermediate revisions by one user not shown)
Line 1: Line 1:
 +
{{DISPLAYTITLE:Perl Tutorial}}
 
What I had to learn about perl:
 
What I had to learn about perl:
 +
 +
= 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 =
 
= open files =
Line 22: Line 40:
 
</pre>
 
</pre>
 
$url then contains what was in before, but without http://
 
$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 =
 +
* http://en.wikibooks.org/wiki/Perl_Programming

Latest revision as of 17:28, 29 June 2009

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