Perl Tipps und Tricks: Difference between revisions
From Lolly's Wiki
Jump to navigationJump to search
No edit summary |
m (Text replacement - "[[Kategorie:" to "[[Category:") |
||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[ | [[Category:Perl|Tipps und Tricks]] | ||
==Negative match in RegEx (?:(?!PATTERN).)*== | |||
Usage of perl as a spcial grep :-): | |||
<syntaxhighlight lang=bash> | |||
perl -ne 'if (/(<a href=[^>]+action=login[^>]+>(?:(?!<\/a>).)*<\/a>)/){ print $1."\n"; }' index.html | |||
</syntaxhighlight> | |||
This one matches a complete <pre><a href=...action=login...>(not </a>)</a></pre>. | |||
Or more complex: | |||
<syntaxhighlight lang=bash> | |||
perl -ne 'if (/(<a href=[^h]*(http[s]{0,1}:\/\/([^\/"]+)[^> "]+)[^> ]*>(?:(?!<\/a>).)*<\/a>)/){ print $3."|".$2."|".$1."\n"; }' index.html | |||
</syntaxhighlight> | |||
Prints out: | |||
<pre> | |||
<server>|<url>|<complete href> | |||
</pre> | |||
==Unread while reading from filehandle== | ==Unread while reading from filehandle== | ||
Line 5: | Line 22: | ||
Dov Grobgeld made my day! | Dov Grobgeld made my day! | ||
< | <syntaxhighlight lang=perl> | ||
# Found at a comment of Dov Grobgeld at https://groups.google.com/d/msg/comp.lang.perl/7fPyGpWpP8M/hc7xTMvAoW0J | # Found at a comment of Dov Grobgeld at https://groups.google.com/d/msg/comp.lang.perl/7fPyGpWpP8M/hc7xTMvAoW0J | ||
while($_ = shift(@linestack) || <IN>) { | while($_ = shift(@linestack) || <IN>) { | ||
Line 11: | Line 28: | ||
push(@linestack, $whatever); # unread | push(@linestack, $whatever); # unread | ||
} | } | ||
</ | </syntaxhighlight> | ||
== Config == | |||
Override compile time flags on the commandline like this: | |||
<syntaxhighlight lang=perl> | |||
PERL_MM_OPT='optimize=-O2 cc=gcc ld=gcc cccdlflags=-DPIC' | |||
</syntaxhighlight> | |||
I used it to run sa-compile on Solaris: | |||
<syntaxhighlight lang=perl> | |||
#!/bin/bash | |||
exec >> /var/log/update-spamd-rules.log 2>&1 | |||
#LD_LIBRARY_PATH=/usr/sfw/lib | |||
PATH=$PATH:/usr/local/bin:/opt/re2c/bin:/usr/sfw/bin:/usr/ccs/bin:/opt/csw/bin | |||
PERL_VER=$(/usr/perl5/bin/perl -e 'printf "%.3f",$];') | |||
SA_VER=$(/opt/spamassassin/bin/spamassassin -V | /usr/bin/nawk ' | |||
/SpamAssassin version/ { | |||
split($NF,version,/\./); | |||
printf "%d.%03d%03d",version[1],version[2],version[3]; | |||
}') | |||
export LD_LIBRARY_PATH PATH PERL_VER SA_VER | |||
/usr/perl5/bin/perlgcc -T /opt/spamassassin/bin/sa-update --updatedir=/var/opt/spamassassin/$SA_VER -D | |||
PERL_MM_OPT='optimize=-O2 cc=gcc ld=gcc cccdlflags=-DPIC' /opt/spamassassin/bin/sa-compile --updatedir=/var/opt/spamassassin/compiled/${PERL_VER}/${SA_VER} -D | |||
/usr/bin/kill -HUP `cat /tmp/spamd-exim-acl.pid` | |||
/usr/bin/kill -HUP `cat /tmp/spamd-ip.pid` | |||
</syntaxhighlight> |
Latest revision as of 02:39, 26 November 2021
Negative match in RegEx (?:(?!PATTERN).)*
Usage of perl as a spcial grep :-):
perl -ne 'if (/(<a href=[^>]+action=login[^>]+>(?:(?!<\/a>).)*<\/a>)/){ print $1."\n"; }' index.html
This one matches a complete
<a href=...action=login...>(not </a>)</a>
.
Or more complex:
perl -ne 'if (/(<a href=[^h]*(http[s]{0,1}:\/\/([^\/"]+)[^> "]+)[^> ]*>(?:(?!<\/a>).)*<\/a>)/){ print $3."|".$2."|".$1."\n"; }' index.html
Prints out:
<server>|<url>|<complete href>
Unread while reading from filehandle
Dov Grobgeld made my day!
# Found at a comment of Dov Grobgeld at https://groups.google.com/d/msg/comp.lang.perl/7fPyGpWpP8M/hc7xTMvAoW0J
while($_ = shift(@linestack) || <IN>) {
:
push(@linestack, $whatever); # unread
}
Config
Override compile time flags on the commandline like this:
PERL_MM_OPT='optimize=-O2 cc=gcc ld=gcc cccdlflags=-DPIC'
I used it to run sa-compile on Solaris:
#!/bin/bash
exec >> /var/log/update-spamd-rules.log 2>&1
#LD_LIBRARY_PATH=/usr/sfw/lib
PATH=$PATH:/usr/local/bin:/opt/re2c/bin:/usr/sfw/bin:/usr/ccs/bin:/opt/csw/bin
PERL_VER=$(/usr/perl5/bin/perl -e 'printf "%.3f",$];')
SA_VER=$(/opt/spamassassin/bin/spamassassin -V | /usr/bin/nawk '
/SpamAssassin version/ {
split($NF,version,/\./);
printf "%d.%03d%03d",version[1],version[2],version[3];
}')
export LD_LIBRARY_PATH PATH PERL_VER SA_VER
/usr/perl5/bin/perlgcc -T /opt/spamassassin/bin/sa-update --updatedir=/var/opt/spamassassin/$SA_VER -D
PERL_MM_OPT='optimize=-O2 cc=gcc ld=gcc cccdlflags=-DPIC' /opt/spamassassin/bin/sa-compile --updatedir=/var/opt/spamassassin/compiled/${PERL_VER}/${SA_VER} -D
/usr/bin/kill -HUP `cat /tmp/spamd-exim-acl.pid`
/usr/bin/kill -HUP `cat /tmp/spamd-ip.pid`