mozerd has asked for the wisdom of the Perl Monks concerning the following question:

Salutations to All

Learning Perl and its a slow slog 4me so far

I have the following script that I use under Linux -- I would like to run the same script using pure Perl under Windows 10 via Strawberry Perl

#!/bin/sh saveTo=/var/www now=$(date); echo "# Generated by Joshaven Potter on $now" > $saveTo/dshield.rsc echo "/ip firewall address-list" >> $saveTo/dshield.rsc wget -q -O - http://feeds.dshield.org/block.txt | awk --posix '/^[0-9] +{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.0\t/ { print "add list=blacklist addre +ss=" $1 "/24 comment=DShield";}' >> $saveTo/dshield.rsc echo "# Generated by Joshaven Potter on $now" > $saveTo/spamhaus.rsc echo "/ip firewall address-list" >> $saveTo/spamhaus.rsc wget -q -O - http://www.spamhaus.org/drop/drop.lasso | awk --posix '/[ +0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\// { print "add list=bl +acklist address=" $1 " comment=SpamHaus";}' >> $saveTo/spamhaus.rsc echo "# Generated by Joshaven Potter on $now" > $saveTo/malc0de.rsc echo "/ip firewall address-list" >> $saveTo/malc0de.rsc wget -q -O - http://malc0de.com/bl/IP_Blacklist.txt | awk --posix '/^[ +0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/ { print "add list=blac +klist address=" $1 " comment=malc0de";}' >> $saveTo/malc0de.rsc

Can a glorious Monk PLEASE convert this script to Perl?

Thank You

Replies are listed 'Best First'.
Re: Shell Script in Perl
by choroba (Cardinal) on Apr 05, 2018 at 14:51 UTC
    Hello mozerd, welcome to the Monastery!

    Please, help us to help you. If you want us to test our solutions, provide sample input data (see SSCCE).

    If you just need general advice, here it is:

    • Use localtime in scalar context to get the current timestamp:
      my $now = localtime;

    • To write to a file, open it via open and use print to populate it.
      open my $dshield, '>', "$save_dir/dshield.rsc" or die $!; print {$dshield} "# Generated by Joshaven Potter on $now\n";
      If you plan to reopen the file for reading later, don't forget to close the filehandle (or just let it go out of scope).

    • Instead of wget, use LWP::Simple or WWW::Mechanize.

    • Instead of awk, you can process the contents returned by the aforementioned user agent line by line, with match and print:
      for my $line (split /\R/, $contents) { print {$fh} "add list=blacklist...\n" if /^[0-9]{1,3}\./; }

    • All three blocks seem very similar. It might be pragmatic to use a subroutine that abstracts all the common logic. (I'd probably create a function in the shell script, too.)

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      choroba -- Thanks for the great tips ... will put it together and see how it compares.

Re: Shell Script in Perl
by marto (Cardinal) on Apr 05, 2018 at 14:46 UTC

    "Can a glorious Monk PLEASE convert this script to Perl?"

    This site focuses on helping people with perl problems or questions they have, rather than fulfilling requests. You're more likely to get help if you show what you tried, how if failed.

Re: Shell Script in Perl
by wjw (Priest) on Apr 05, 2018 at 15:14 UTC

    Might want to check out Guide to posting and/or How do I post a question effectively?

    You need to show some effort. What you want to do is pretty simple and covered in the standard Perl documentation. Use the Super Search here on the site to find examples.... there are some really good ones here. Using search for basic stuff is very simple.

    Mostly, you need to prove that you have tried! This ain't a coding service!

    ...the majority is always wrong, and always the last to know about it...

    A solution is nothing more than a clearly stated problem...