Determine Disk Usage
By · CommentsThis command will show the number of KBs used for all non-hidden files and folders in the current directory:
du -sk * | sort -rn
Find By Contents
By · CommentsThis will recursively search all the files, with a certain filename pattern (in this case *.h) in a certain directory, that contains a certain text pattern in its contents (in this case ‘DeviceDriver’).
find . -name '*.h' -print0 \ | xargs -0 grep -n -e DeviceDriver
CGI To Display Module Documentation
By · CommentsHere’s a CGI to display a module’s POD. The module has to be in the $INC. This is great for providing documentation to internal modules.
# save this as showdoc.cgi #!/usr/local/bin/perl use strict; use Pod::Html; use CGI; use CGI::Carp qw(fatalsToBrowser); # Send out the header print "Content-type: text/html", "\n\n"; my $q = new CGI; my $module = $q->param('module'); require $module; $| = 1; chdir ("/tmp"); my $fullpath = $INC{$module} or die "$module not found"; pod2html("--infile=$fullpath", "--flush"); # Clean up the junk left by pod2html END { unlink("pod2html-dircache"); unlink("pod2html-itemcache"); }
To use this, say you want to view POD for Data::Dumper:
http://localhost/cgi-bin/showdoc.cgi?module=Data/Dumper.pm
crontab Format
By · Comments[I could never remember this so I'm putting it here.]
These are the fields of a crontab file:
a b c d e /full/path/to/script where a = minute (0-59), b = hour (0-23), c = day of the month (1-31), d = month of the year (1-12), e = day of the week (0-6 with 0=Sunday). * = every min, hour, day, etc. */10 = every 10 min, hour, day, etc.
Read A Text File
By · CommentsThis will open a text file and read it line by line. As it is, it will just print each line to the screen, so very much like the unix ‘cat’ command, but could very well do anything on the line just read by replacing the cout statement.
#include <fstream> #include <iostream> #include <string> void main() { string s; ifstream infile; infile.open("aaa.txt"); while(infile >> s) { cout << s << endl; } }
Create Skeleton Module
By · CommentsUse this to create a skeleton module:
h2xs -AXn Your::Module
stty Settings
By · CommentsThis is my stty setting (all in one line):
stty intr '^c' erase '^?' kill '^u' echoe \ echoctl echoke -ixany
Detect CPU Endian-ness
By · CommentsTo detect a CPU’s endian architecture, use either one of the variables set like so:
$is_big_endian = unpack("h*", pack("s", 1)) =~ /01/; $is_little_endian = unpack("h*", pack("s", 1)) =~ /^1/;
Found in Perlmonks
Sorting Section Numbers
By · CommentsHere’s a code to sort section numbers in ascending order:
sub sort_sections { my ($data) = @_; my $sorted = (); @$sorted = map { $_->[0] } sort { my $x=1; while (defined $b->[1][$x]) { defined $a->[1][$x] or return -1; if ($x%2) { ## Strict numeric comparison return 1 if $a->[1][$x] > $b->[1][$x]; return -1 if $a->[1][$x] < $b->[1][$x]; } else { ## Non-numeric comparison return 1 if $a->[1][$x] gt $b->[1][$x]; return -1 if $a->[1][$x] lt $b->[1][$x]; } $x++; } return defined $a->[1][$x] ? 1 : 0; } map { [$_, [split(/(\d+)/, $_)]] } @$data; return $sorted; }
Here’s a test for it:
$sects = ['1.1', '1.2.2', '1.3', '1.2', '1.3.1']; print Dumper($sects); $sorted_sects = sort_sections($sects); use Data::Dumper; print Dumper($sorted_sects);
And here’s the output:
$VAR1 = [ '1.1', '1.2.2', '1.3', '1.2', '1.3.1' ]; $VAR1 = [ '1.1', '1.2', '1.2.2', '1.3', '1.3.1' ];
Found in Perlmonks.
Base Conversion
By · CommentsThe following routines will convert a number to and from among the different bases: decimal, hexadecimal, and binary.
################################################ # Convert a binary input to hex # Does not return any leading 0s # sub bin2hex { my $inpt = shift; my $hex; my $bits = length($inpt); $inpt = (32 - $bits) x '0' . $inpt; my $dec = unpack("N", pack("B32", substr("0" x 32 . $inpt, -32))); return(sprintf("%x", $dec)); } ################################################ # Convert a decimal input to binary # Arguments = decimal_number, number_of_bits # sub dec2bin { my $dec = int(shift); my $bits = shift; my $bin = unpack("B32", pack("N", $dec)); substr($bin, 0, (32 - $bits)) = ''; return($bin); } ################################################ # Convert a binary input to decimal # sub bin2dec { my $bin = shift; my $bits = length($bin); $bin = (32 - $bits) x '0' . $bin; my $dec = unpack("N", pack("B32", substr("0" x 32 . $bin, -32))); return($dec); } ############################################### # Convert a hex input to decimal # sub hex2dec { my $h = shift; $h =~ s/^0x//g; return( hex($h)); }