Difference between revisions of "Perl"

From wiki
Jump to navigation Jump to search
Line 10: Line 10:
 
<syntaxhighlight lang=Perl>
 
<syntaxhighlight lang=Perl>
 
use Data::Dumper;
 
use Data::Dumper;
 +
print Dumper($variable);
 +
</syntaxhighlight>
 +
 +
if-then-else:
 +
<syntaxhighlight lang=Perl>
 +
if < expr > {
 +
<codeblock>
 +
} elsif < expr ) {
 +
<codeblock>
 +
} else {
 +
<codeblock>
 +
}
 
print Dumper($variable);
 
print Dumper($variable);
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 11:50, 19 March 2018

I'm leaving Perl, traded it for Python. Some shortcuts I'd like to remember

WARNING

Checking existence of a hash element creates the parent keys (autovivification). Always check top-down.

Things that do work

Print out the contents of $variable in a structured format.

use Data::Dumper;
print Dumper($variable);

if-then-else:

if < expr > {
 <codeblock>
} elsif < expr ) {
 <codeblock>
} else {
 <codeblock>
}
print Dumper($variable);
$var += <value>
Add <value> to $var (works for -, *, / too)
$string .= $addstring
Concatenate $string and $addstring
$string =~ s/<regexp>/<newvalue>/[g]
Substitute <regexp> with <newvalue> in $string. The g modifier makes the all occurrences of <regexp> are substituted.
$newstring = substr($string,start,length)
Return substring of $string
sprintf(format,$string)
Return formatted string. format is e.g. "%.3f". Check Python:Strings#Advanced for all formats.

Searching is explained in Regular Expressions.