<?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, 29 Jul 2010 22:14:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=5766</generator>
		<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[Perl]]></category>
		<category><![CDATA[opensource]]></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[Perl]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[geek]]></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! -->