So, I decided to learn some Perl earlier today. I had a small script that downloaded and converted a hosts file for use with unbound:

#!/bin/sh curl https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts +|grep '^0\.0\.0\.0' | awk '{print "local-zone: \""$2"\" redirect\nloc +al-data: \""$2" A 0.0.0.0\""}' > ads.conf

Which discards all lines except lines like this:

0.0.0.0 apps.id.net

And changes it into two lines like this:

local-zone: "apps.id.net" redirect local-data: "apps.id.net A 0.0.0.0"

I poked around this site and read some docs and came up with this:

#!/usr/bin/perl use strict; use warnings; use LWP::Simple; my $url = "https://raw.githubusercontent.com/StevenBlack/hosts/master/ +hosts"; open(my $file, '>', 'ads.conf') or die "wtfile?"; for (split /^/, get ($url)) { if ($_ =~ /0\.0\.0\.0/ and $_ !~ /#/){ $_ =~ s/0\.0\.0\.0 //; $_ =~ s/\n//; print $file "local-zone: \"" . $_ . "\" redirect\nloca +l-data: \"" . $_ . " A 0.0.0.0\"\n"; } } close $file;

Turns out the Perl script takes 5 times as long as the shell script, and uses nearly twice the cpu. What mistakes did I make, and is there a way to do this in Perl faster then the shell script can?

Updated script based on comments:

#!/usr/bin/perl use strict; use warnings; use LWP::Simple; my $url = "https://raw.githubusercontent.com/StevenBlack/hosts/master/ +hosts"; open(my $file, '>', 'ads.conf') or die "wtfile?"; for (split /^/, get ($url)) { if ($_ =~ /^\Q0.0.0.0\E (.*)$/){ print $file "local-zone: \"" . $1 . "\" redirect\nloca +l-data: \"" . $1 . " A 0.0.0.0\"\n"; }

Update 2, thank you marioroy (link) and rizzo (link), combining your two suggestions gets me the fastest script so far:

#!/usr/bin/perl use strict; use warnings; my $_url = "https://raw.githubusercontent.com/StevenBlack/hosts/master +/hosts"; open(my $file, '>', 'ads.conf') or die "wtfile?"; my $var = `curl $_url` or die "wturl?"; while($var =~ /(\n0\.0\.0\.0)(\s)([\w\.]+?)\s+/g){ print $file "local-zone: \"" . $3 . "\" redirect\nlocal-data: +\"" . $3 . " A 0.0.0.0\"\n"; } close $file;

Interestingly that script, seems to be on par with the shell script time wise, but does it using 1/6th the cpu.


In reply to Making this script process 56,000 lines 5 times faster by kris004

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.