Count Characters in String

Count occurences of the letters p and q in a string $str:

my $pq_count = $str =~ tr/pq//;

Remove Newline From Array

pop @data if $data[-1] eq "\n";

Generate DSA Password

The following is a quick script that generates DSA passwords from an input string. To use, save the script as an executable file, and then issue:

./dsagen.pl password

where password is a password you want to hash.

dsagen.pl
#!/usr/bin/perl -w
###########################################################################
##  Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3      ##
###########################################################################
use strict;
use Crypt::PasswdMD5;
 
if ($#ARGV+1 != 1) {
    print "\nUsage: dsagen.pl <password>\n";
    exit;
}
 
print crypt(
    $ARGV[0], 
    sub {
        my @table = ( '.', '/', 0 .. 9, 'A' .. 'Z', 'a' .. 'z' );
        my $salt;
        for(1..2) {
            $salt .= (@table)[rand @table];
        }
        return $salt;
    }
)."\n";

Calculate IP Address Blocks Given a Range of IP Addresses

As a one-liner:

perl -MNet::IP -e '$ip = new Net::IP ("$ARGV[0] - $ARGV[1]"); print map { "$_\n" } $ip->find_prefixes();' "127.0.0.1" "127.0.0.2"

the script:

  • uses the Net::IP package.
  • takes the range of the first argument (127.0.0.1) and the second argument (127.0.0.2) passed on the command-line.
  • prints the address blocks within that range.

An example usage is the following:

perl -MNet::IP -e '$ip = new Net::IP ("$ARGV[0] - $ARGV[1]"); print map { "$_\n" } $ip->find_prefixes();' "127.0.0.0" "127.0.63.255

which returns:

127.0.0.0/18

Set Scheduling Priority

This can be performed in the following way for the process and the IO priority:

use BSD::Resource qw(PRIO_PROCESS setpriority);
use Linux::IO_Prio qw(IOPRIO_WHO_PROCESS IOPRIO_PRIO_VALUE IOPRIO_CLASS_BE ioprio_set);
BEGIN { 
  require autodie::hints; 
  autodie::hints->set_hints_for(
    \&ioprio_set, 
    { 
      fail => sub { 
        $_[0] == -1 
      } 
    } 
  ) 
};
use autodie qw(:all setpriority ioprio_set);
 
setpriority(
    PRIO_PROCESS,
    $$,
    19 # nice: from -20 most favourable to 19 least favourable 
);
ioprio_set(
    IOPRIO_WHO_PROCESS,
    $$,
    # IOPRIO_CLASS_BE - best effort (0 is most favourable and 7 is least favourable)
    IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 7)
);

Randomise Lines in Text File

The following one-liner:

cat lines.txt | perl -MList::Util -e 'print List::Util::shuffle <>'

will randomly shuffle the lines in lines.txt.


fuss/perl.txt ยท Last modified: 2017/02/22 18:30 (external edit)

Access website using Tor Access website using i2p


For the copyright, license, warranty and privacy terms for the usage of this website please see the license, privacy and plagiarism pages.