<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="FeedCreator 1.8" -->
<?xml-stylesheet href="https://dsss.be/w/lib/exe/css.php?s=feed" type="text/css"?>
<rdf:RDF
    xmlns="http://purl.org/rss/1.0/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
    xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel rdf:about="https://dsss.be/w/feed.php">
        <title>Tyler&#039;s wiki - perl</title>
        <description></description>
        <link>https://dsss.be/w/</link>
        <image rdf:resource="https://dsss.be/w/_media/wiki:dokuwiki.svg" />
       <dc:date>2026-07-05T22:22:47+00:00</dc:date>
        <items>
            <rdf:Seq>
                <rdf:li rdf:resource="https://dsss.be/w/perl:ansi_color?rev=1252081217&amp;do=diff"/>
                <rdf:li rdf:resource="https://dsss.be/w/perl:command_line_options_and_usage_with_getopt_long?rev=1250182622&amp;do=diff"/>
                <rdf:li rdf:resource="https://dsss.be/w/perl:command_line_options_and_usage_with_getopts?rev=1250182620&amp;do=diff"/>
                <rdf:li rdf:resource="https://dsss.be/w/perl:command_line_options_with_usage?rev=1250182605&amp;do=diff"/>
                <rdf:li rdf:resource="https://dsss.be/w/perl:dns_stuff?rev=1250182627&amp;do=diff"/>
                <rdf:li rdf:resource="https://dsss.be/w/perl:for_each_file?rev=1250182604&amp;do=diff"/>
                <rdf:li rdf:resource="https://dsss.be/w/perl:forking_server?rev=1250182602&amp;do=diff"/>
                <rdf:li rdf:resource="https://dsss.be/w/perl:index?rev=1262987455&amp;do=diff"/>
                <rdf:li rdf:resource="https://dsss.be/w/perl:kvpff?rev=1250182600&amp;do=diff"/>
                <rdf:li rdf:resource="https://dsss.be/w/perl:loop_with_timekeeping?rev=1250182598&amp;do=diff"/>
                <rdf:li rdf:resource="https://dsss.be/w/perl:make_a_loop_parallel?rev=1250182625&amp;do=diff"/>
                <rdf:li rdf:resource="https://dsss.be/w/perl:prototype_class_for_oo?rev=1262987538&amp;do=diff"/>
                <rdf:li rdf:resource="https://dsss.be/w/perl:read_password_without_echo?rev=1250182594&amp;do=diff"/>
                <rdf:li rdf:resource="https://dsss.be/w/perl:real_csv_parsing?rev=1250182632&amp;do=diff"/>
                <rdf:li rdf:resource="https://dsss.be/w/perl:sets?rev=1250182629&amp;do=diff"/>
                <rdf:li rdf:resource="https://dsss.be/w/perl:single_document_web_server?rev=1250182596&amp;do=diff"/>
            </rdf:Seq>
        </items>
    </channel>
    <image rdf:about="https://dsss.be/w/_media/wiki:dokuwiki.svg">
        <title>Tyler's wiki</title>
        <link>https://dsss.be/w/</link>
        <url>https://dsss.be/w/_media/wiki:dokuwiki.svg</url>
    </image>
    <item rdf:about="https://dsss.be/w/perl:ansi_color?rev=1252081217&amp;do=diff">
        <dc:format>text/html</dc:format>
        <dc:date>2009-09-04T16:20:17+00:00</dc:date>
        <dc:creator>Anonymous (anonymous@undisclosed.example.com)</dc:creator>
        <title>ansi_color</title>
        <link>https://dsss.be/w/perl:ansi_color?rev=1252081217&amp;do=diff</link>
        <description>if you want to avoid installing Term::ANSIColor:


sub ansi {
	my ($c) = @_;
	if (!$c) { return &quot;\e[m&quot;; }
	my %colors = qw(
		reset       0
		bold        1
		underline   4
		inverse     7
		black      30
		red        31
		green      32
		yellow     33
		blue       34
		violet     35
		magenta    35
		cyan       36
		white      37
		on_black   40
		on_red     41
		on_green   42
		on_yellow  43
		on_blue    44
		on_violet  45
		on_magenta 45
		on_cyan    46
		on_white   47
	);
	my $r = join(&#039;;&#039;,ma…</description>
    </item>
    <item rdf:about="https://dsss.be/w/perl:command_line_options_and_usage_with_getopt_long?rev=1250182622&amp;do=diff">
        <dc:format>text/html</dc:format>
        <dc:date>2009-08-13T16:57:02+00:00</dc:date>
        <dc:creator>Anonymous (anonymous@undisclosed.example.com)</dc:creator>
        <title>command_line_options_and_usage_with_getopt_long</title>
        <link>https://dsss.be/w/perl:command_line_options_and_usage_with_getopt_long?rev=1250182622&amp;do=diff</link>
        <description>#!/usr/bin/perl

use strict;
use Getopt::Long;

# declare option-related vars
my $bVerbose;
my $somevalue;

# allow &quot;-se&quot; to mean &quot;-s -e&quot;, also makes single-letter options case sensitive
Getopt::Long::Configure (&quot;bundling&quot;); 

# stop parsing after the first non-option; needed to let you pass options 
# in the host command
Getopt::Long::Configure (&quot;require_order&quot;);

# parse command line options
exit unless GetOptions(
	&quot;help&quot; =&gt; sub { usage(); },
	&quot;verbose|v&quot; =&gt; \$bVerbose,
	&quot;number|n=i&quot; =&gt; sub {…</description>
    </item>
    <item rdf:about="https://dsss.be/w/perl:command_line_options_and_usage_with_getopts?rev=1250182620&amp;do=diff">
        <dc:format>text/html</dc:format>
        <dc:date>2009-08-13T16:57:00+00:00</dc:date>
        <dc:creator>Anonymous (anonymous@undisclosed.example.com)</dc:creator>
        <title>command_line_options_and_usage_with_getopts</title>
        <link>https://dsss.be/w/perl:command_line_options_and_usage_with_getopts?rev=1250182620&amp;do=diff</link>
        <description>#!/usr/bin/perl

use Getopt::Std;

my %opt;
getopts( &#039;afqcrh&#039;, \%opt ) or usage();
usage() if ($opt{h} or !@ARGV);

sub usage() {
	(my $progName = $0) =~ s#.*[/\\]##;
	print &lt;&lt;EODOC;
TITLE

INFO

Usage:
    $progName [-a | -f | -q] [-r] [-c] [-h] [files]

Options:
  -a Stuff
  -f Stuff
  -q Stuff

EODOC
	exit;
}</description>
    </item>
    <item rdf:about="https://dsss.be/w/perl:command_line_options_with_usage?rev=1250182605&amp;do=diff">
        <dc:format>text/html</dc:format>
        <dc:date>2009-08-13T16:56:45+00:00</dc:date>
        <dc:creator>Anonymous (anonymous@undisclosed.example.com)</dc:creator>
        <title>command_line_options_with_usage</title>
        <link>https://dsss.be/w/perl:command_line_options_with_usage?rev=1250182605&amp;do=diff</link>
        <description># Tyler Bletsch
use strict;

# Get command line options
while ($ARGV[0] =~ /^-(.)(.*)/) {
	shift;
	my ($opt,$param) = ($1,$2);
	
	if ($opt eq &#039;a&#039;) { next; }
	elsif ($opt eq &#039;b&#039;) { next; }
	else { &amp;usage }
}

# Check for existance of remaining arguments
&amp;usage unless (@ARGV);

###############################################################################

sub usage() {
	(my $progName = $0) =~ s#.*[/\\]##;
	print &lt;&lt;EODOC;
TITLE

Purpose

Usage:
    $progName [options] [arguments]

Options:
    a …</description>
    </item>
    <item rdf:about="https://dsss.be/w/perl:dns_stuff?rev=1250182627&amp;do=diff">
        <dc:format>text/html</dc:format>
        <dc:date>2009-08-13T16:57:07+00:00</dc:date>
        <dc:creator>Anonymous (anonymous@undisclosed.example.com)</dc:creator>
        <title>dns_stuff</title>
        <link>https://dsss.be/w/perl:dns_stuff?rev=1250182627&amp;do=diff</link>
        <description>use Socket;

sub lookup {
	my ($a) = @_;
	return is_ip($a) ? ip2host($a)||$a : host2ip($a)||$a;
}

sub is_ip { $_[0] =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ }

sub ip2host {
	my ($a) = @_;
	return gethostbyaddr(inet_aton($a), AF_INET) || undef;
}

sub host2ip {
	my ($a) = @_;
	my $hrec = (gethostbyname($a))[4] or return undef;
	return inet_ntoa($hrec);
}</description>
    </item>
    <item rdf:about="https://dsss.be/w/perl:for_each_file?rev=1250182604&amp;do=diff">
        <dc:format>text/html</dc:format>
        <dc:date>2009-08-13T16:56:44+00:00</dc:date>
        <dc:creator>Anonymous (anonymous@undisclosed.example.com)</dc:creator>
        <title>for_each_file</title>
        <link>https://dsss.be/w/perl:for_each_file?rev=1250182604&amp;do=diff</link>
        <description>using standard module:


use File::Find;

find({ wanted =&gt; sub {
	# for each file
	# $_                 is file name
	# $File::Find::name  is full path
	# $File::Find::dir   is directory path
}, preprocess =&gt; sub {
	# process the list of files about to be wanted()
	# $File::Find::dir   is directory path
	return @_;
}, follow =&gt; 0, # follow symlinks?
no_chdir =&gt; 0, # dont chdir (so $_ equals $File::Find::name)
}, @directories);</description>
    </item>
    <item rdf:about="https://dsss.be/w/perl:forking_server?rev=1250182602&amp;do=diff">
        <dc:format>text/html</dc:format>
        <dc:date>2009-08-13T16:56:42+00:00</dc:date>
        <dc:creator>Anonymous (anonymous@undisclosed.example.com)</dc:creator>
        <title>forking_server</title>
        <link>https://dsss.be/w/perl:forking_server?rev=1250182602&amp;do=diff</link>
        <description># Tyler Bletsch
use IO::Socket;

# Will listen on $listenPort
my $sockListen = new IO::Socket::INET (LocalPort =&gt; $listenPort, Proto =&gt; &#039;tcp&#039;, Listen =&gt; 1000, Reuse =&gt; 1);
die &quot;Could not create socket: $!\n&quot; unless $sockListen;

print &quot;Listening on port $listenPort.\n&quot;;

while (1) {
	print &quot;Waiting to accept on port $listenPort...\n&quot;;
	$sockClient = $sockListen-&gt;accept(); # Get next client
	$clientSockAddr = $sockClient-&gt;peername();
	($clientPort, $clientAddr) = sockaddr_in($clientSockAddr);
	$c…</description>
    </item>
    <item rdf:about="https://dsss.be/w/perl:index?rev=1262987455&amp;do=diff">
        <dc:format>text/html</dc:format>
        <dc:date>2010-01-08T21:50:55+00:00</dc:date>
        <dc:creator>Anonymous (anonymous@undisclosed.example.com)</dc:creator>
        <title>index</title>
        <link>https://dsss.be/w/perl:index?rev=1262987455&amp;do=diff</link>
        <description>Perl code snippets

	*  Prototype class for OO
	*  Real CSV Parsing
	*  ANSI color
	*  Sets
	*  DNS stuff
	*  Make a loop parallel
	*  Command line options and usage with Getopt Long
	*  Command line options and usage with getopts
	*  command line options with usage
	*  For each file
	*  Forking server
	*  kvpff
	*  Loop with timekeeping
	*  Single document web server
	*  Read password without echo
	*  Amazon preview leech

Blobs of code



sub max { my $r=$_[0]; for (@_) { if ($_&gt;$r) { $r=$_ } …</description>
    </item>
    <item rdf:about="https://dsss.be/w/perl:kvpff?rev=1250182600&amp;do=diff">
        <dc:format>text/html</dc:format>
        <dc:date>2009-08-13T16:56:40+00:00</dc:date>
        <dc:creator>Anonymous (anonymous@undisclosed.example.com)</dc:creator>
        <title>kvpff</title>
        <link>https://dsss.be/w/perl:kvpff?rev=1250182600&amp;do=diff</link>
        <description># Key Value Pair File Format reader/writer 1.0
# By Tyler Bletsch
use Carp;

# Format:
#    ; comment type one
#    # comment type two
#    ; 
#    ; standard key value pair
#    key1=value1
#    ; multiline value
#    key2=\
#    This is literally the data.
#    The only problem is that there is no way to escape the end symbol,
#    which is a dot alone on a line (AKA &quot;\n.\n&quot;).
#    .
#    ; if your data has an equal sign, a multiline escape is used
#    ;   (even if the data is one line)
#    …</description>
    </item>
    <item rdf:about="https://dsss.be/w/perl:loop_with_timekeeping?rev=1250182598&amp;do=diff">
        <dc:format>text/html</dc:format>
        <dc:date>2009-08-13T16:56:38+00:00</dc:date>
        <dc:creator>Anonymous (anonymous@undisclosed.example.com)</dc:creator>
        <title>loop_with_timekeeping</title>
        <link>https://dsss.be/w/perl:loop_with_timekeeping?rev=1250182598&amp;do=diff</link>
        <description>my $timeStart = time;

my $itDone=0;
my $itTotal; # Set to the total number of iterations

# LOOP BEGIN {
	
	# LOOP BODY;
	
	$itDone++;

	my $timeProc = (time() - $timeStart)||1;
	my $speed = ($itDone/$timeProc) || -1; # files/second
	my $etaS = int($itTotal - $itDone)/$speed; #seconds
	my $etaM = int($etaS/60); $etaS %= 60;
	my $etaH = int($etaM/60); $etaM %= 60; 
	printf &quot;  &gt;&gt; $itDone/$itTotal = %.1f%%, $timeProc s, %0.2f it/s, ETA %d:%02d:%02d\n&quot;,($itDone/$itTotal*100),$speed,$etaH,$etaM,$eta…</description>
    </item>
    <item rdf:about="https://dsss.be/w/perl:make_a_loop_parallel?rev=1250182625&amp;do=diff">
        <dc:format>text/html</dc:format>
        <dc:date>2009-08-13T16:57:05+00:00</dc:date>
        <dc:creator>Anonymous (anonymous@undisclosed.example.com)</dc:creator>
        <title>make_a_loop_parallel</title>
        <link>https://dsss.be/w/perl:make_a_loop_parallel?rev=1250182625&amp;do=diff</link>
        <description>Introduction

Let&#039;s say you have a loop like:


 foreach my $host (@hosts) {
    system &quot;ssh $host reboot&quot;;
 }


This is well and good, but it takes forever if you want to do 1500 clients.  Therefore, we&#039;d want to parallelize the loop.  There are two ways to go about this:</description>
    </item>
    <item rdf:about="https://dsss.be/w/perl:prototype_class_for_oo?rev=1262987538&amp;do=diff">
        <dc:format>text/html</dc:format>
        <dc:date>2010-01-08T21:52:18+00:00</dc:date>
        <dc:creator>Anonymous (anonymous@undisclosed.example.com)</dc:creator>
        <title>prototype_class_for_oo</title>
        <link>https://dsss.be/w/perl:prototype_class_for_oo?rev=1262987538&amp;do=diff</link>
        <description>Awesome.pm:


package Awesome;

use strict;

# static (class-wide) variable
my $object_count = 0;

# constructor -- a static method that creates and returns a new object
sub new {
	my $class = shift;
	# remaining args are constructor parameters.  
	# we&#039;ll assume it takes one param: name.  this parameter gets saved in our new &quot;object&quot;, a blessed hash ref
	my $self = {name =&gt; $_[0]};
	bless($self,$class);
	$object_count++; # increment a static counter
	return $self;
}

# static method -- the firs…</description>
    </item>
    <item rdf:about="https://dsss.be/w/perl:read_password_without_echo?rev=1250182594&amp;do=diff">
        <dc:format>text/html</dc:format>
        <dc:date>2009-08-13T16:56:34+00:00</dc:date>
        <dc:creator>Anonymous (anonymous@undisclosed.example.com)</dc:creator>
        <title>read_password_without_echo</title>
        <link>https://dsss.be/w/perl:read_password_without_echo?rev=1250182594&amp;do=diff</link>
        <description>sub readPassword {
	my ($prompt) = @_;
	$prompt ||= &#039;password: &#039;;
	my $password;
	if (eval &quot;require Term::ReadKey&quot;) { # runtime load
		Term::ReadKey-&gt;import(qw/ReadMode ReadLine/);
		local $|=1;
		ReadMode(&#039;noecho&#039;);
		print STDERR $prompt;
		chomp($password = ReadLine(0));
		ReadMode(&#039;normal&#039;);
		print STDERR &quot;\n&quot;;
	} else {
		print STDERR &quot;WARNING: Unable to load Term::ReadKey -- TYPED PASSWORDS WILL BE ECHOED!\n&quot;;
		print STDERR $prompt;
		chomp ($password = &lt;STDIN&gt;);
	}
	return $password;
}</description>
    </item>
    <item rdf:about="https://dsss.be/w/perl:real_csv_parsing?rev=1250182632&amp;do=diff">
        <dc:format>text/html</dc:format>
        <dc:date>2009-08-13T16:57:12+00:00</dc:date>
        <dc:creator>Anonymous (anonymous@undisclosed.example.com)</dc:creator>
        <title>real_csv_parsing</title>
        <link>https://dsss.be/w/perl:real_csv_parsing?rev=1250182632&amp;do=diff</link>
        <description>sub parse_csv {
	if (!ref $_[0]) { # if given a file, open it and recurse on the handle
		my ($filename) = @_;
		open my $fp, $filename or die &quot;$filename: $!\n&quot;;
		my $r = parse_csv($fp);
		close $fp;
		return $r;
	}
	
	my ($fp) = @_;
	my $char;
	my $status;
	# yes, we&#039;re using a single-character FSM to parse CSV in perl
	# yes, I know about split and regexes, but that doesn&#039;t help with matched quotes &amp; embedded commas
	# yes, I know about Text::CSV, but that doesn&#039;t support embedded newlines
	#…</description>
    </item>
    <item rdf:about="https://dsss.be/w/perl:sets?rev=1250182629&amp;do=diff">
        <dc:format>text/html</dc:format>
        <dc:date>2009-08-13T16:57:09+00:00</dc:date>
        <dc:creator>Anonymous (anonymous@undisclosed.example.com)</dc:creator>
        <title>sets</title>
        <link>https://dsss.be/w/perl:sets?rev=1250182629&amp;do=diff</link>
        <description>Consider finding the union and intersection of two unique arrays @a and @b:


foreach $e (@a) { $union{$e} = 1 }
foreach $e (@b) {
	if ( $union{$e} ) { $isect{$e} = 1 }
	$union{$e} = 1;
}
@union = keys %union;
@isect = keys %isect;


This would be more idiomatically written as:</description>
    </item>
    <item rdf:about="https://dsss.be/w/perl:single_document_web_server?rev=1250182596&amp;do=diff">
        <dc:format>text/html</dc:format>
        <dc:date>2009-08-13T16:56:36+00:00</dc:date>
        <dc:creator>Anonymous (anonymous@undisclosed.example.com)</dc:creator>
        <title>single_document_web_server</title>
        <link>https://dsss.be/w/perl:single_document_web_server?rev=1250182596&amp;do=diff</link>
        <description>This little script acts as a web server, but it serves only one document no matter what the requested URL was: the document embedded in the script itself.  This is useful to throw on a machine in a lab environment to identify who owns it and what it&#039;s for.</description>
    </item>
</rdf:RDF>
