<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Linux @ Home &#187; programmierung</title>
	<atom:link href="http://www.linuxhome.ch/category/programmierung/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.linuxhome.ch</link>
	<description>Pinguine, Politik undsoweiter...</description>
	<lastBuildDate>Thu, 05 Jan 2012 09:03:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ggT / gcd, wer kann&#8217;s am Kürzesten?</title>
		<link>http://www.linuxhome.ch/geek/ggt-gcd-wer-kanns-am-kurzesten/</link>
		<comments>http://www.linuxhome.ch/geek/ggt-gcd-wer-kanns-am-kurzesten/#comments</comments>
		<pubDate>Thu, 05 Jan 2012 08:50:45 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[geek]]></category>
		<category><![CDATA[programmierung]]></category>

		<guid isPermaLink="false">http://www.linuxhome.ch/?p=1074</guid>
		<description><![CDATA[ggT / gcd, wer kann&#8217;s am Kürzesten? Diese Frage, tauchte auf, nachdem ich versuchte, den grössten gemeinsamen Teiler aus Lust und Laune in ein paar Programmiersprachen zu implementieren (und einige male kläglich scheiterte). Von der Auswahl ausgenommen sind esoterische oder „unleserliche“ Sprachen (APL) und natürlich solche, die eine gcd-Funktion bereits in den Bibliotheken haben. Unter [...]]]></description>
			<content:encoded><![CDATA[<img style='float: left; margin-right: 10px; border: none;' src='http://www.gravatar.com/avatar.php?gravatar_id=d6b1294fe73eea0bc9dc84a732e5e880&amp;default=http://use.perl.org/images/pix.gif' alt='No Gravatar' width=40 height=40/><p>ggT / gcd, wer kann&#8217;s am Kürzesten? Diese Frage, tauchte auf, nachdem ich versuchte, den grössten gemeinsamen Teiler aus Lust und Laune in ein paar Programmiersprachen zu implementieren (und einige male kläglich scheiterte). Von der Auswahl ausgenommen sind esoterische oder „unleserliche“ Sprachen (APL) und natürlich solche, die eine gcd-Funktion bereits in den Bibliotheken haben. Unter diesen Gesichtspunkten gewinnt hier relativ klar <strong>Forth</strong>! Irgendwie überraschend (aber beim zweiten Nachdenken doch nicht mehr so überraschend), dass eine Programmiersprache aus den Sechzigerjahren des vorigen Jahrhunderts so unglaublich effizient im Ausdruck ist:</p>

<div class="wp_syntax"><div class="code"><pre class="forth" style="font-family:monospace;">: gcd ( a b -- n )
  begin dup while tuck mod repeat drop ;</pre></div></div>

<p>Zugegeben, ohne genau zu wissen, was tuck macht, ist es etwas schwierig zu verstehen, aber das Prinzip der wiederholenden Modulo ist schon klar zu erkennen.<br />
Aufrufe dieser Funktion macht man dann in etwa so:</p>
<pre>142864 24 gcd . 8</pre>
<p>oder pro Linie ein Argument (da merkt man deutlich, dass Forth nur auf einem Stack arbeitet):</p>
<pre>14361672
649981417530
gcd
. 1446</pre>
<p>Wer dies unbedingt austesten will, sollte sich <em>gforth</em> (GNU) installieren. Für Fans der UPN: In Forth rechnet man aufgrund des Stacks natürlich mit der umgekehrten polnischen Notation:</p>
<pre>5 3 + 7 2 + * .</pre>
<p>(mit dem Punkt holt man sich das letzte Resultat vom Stack, quasi ein <em>pop</em>). Natürlich gibt es auch andere Kandidaten, die zumindest ähnlich kurz sind, aber meines Erachtens ist Forth noch die bekannteste (ich habe jedenfalls noch nie was von K oder Joy gehört). Interessanterweise bietet auch gnuplot eine Lösung dafür an. Ich weiss nicht, ob man Gnuplot zu den Turing-vollständigen Sprachen zählt, deshalb ausser Konkurrenz:</p>

<div class="wp_syntax"><div class="code"><pre class="gnuplot" style="font-family:monospace;">gcd <span style="color: #000099; font-weight:bold;">&#40;</span>a<span style="color: #000; font-weight: bold;">,</span> b<span style="color: #000099; font-weight:bold;">&#41;</span> <span style="color: #000; font-weight: bold;">=</span> <span style="color: #007800;">b =</span><span style="color: #000; font-weight: bold;">=</span> <span style="color: #cc66cc;">0</span> ? a <span style="color: #000; font-weight: bold;">:</span> gcd <span style="color: #000099; font-weight:bold;">&#40;</span>b<span style="color: #000; font-weight: bold;">,</span> a <span style="color: #000; font-weight: bold;">%</span> b<span style="color: #000099; font-weight:bold;">&#41;</span></pre></div></div>

<p>Ach ja, der Preis für die längste Implementierung von gcd geht an SED (ein Streaming-Editor von Unix, der eigentlich nie als Programmiersprache gedacht war), den Sourcecode kann man sich bei rosettacode.org angucken <img src='http://www.linuxhome.ch/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<address>[1] <a href="http://rosettacode.org/wiki/Gcd">http://rosettacode.org/wiki/Gcd</a></address>
]]></content:encoded>
			<wfw:commentRss>http://www.linuxhome.ch/geek/ggt-gcd-wer-kanns-am-kurzesten/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Klein und fein: Der Boyer-Moore-Horspool Algorithmus</title>
		<link>http://www.linuxhome.ch/geek/klein-und-fein-der-boyer-moore-horspool-algorithmus/</link>
		<comments>http://www.linuxhome.ch/geek/klein-und-fein-der-boyer-moore-horspool-algorithmus/#comments</comments>
		<pubDate>Tue, 01 Nov 2011 14:51:52 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[geek]]></category>
		<category><![CDATA[programmierung]]></category>

		<guid isPermaLink="false">http://www.linuxhome.ch/?p=1063</guid>
		<description><![CDATA[Zugegeben, ich verstand den Boyer-Moore Algorithmus nicht ganz, was alleine schon ein Grund war, eine einfachere Variante zu implementieren. Der Boyer-Moore-Horspool ist so eine Variante. Glücklicherweise zeigt sich, dass die Horspool-Vereinfachung die Textsuche im durchschnittlichen Fall nicht oder nicht viel langsamer werden lässt (rein subjektive Einschätzung!). Neugierige sollten den Code 1:1 kopieren und verwenden können [...]]]></description>
			<content:encoded><![CDATA[<img style='float: left; margin-right: 10px; border: none;' src='http://www.gravatar.com/avatar.php?gravatar_id=d6b1294fe73eea0bc9dc84a732e5e880&amp;default=http://use.perl.org/images/pix.gif' alt='No Gravatar' width=40 height=40/><p>Zugegeben, ich verstand den Boyer-Moore Algorithmus nicht ganz, was alleine schon ein Grund war, eine einfachere Variante zu implementieren. Der Boyer-Moore-Horspool ist so eine Variante. Glücklicherweise zeigt sich, dass die Horspool-Vereinfachung die Textsuche im durchschnittlichen Fall nicht oder nicht viel langsamer werden lässt (rein subjektive Einschätzung!). Neugierige sollten den Code 1:1 kopieren und verwenden können mit einem beliebigen Java-Compiler. Und bitte einfach den Programmierstil ausblenden, es ging mir nur darum, den Algorithmus zu testen und wenigstens halbwegs zu verstehen. Heute braucht man sich kaum mehr selber um Implementierungen von Suchalgorithmen zu bemühen, die meisten Programmierumgebungen bringen entsprechende Bibliotheksfunktionen seit vielen Jahren mit.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BoyerMooreHorspoolSearch <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> text<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> text_len<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> ALPHABET_SIZE <span style="color: #339933;">=</span> <span style="color: #cc66cc;">256</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> BoyerMooreHorspoolSearch<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">String</span> str <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;overeovere The oxen-brown Fox jumped over the Fox-lake. It (fox) didn't&quot;</span> <span style="color: #339933;">+</span>
				<span style="color: #0000ff;">&quot; know why, but it was kinda fun overemphasized! xxxxxxxxxxx aaaaaaa&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">text</span> <span style="color: #339933;">=</span> str.<span style="color: #006633;">toCharArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		text_len <span style="color: #339933;">=</span> text.<span style="color: #006633;">length</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DEBUG: text_len: &quot;</span> <span style="color: #339933;">+</span> text_len<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> BoyerMooreHorspoolSearch<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> text<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">text</span> <span style="color: #339933;">=</span> text.<span style="color: #006633;">toCharArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		text_len <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">text</span>.<span style="color: #006633;">length</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> search<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> pattern<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> pat <span style="color: #339933;">=</span> pattern.<span style="color: #006633;">toCharArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> pat_len <span style="color: #339933;">=</span> pat.<span style="color: #006633;">length</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> occurrenceTable <span style="color: #339933;">=</span> initOccurrence<span style="color: #009900;">&#40;</span>pat<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> pos <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// our text index position</span>
		<span style="color: #000066; font-weight: bold;">int</span> countloops <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>pos <span style="color: #339933;">&lt;=</span> text_len<span style="color: #339933;">-</span>pat_len<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> pat_len<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// our pattern index position</span>
			<span style="color: #666666; font-style: italic;">// As long as the pattern chars are hits (we start at the</span>
			<span style="color: #666666; font-style: italic;">// end of the pattern) =&gt; go one backwards (to the left).</span>
			<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">&amp;&amp;</span> pat<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> text<span style="color: #009900;">&#91;</span>pos<span style="color: #339933;">+</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> j<span style="color: #339933;">--;</span> <span style="color: #666666; font-style: italic;">// hits</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> report<span style="color: #009900;">&#40;</span>pattern, pos<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// We get here, when: a) a full hit was found</span>
			<span style="color: #666666; font-style: italic;">// and b) nothing was found or partially pattern was found</span>
			<span style="color: #666666; font-style: italic;">// in either case, we have to check, how far to the right we</span>
			<span style="color: #666666; font-style: italic;">// can jump now.</span>
			<span style="color: #666666; font-style: italic;">//</span>
			<span style="color: #666666; font-style: italic;">// Now we do this in one step: take the jump-length from</span>
			<span style="color: #666666; font-style: italic;">// the occurrenceTable indicating pat_len, if the character</span>
			<span style="color: #666666; font-style: italic;">// of the current pos in text is _not_ in the pattern and</span>
			<span style="color: #666666; font-style: italic;">// giving the difference from the right to the rightmost character</span>
			<span style="color: #666666; font-style: italic;">// in the pattern, if the character at pos in text _is_ in our</span>
			<span style="color: #666666; font-style: italic;">// pattern</span>
			pos <span style="color: #339933;">+=</span> occurrenceTable<span style="color: #009900;">&#91;</span>text<span style="color: #009900;">&#91;</span>pos<span style="color: #339933;">+</span>pat_len <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			countloops<span style="color: #339933;">++;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DEBUG: Loops: &quot;</span> <span style="color: #339933;">+</span> countloops<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> initOccurrence<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> pat<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> occurrenceTable <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span>ALPHABET_SIZE<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">char</span> a<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> pat_len <span style="color: #339933;">=</span> pat.<span style="color: #006633;">length</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>a <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> a <span style="color: #339933;">&lt;</span> ALPHABET_SIZE<span style="color: #339933;">;</span> a<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
			occurrenceTable<span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> pat_len<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// We fill the table at the position of the ASCII char</span>
		<span style="color: #666666; font-style: italic;">// (for example occurrenceTable[97] for 'a') with the</span>
		<span style="color: #666666; font-style: italic;">// index value of it's last occurrence in the pattern.</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> pat_len<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
			occurrenceTable<span style="color: #009900;">&#91;</span>pat<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span>pat_len <span style="color: #339933;">-</span> j <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> occurrenceTable<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> report<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> pat, <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> pos<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Found <span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">+</span> pat <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span> at position &quot;</span>
				<span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>pos<span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; (index &quot;</span> <span style="color: #339933;">+</span> pos <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;) in the text!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Driver</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @param args
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//		BoyerMooreHorspoolSearch bmh = new BoyerMooreHorspoolSearch(&quot;Is there a fox?&quot;);</span>
		BoyerMooreHorspoolSearch bmh <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BoyerMooreHorspoolSearch<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		bmh.<span style="color: #006633;">search</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Fox&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		bmh.<span style="color: #006633;">search</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fox&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		bmh.<span style="color: #006633;">search</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; fox&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this should _not_ be found</span>
		bmh.<span style="color: #006633;">search</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ox&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		bmh.<span style="color: #006633;">search</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;xxx&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		bmh.<span style="color: #006633;">search</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;overe&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.linuxhome.ch/geek/klein-und-fein-der-boyer-moore-horspool-algorithmus/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automatisierte Server-infrastruktur mit Puppet in der KMU: Lohnt sich das?</title>
		<link>http://www.linuxhome.ch/linux/automatisierte-server-infrastruktur-mit-puppet-in-der-kmu-lohnt-sich-das/</link>
		<comments>http://www.linuxhome.ch/linux/automatisierte-server-infrastruktur-mit-puppet-in-der-kmu-lohnt-sich-das/#comments</comments>
		<pubDate>Thu, 08 Sep 2011 12:54:58 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[geek]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.linuxhome.ch/?p=1035</guid>
		<description><![CDATA[Puppet ist seit Jahren im Aufwind und entwickelt sich zum Nonplusultra im professionellen Konfigurationsmanagement. Es ist schon längst nicht mehr nur die &#8220;Alternative zu cfengine&#8221; die es anfangs noch war. Die Liste der Firmen, die seit längerem und im grossen bis sehr grossen Umfang (bis zehntausende von Servern oder Instanzen) auf Puppet setzen ist beeindruckend, [...]]]></description>
			<content:encoded><![CDATA[<img style='float: left; margin-right: 10px; border: none;' src='http://www.gravatar.com/avatar.php?gravatar_id=d6b1294fe73eea0bc9dc84a732e5e880&amp;default=http://use.perl.org/images/pix.gif' alt='No Gravatar' width=40 height=40/><p>Puppet ist seit Jahren im Aufwind und entwickelt sich zum Nonplusultra im professionellen Konfigurationsmanagement. Es ist schon längst nicht mehr nur die &#8220;Alternative zu <a title="cfengine homepage" href="http://cfengine.com/" target="_blank">cfengine</a>&#8221; die es anfangs noch war. Die<a title="Who uses Puppet?" href="http://projects.puppetlabs.com/projects/puppet/wiki/Whos_Using_Puppet" target="_blank"> Liste</a> der Firmen, die seit längerem und im grossen bis sehr grossen Umfang (bis zehntausende von Servern oder Instanzen) auf Puppet setzen ist beeindruckend, darunter finden sich unter anderem Google, Twitter, Red Hat, Sun, Citrix, match.com, Rackspace etc. Wegen der ausgezeichneten Reputation von Puppet und der möglichen Vollautomatisierung (ein heiliger Gral von Systemadministratoren, leider wird dies manchmal eher als Bedrohung ihres Jobs wahrgenommen) entschieden wir uns für eine Evaluation dieser Software. Der Preis für die Flexibilität und die überragende Mächtigkeit dieses Instruments ist allerdings am Anfang ziemlich hoch: Nicht wegen dem Kaufpreis, es handelt sich ja um Opensource, aber die Lernkurve ist am Anfang wirklich sehr steil. So steil, dass ich mich die erste Woche mehr als einmal gefragt habe: Sollen wir wirklich? Lohnt sich das überhaupt für unsere 20 Linux-Server? Vielleicht ist es gar nicht so schlecht, dass die Einstiegshürde so hoch ist, damit wird sichergestellt, dass sich nur eingehend damit beschäftigt, wer genau weiss, was er tut (das ist auch bei Linux-Administratoren nicht automatisch der Fall <img src='http://www.linuxhome.ch/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  und was genau mit dem Werkzeug erreicht werden soll.</p>
<p>Nach zwei Wochen intensiven Lernens (Meistens &#8220;Learning by doing&#8221;, denn praktisch alle  Puppet-Bücher sind nicht wirklich brauchbar) ist die Antwort Ja. Ja, und es würde sich sogar nur mit 10 Servern bereits lohnen. Denn nach dem hohen Lernaufwand am Anfang wird das Erstellen von neuen Modulen und Konfigurationen immer einfacher und schneller. Es macht Spass, ein derartig mächtiges Werkzeug immer besser zu beherrschen. Zudem gibt es eine Unmenge von<a title="Puppet Module Forge" href="http://forge.puppetlabs.com/" target="_blank"> vorgefertigten Modulen</a> auf den entsprechenden Internetseiten zu finden. Und nicht zuletzt lernt man sehr viel über deklarative Sprachen und wenn man will, kann man zugleich noch etwas Ruby Programmieren lernen, je nachdem wie tief man in die Materie eintauchen möchte.</p>
<p>Einen anständigen Workflow vorausgesetzt kommt man unter Umständen auch intensiv mit Git in Kontakt, was einem den zukünftigen Umgang mit diesem ebenfalls sehr mächtigen Versionierungswerkzeug in anderen Projekten sehr einfach macht (Projektleiter, ob kommerziell oder Opensource sind froh, wenn sie nicht erst einen Kurs in &#8220;Git basics&#8221; geben müssen). Als kleiner Bonus wird durch den Workflow auch ein Produktionsrelease-Zyklus eingaführt mit Test und Live-Szenarien, ohne die sonst üblichen Probleme eines Paradigmen- oder Prozesswechsels (Akzeptanz u.ä.), denn die ersten Früchte der Arbeit werden sehr schnell auch individuell spürbar. Für vorsichtigere Naturen lohnt sich bestimmt ein Blick auf die <a title="Puppet Enterprise" href="http://puppetlabs.com/puppet/puppet-enterprise/" target="_blank">Enterprise-Version</a>, mit der die Installation vollautomatisch auf Klick funktioniert und für die man auch offiziellen Support bekommt.</p>
<p>Warum scheuen dennoch viele Administratoren den Weg in die Vollautomatisierung? Bei einigen herrscht wohl noch die altbekannte Angst vor dem Überflüssigwerden des Administrators vor. Dies lässt sich in der Welt der proprietären Betriebssysteme und Software (aber nicht nur dort) sehr häufig beobachten, wo selbst einfachste, repetierbare Aufgaben teilweise Stunden benötigen. Mein Argument gegen diese Angst ist, dass wir unseren Job nicht machen, um langweilige, wiederholende und im Prinzip immergleiche Arbeiten zu machen. Dazu kann man auch Roboter (sprich: Puppet) nehmen! Als Systemadministrator / Entwickler etc. hat man genug zu tun, kreative und nachhaltige Lösungen für nicht-alltägliche Probleme zu finden. Diese Arbeit ist essentiell das, was uns  implizit motiviert! Typische Situation: Die ersten 10 Apache-Installationen macht man noch gerne, weil man lernt und das Ergebnis sehen kann. Danach merkt man schnell, dass es immer dasselbe ist. Warum also nicht automatisieren? Jeder Systemadministrator den ich kenne, hat eine Liste mit 100+ interessanteren Problemen und Ideen, welche ihrer Firma einen echten Mehrwert bieten würden, wenn man denn neben den alltäglichen Aufgaben Zeit für sie fände. Deshalb ist Automatisierung keine Bedrohung sondern eine Befreiung! Sobald man mit vielen Servern zu tun hat, muss man sich ohnehin überlegen, wie man diese effizient und nachvollziehbar gleich aufsetzen und administrieren will, daher ist in grösseren Firmen diese Form der Automatisierung schon eher ein Sachzwang, will man motivierte und kreative anstatt genervte Administatoren.</p>
<p>Im Gegensatz zu cfengine besteht derzeit leider noch keine offizielle <a title="Puppet on Windows" href="http://projects.puppetlabs.com/projects/puppet/wiki/Puppet_Windows" target="_blank">Windows-Unterstützung.</a> Es sind jedoch Entwicklungen im Gange, dass bald die wichtigsten Ressourcen durch einen Windows-Agent realisiert werden können.</p>
<p>In diesem Sinne gelte das alte Administratoren-Credo: Wenn eine Arbeit automatisch erledigt werden kann, soll man keinen Menschen damit belästigen. Denn Menschen haben Interessanteres zu tun.</p>
<address>[1] <a href="http://cfengine.com/">http://cfengine.com/</a></address>
<address>[2] <a href="http://projects.puppetlabs.com/projects/puppet/wiki/Whos_Using_Puppet">http://projects.puppetlabs.com/projects/puppet/wiki/Whos_Using_Puppet</a></address>
<address>[3] <a href="http://forge.puppetlabs.com/">http://forge.puppetlabs.com/<br />
</a>[4] <a href="http://puppetlabs.com/puppet/puppet-enterprise/">http://puppetlabs.com/puppet/puppet-enterprise/</a><a href="http://forge.puppetlabs.com/"><br />
</a>[4] <a href="http://projects.puppetlabs.com/projects/puppet/wiki/Puppet_Windows">http://projects.puppetlabs.com/projects/puppet/wiki/Puppet_Windows</a></address>
]]></content:encoded>
			<wfw:commentRss>http://www.linuxhome.ch/linux/automatisierte-server-infrastruktur-mit-puppet-in-der-kmu-lohnt-sich-das/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sexy Shellskripte dank kdialog</title>
		<link>http://www.linuxhome.ch/geek/sex-shellskripte-dank-kdialog/</link>
		<comments>http://www.linuxhome.ch/geek/sex-shellskripte-dank-kdialog/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 16:10:10 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[geek]]></category>
		<category><![CDATA[programmierung]]></category>
		<category><![CDATA[KDE]]></category>
		<category><![CDATA[shellskript]]></category>

		<guid isPermaLink="false">http://www.linuxhome.ch/?p=229</guid>
		<description><![CDATA[Shellskripte müssen nicht zwangsläufig an die Shell in der Konsole gebunden sein. Sie können auch ansprechend verpackt werden für Otto Normaluser. Dazu gibt es verschiedenste Ansätze. Einer der modernsten und flexibelsten ist kdialog unter KDE4. Das Besondere an kdialog ist seine Fähigkeit zur Interprozess-Kommunikation (IPC) via DBus. DBus wurde von freedesktop.org entwickelt und ist mittlerweile [...]]]></description>
			<content:encoded><![CDATA[<img style='float: left; margin-right: 10px; border: none;' src='http://www.gravatar.com/avatar.php?gravatar_id=d6b1294fe73eea0bc9dc84a732e5e880&amp;default=http://use.perl.org/images/pix.gif' alt='No Gravatar' width=40 height=40/><p>Shellskripte müssen nicht zwangsläufig an die Shell in der Konsole gebunden sein. Sie können auch ansprechend verpackt werden für Otto Normaluser. Dazu gibt es verschiedenste Ansätze. Einer der modernsten und flexibelsten ist kdialog unter KDE4. Das Besondere an kdialog ist seine Fähigkeit zur Interprozess-Kommunikation (IPC) via DBus. DBus wurde von freedesktop.org entwickelt und ist mittlerweile bei fast allen Distributionen standardmäßig vorhanden. In KDE 3 kam dafür das KDE-eigene DCOP zum Einsatz. Noch früher in KDE 1 wurde eine Corba-Implementiereung verwendet. Beide Lösungen waren jedoch zu gross und umständlich.</p>
<p>Mein Beispielskript hier zeigt, wie man einen Fortschrittsbalken implementiert, welcher via DBus vom laufenden Shellskript gesteuert werden kann. Natürlich kann man via DBus noch viele weitere mehr oder weniger nützliche oder witzige Dinge machen mit dem jeweiligen Dialog, wie z.B. den Titel und Inhalt laufend verändern etc.</p>
<p>Solche Skripte müssen übrigens nicht auf KDE beschränkt sein. Es sollte schon mittels installierten kdelibs und natürlich kdialog möglich sein, dieselben unter Gnome, XFCE etc.laufen zu lassen, obwohl dort der Einsatz von zenity (GTK-basiert) eher angebracht wäre. Zenity ist praktisch in jeder Distribution installiert. Nur Leider fehlt dort der Progress-Callback resp. die Interprozess-Kommunikation. Zenity&#8217;s Progressbar ist dadurch eher eine simple Aktivitätsanzeige. Allen Alternativen zu kdialog ist gemein, dass sie die erwähnte Progressbar lediglich indirekt implementieren, z.B. indem sie die Prozentwerte des Balkens entgegennehmen oder die Aktivität während einer Operation anzeigen, Beispiel:</p>
<pre>find /usr/bin | zenity --progress --pulsate</pre>
<p>Durch die Verwendung von DBus können wir vielleicht bald auf eine wirklich Desktopunabhängige Lösung hoffen, indem z.B. auch zenity die Dialoge über DBus ansteuerbar macht&#8230;</p>
<p>Natürlich halten damit auch wieder die sleep-Anweisungen Einzug, damit nicht alle Text vorbeihuschen. Sie verlangsamen den Ablauf zwar insgesamt, aber bei einem Prozedur wie &#8220;USB-Stick entschlüsseln und einhängen&#8221; ist dies meines erachtens vernachläßigbar zu gunsten der Lesbarkeit der Infotexte.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/env bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># TODO: Dieses Skript sollte durch das udev-System aufgerufen werden</span>
<span style="color: #666666; font-style: italic;"># siehe dazu /etc/udev/rules.d/90-crypto-usb-stick.rules</span>
&nbsp;
<span style="color: #007800;">USER</span>=myuser
<span style="color: #007800;">DEVICE</span>=<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>usbstick
<span style="color: #007800;">MAPPERDIR</span>=<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>mapper<span style="color: #000000; font-weight: bold;">/</span>crypto_usbstick
<span style="color: #007800;">MOUNTDIR</span>=<span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span>crypto_usbstick
&nbsp;
<span style="color: #007800;">dbusRef</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">kdialog</span> <span style="color: #660033;">--title</span> <span style="color: #ff0000;">&quot;Crypto-USB-Stick aktivieren und einhängen&quot;</span> <span style="color: #660033;">--progressbar</span> <span style="color: #ff0000;">&quot;Starte...&quot;</span> <span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #c20cb9; font-weight: bold;">kdialog</span> <span style="color: #660033;">--password</span> <span style="color: #ff0000;">&quot;Bitte Passwort für Crypto-USB-Stick eingeben:&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> cryptsetup luksOpen <span style="color: #007800;">$DEVICE</span> <span style="color: #800000;">${MAPNAME}</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #c20cb9; font-weight: bold;">kdialog</span> <span style="color: #660033;">--error</span> <span style="color: #ff0000;">&quot;Konnte das Cryptodevice nicht erstellen! Vermutlich falsches Passwort eingeben?&quot;</span>
    qdbus <span style="color: #007800;">$dbusRef</span> org.kde.kdialog.ProgressDialog.close
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1</span>
&nbsp;
qdbus <span style="color: #007800;">$dbusRef</span> org.kde.kdialog.ProgressDialog.setLabelText <span style="color: #ff0000;">&quot;Verzeichnis <span style="color: #007800;">$MOUNTDIR</span> wird gesucht&quot;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-e</span> <span style="color: #007800;">$MOUNTDIR</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    qdbus <span style="color: #007800;">$dbusRef</span> org.kde.kdialog.ProgressDialog.setLabelText <span style="color: #ff0000;">&quot;Verzeichnis <span style="color: #007800;">$MOUNTDIR</span> wird erstellt&quot;</span>
    <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #007800;">$MOUNTDIR</span>
<span style="color: #000000; font-weight: bold;">fi</span>
qdbus <span style="color: #007800;">$dbusRef</span> Set org.kde.kdialog.ProgressDialog value <span style="color: #000000;">1</span>
<span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1</span>
&nbsp;
qdbus <span style="color: #007800;">$dbusRef</span> org.kde.kdialog.ProgressDialog.setLabelText <span style="color: #ff0000;">&quot;Cryptodevice wird nach <span style="color: #007800;">$MOUNTDIR</span> eingehängt&quot;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #c20cb9; font-weight: bold;">mount</span> <span style="color: #007800;">$MAPPERDIR</span> <span style="color: #007800;">$MOUNTDIR</span> -orw,user,<span style="color: #7a0874; font-weight: bold;">exec</span>,<span style="color: #007800;">uid</span>=<span style="color: #007800;">$USER</span>,<span style="color: #007800;">gid</span>=<span style="color: #007800;">$USER</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #c20cb9; font-weight: bold;">kdialog</span> <span style="color: #660033;">--error</span> <span style="color: #ff0000;">&quot;Konnte den Crypto-USB-Stick nicht in <span style="color: #007800;">$MOUNTDIR</span> einhängen!&quot;</span>
    qdbus <span style="color: #007800;">$dbusRef</span> org.kde.kdialog.ProgressDialog.close
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1</span>
&nbsp;
qdbus <span style="color: #007800;">$dbusRef</span> Set org.kde.kdialog.ProgressDialog value <span style="color: #000000;">2</span>
qdbus <span style="color: #007800;">$dbusRef</span> org.kde.kdialog.ProgressDialog.setLabelText <span style="color: #ff0000;">&quot;Vorgang erfolgreich beendet!&quot;</span>
<span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">2</span>
&nbsp;
qdbus <span style="color: #007800;">$dbusRef</span> org.kde.kdialog.ProgressDialog.close</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.linuxhome.ch/geek/sex-shellskripte-dank-kdialog/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Perl 5.10 &#8211; was man wissen muss</title>
		<link>http://www.linuxhome.ch/programmierung/perl-510-was-man-wissen-muss/</link>
		<comments>http://www.linuxhome.ch/programmierung/perl-510-was-man-wissen-muss/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 15:45:15 +0000</pubDate>
		<dc:creator>Bo</dc:creator>
				<category><![CDATA[opensource]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[programmierung]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://www.linuxhome.ch/?p=154</guid>
		<description><![CDATA[Perl 5.10 ist nun bereits eine Weile draussen und hat Einzug auf die gängigen Linux-Distributionen gehalten. Aus diesem Grunde möchte ich an dieser Stelle einen kleinen Reminder bringen, welche Features für den Programmieraltag wirklich der Rede Wert sind. Viele der Neuerungen sind Backports aus der Designarbeit an Perl 6. Da dies keine Einführung in Perl [...]]]></description>
			<content:encoded><![CDATA[<img style='float: left; margin-right: 10px; border: none;' src='http://www.gravatar.com/avatar.php?gravatar_id=576c71ed74da11fdd1ca2c17617b2ee8&amp;default=http://use.perl.org/images/pix.gif' alt='No Gravatar' width=40 height=40/><p>Perl 5.10 ist nun bereits eine Weile draussen und hat Einzug auf die gängigen Linux-Distributionen gehalten. Aus diesem Grunde möchte ich an dieser Stelle einen kleinen Reminder bringen, welche Features für den Programmieraltag wirklich der Rede Wert sind. Viele der Neuerungen sind Backports aus der Designarbeit an Perl 6.</p>
<p>Da dies keine Einführung in Perl darstellt, sondern eine Info an Cracks, Admins und solche die es werden wollen, gehe ich nicht auf die Details ein. Ich schlage vor die Code-Beispiele jeweils in die Kommandozeile zu kopieren und zu damit herum zu spielen.</p>
<p>Wenn man die neuen Features benutzen will muss man dies im Skript deklarieren.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">use</span> feature <span style="color: #ff0000;">':5.10'</span><span style="color: #339933;">;</span></pre></div></div>

<p><span id="more-154"></span></p>
<h2>Klein aber Oho</h2>
<p>Es gibt viele kleine Änderungen die es aber in sich haben. Ich möchte hier vier herauspicken die sich ganz besonders lohnen.</p>
<h3>Sag es</h3>
<p>Die kleinste, grösste, simpelste und wichtigste Änderung ist ganz klar die Funktion say. Endlich kann man eine Zeile ohne grosse Schreibarbeit ausgeben!</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">say <span style="color: #ff0000;">'say means: never ever again print &quot;bla\n&quot;!'</span><span style="color: #339933;">;</span></pre></div></div>

<p>Man mag diese Änderung zwar belächeln. Aber es ist doch wichtig, das dies endlich geschehen ist&#8230; und wie ich finde ist es sehr elegant gelungen.</p>
<h3><span style="x-small;">Zeilen lesen</span></h3>
<p>Eine weitere Nettigkeit ist, das nun die Funktion readline wie der diamond operator (&lt;&gt;) agiert. Nun geht das Einlesen einiges intuitiver von der Hand:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">say <span style="color: #ff0000;">'press ctrl+d to resume...'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">readline</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">print</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Clevere Vergleiche</h3>
<p>Es steht nun ein sogenannter &#8220;smart match operator&#8221; zur Verfügung. Er entscheidet selber &#8220;intelligent&#8221; wie Strings und Zahlen miteinander verglichen werden sollen.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">say <span style="color: #ff0000;">'0 equals &quot;abc&quot; if compared with =='</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">==</span> <span style="color: #ff0000;">&quot;abc&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
say <span style="color: #ff0000;">'But 0 differs from &quot;abc&quot; if using ~~ &quot;the smart operator&quot;'</span> <span style="color: #b1b100;">unless</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">~~</span> <span style="color: #ff0000;">&quot;abc&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Defined or</h3>
<p>Ein neuer Operator ermöglicht einfach Verkettungen. Das besondere ist, das er undef statt false/true auswertet.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">sub</span> a <span style="color: #009900;">&#123;</span> <span style="color: #000066;">return</span> <span style="color: #000066;">undef</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">sub</span> b <span style="color: #009900;">&#123;</span> <span style="color: #000066;">return</span> <span style="color: #ff0000;">'b is defined!'</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">sub</span> c <span style="color: #009900;">&#123;</span> <span style="color: #000066;">return</span> <span style="color: #000066;">undef</span> <span style="color: #009900;">&#125;</span>
say a<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">//</span> b<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">//</span> c<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Mal ganz was neues</h2>
<p>Neben den kleinen erwähnten Nettigkeiten gibt es aber auch grössere Ergänzungen und Änderungen. Es sind deren zwei die mir auffallen.</p>
<h3>Ein &#8220;kein&#8221; Switch-Statement</h3>
<p>Perl hat endlich ganz offiziell sein eigenes Switch-Statement. Doch wie könnte es anders sein, das Statement heisst nicht switch sondern given. Das ganze sieht ziemlich gut aus und kann sehr flexibel angewendet werden.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">say <span style="color: #ff0000;">'Tell me something for my switch-statement:'</span><span style="color: #339933;">;</span>
<span style="color: #000066;">chomp</span> <span style="color: #009900;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$input</span> <span style="color: #339933;">=</span> <span style="color: #000066;">readline</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
given <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$input</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    when <span style="color: #009900;">&#40;</span><span style="color: #000066;">undef</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> say <span style="color: #ff0000;">'what the heck!'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    when <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'perl'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> say <span style="color: #ff0000;">'you name it!'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    when <span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\d+/</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> say <span style="color: #ff0000;">'nice number...'</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    say <span style="color: #ff0000;">'you type boring stuff!'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Das Statement ist sehr flexibel, da der Mechanismus dahinter simpel ist. given setzt $_ und when wertet dieselbe aus.</p>
<h2>Neue Regex Engine!</h2>
<p>Die wichtigste Änderung mit dem grössten Impact dürfte die Neuimplementation der Regex-Engine sein. Details wie z.B. das sie nun iterativ statt rekursiv implementiert ist hören sich nett an, ändern aber wenig am Programmieraltag. Das beste Feature für mich ist ganz klar, dass man Teile des Regex nun benennen kann und sich so übersichtlicher Ausdrücke verschachteln lassen.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">say <span style="color: #ff0000;">'type any brackets to check if they are consistent'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$regex</span> <span style="color: #339933;">=</span> <span style="color: #000066;">qr</span><span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;amp</span><span style="color: #339933;">;</span>BRA<span style="color: #009900;">&#41;</span>     <span style="color: #666666; font-style: italic;"># 'run' the pattern</span>
            <span style="color: #666666; font-style: italic;">#</span>
<span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #009900;">&#40;</span>DEFINE<span style="color: #009900;">&#41;</span>  <span style="color: #666666; font-style: italic;"># Define section</span>
<span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;lt</span><span style="color: #339933;">;</span>SIGN<span style="color: #0000ff;">&amp;gt</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;"># Pattern declaration of signs</span>
 <span style="color: #009900;">&#91;</span><span style="color: #339933;">^</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span>      <span style="color: #666666; font-style: italic;"># everithing except brackets</span>
<span style="color: #009900;">&#41;</span>           <span style="color: #666666; font-style: italic;">#</span>
<span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;lt</span><span style="color: #339933;">;</span>BRA<span style="color: #0000ff;">&amp;gt</span><span style="color: #339933;">;</span>     <span style="color: #666666; font-style: italic;"># Recursive pattern declaration of brackets</span>
 <span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;amp</span><span style="color: #339933;">;</span>SIGN<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>  <span style="color: #666666; font-style: italic;"># any signs or nothing</span>
 \<span style="color: #009900;">&#40;</span>         <span style="color: #666666; font-style: italic;"># followed by a opening bracket</span>
 <span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;amp</span><span style="color: #339933;">;</span>SIGN<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>  <span style="color: #666666; font-style: italic;"># possibly some further signs</span>
 <span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;amp</span><span style="color: #339933;">;</span>BRA<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>   <span style="color: #666666; font-style: italic;"># recursive call to deeper nested brackets</span>
 <span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;amp</span><span style="color: #339933;">;</span>SIGN<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>  <span style="color: #666666; font-style: italic;"># again there may be some stuff between</span>
 \<span style="color: #009900;">&#41;</span>         <span style="color: #666666; font-style: italic;"># now we need a closing bracket</span>
 <span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;amp</span><span style="color: #339933;">;</span>SIGN<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>  <span style="color: #666666; font-style: italic;"># and let again space for some signs</span>
 <span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;amp</span><span style="color: #339933;">;</span>BRA<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>   <span style="color: #666666; font-style: italic;"># recursice call to brackets for outside nested ones</span>
<span style="color: #009900;">&#41;</span>           <span style="color: #666666; font-style: italic;">#</span>
<span style="color: #009900;">&#41;</span>           <span style="color: #666666; font-style: italic;"># End of the define section</span>
<span style="color: #009900;">&#125;</span>xms<span style="color: #339933;">;</span>       <span style="color: #666666; font-style: italic;"># End of the regex declaration</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$in</span> <span style="color: #339933;">=</span> <span style="color: #000066;">readline</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$in</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/ ^ $regex $ /xms</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    say <span style="color: #ff0000;">'Your input is valid'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    say <span style="color: #ff0000;">'Your input is NOT valid'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Dieses Beispiel findet heraus ob die Klammerung vollständig ist. Die Implementation ist von mir, also bitte nicht produktiv verwenden. Ich gebe keine Gewähr <img src='http://www.linuxhome.ch/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> <br />
Wer Besonders gut aufgepasst hat merkt, das dieser Regex rekursiv arbeitet. BRA wird in seiner eigenen Definition wieder verwendet.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.linuxhome.ch/programmierung/perl-510-was-man-wissen-muss/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Singleton-Pattern in Perl und Ruby</title>
		<link>http://www.linuxhome.ch/geek/singleton-pattern-in-perl-und-ruby/</link>
		<comments>http://www.linuxhome.ch/geek/singleton-pattern-in-perl-und-ruby/#comments</comments>
		<pubDate>Sat, 09 Feb 2008 18:31:32 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[geek]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[programmierung]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.linuxhome.ch/ruby/singleton-pattern-in-perl-und-ruby/</guid>
		<description><![CDATA[Als auf der Arbeit einmal Langeweile angesagt war, diskutierte ich mit meinem lieben Arbeitskollegen Boris über Software-Patterns (Entwurfsmuster). Da kam uns die Idee, ein paar Patterns in unserer jeweils bevorzugten Programmiersprache umzusetzen. Als erstes, da relativ einfach: Singleton, also beispielsweise eine Klasse, die sich nur einmal instanzieren lässt und eine Funktion bereitstellt, die diese Instanz [...]]]></description>
			<content:encoded><![CDATA[<img style='float: left; margin-right: 10px; border: none;' src='http://www.gravatar.com/avatar.php?gravatar_id=d6b1294fe73eea0bc9dc84a732e5e880&amp;default=http://use.perl.org/images/pix.gif' alt='No Gravatar' width=40 height=40/><p>Als auf der Arbeit einmal Langeweile angesagt war, diskutierte ich mit meinem lieben Arbeitskollegen Boris über <a title="Entwurfsmuster" href="http://de.wikipedia.org/wiki/Entwurfsmuster">Software-Patterns (Entwurfsmuster)</a>. Da kam uns die Idee, ein paar Patterns in unserer jeweils bevorzugten Programmiersprache umzusetzen.</p>
<p>Als erstes, da relativ einfach: <a title="Singleton Pattern" href="http://de.wikipedia.org/wiki/Singleton_%28Entwurfsmuster%29">Singleton</a>, also beispielsweise eine Klasse, die sich nur einmal instanzieren lässt und eine Funktion bereitstellt, die diese Instanz zurückgibt (&#8220;new&#8221; oder &#8220;instance&#8221;).</p>
<p><span id="more-45"></span></p>
<p>Er überraschte mich schon kurz danach mit einer Implementation in Perl, die er ohne Online-Hilfe einfach so mal kurz geschrieben hat. Ich war von den Socken, denn Singleton wird in Perl nicht direkt angeboten. Da hat sich sein großes Talent wieder mal gezeigt. Gleichzeitig war ich froh, dass ich in Ruby schon Schnittstellen für verschiedenste Pattern zur Verfügung habe, denn ich bin nicht wirklich der geborene Programmierer.</p>
<p>Hier die Boris&#8217; Perl-Version, in Form eines Moduls und eines Testskriptes, bitte erwartet keine genaue Erklärungen von mir, was z.B. die Funktion &#8220;bless&#8221; genau macht usw.:</p>
<p>einzelgaenger.pm:</p>
<pre>package einzelgaenger;

use strict;

my $ref = 0;
my $class = '';

sub getInstance {
        $class = shift;
        unless ($ref) {
                $ref = {};
                bless($ref,$class);
        }
        return($ref);
}

sub say {
        my $this = shift;
        if ($this-&gt;{'hit'}) {
                print "AUTSCH! Ich sag nichts mehr, du!\n";
        }
        else {
                print "Ich bin ein $class. Mich gibt es nur einmal, egal
wie oft du mich instanzierst, du bekommst immer: $this\n";
        }
}

sub hit {
        my $this = shift;
        $this-&gt;{'hit'} = 1;
        print "*klaps!*\n";
}

1;</pre>
<p>einzelgaenger-test.pl:</p>
<pre>use strict;
use einzelgaenger;

print &lt;&lt;TEXT;

Wulli &#038; Peter  -  Das Singleton-Pattern
***************************************

TEXT

print "Wir machen uns einen Wulli.\n";       my $wulli =
einzelgaenger-&gt;getInstance();
print "Wir machen uns einen Peter.\n\n";     my $peter =
einzelgaenger-&gt;getInstance();

print "Wulli sagt: ";                        $wulli-&gt;say();
print "Peter sagt: ";                        $peter-&gt;say();

print "\nWir schlagen Wulli...  ";           $wulli-&gt;hit();
print "Peter sagt: ";                        $peter-&gt;say();

print "\nWulli und Peter sind ein und der selbe!\n";

print "\n";
exit 0;</pre>
<p>Hier der Output:</p>
<pre> Wulli &#038; Peter  -  Das Singleton-Pattern
***************************************

Wir machen uns einen Wulli.
Wir machen uns einen Peter.

Wulli sagt: Ich bin ein einzelgaenger. Mich gibt es nur einmal, egal
wie oft du mich instanzierst, du bekommst immer: einzelgaenger=HASH(0x8968c28)
Peter sagt: Ich bin ein einzelgaenger. Mich gibt es nur einmal, egal
wie oft du mich instanzierst, du bekommst immer: einzelgaenger=HASH(0x8968c28)

Wir schlagen Wulli...  *klaps!*
Peter sagt: AUTSCH! Ich sag nichts mehr, du!

Wulli und Peter sind ein und der selbe!</pre>
<p>Und hier dasselbe in Ruby. Es zeigt wieder mal, dass Ruby die ideale Sprache ist für schreib- und denkfaule Admins wie ich einer bin:</p>
<pre>#!/usr/bin/env ruby

require 'singleton'

class Einzelgaenger

  include Singleton

  def id_ausgeben
    self.object_id
  end

end

peter = Einzelgaenger.instance
wullie = Einzelgaenger.instance

print "Peter sagt: \"Hallo, ich bin der Peter, meine ID ist: #{peter.id_ausgeben}!\"\n"
print "Wullie meint dazu: \"Hmmm...seltsam, mein ID ist: #{wullie.id_ausgeben}!?!\"\n"

print "\nPeter und Wullie sind also ein und derselbe! Von Einzelgaenger kann keine weitere Instanz gebildet werden\n"</pre>
<p>Hier der Output:</p>
<pre>Peter sagt: "Hallo, ich bin der Peter, meine ID ist: -604372568!"
Wullie meint dazu: "Hmmm...seltsam, meine ID ist: -604372568!?!"

Peter und Wullie sind also ein und derselbe! Von Einzelgaenger kann keine weitere Instanz gebildet werden.</pre>
<p>Als nächstes war dann das <a title="Beobachter-Muster" href="http://de.wikipedia.org/wiki/Beobachter_%28Entwurfsmuster%29">Beobachter-Muster</a> dran, aber dazu später mehr&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.linuxhome.ch/geek/singleton-pattern-in-perl-und-ruby/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
