Map Hex Array to Ascii Where @values are an array of hex values my @ascii_vals = map (chr, map(hex,@values));
Convert IP to HEX in Perl – one liner Strip out the IP.. $IP=”192168016012″; $IP = sprintf “%*vX”, “:”, $IP; Vector hex conversion of IP
Perl – Fork and Kill a System Process on Timeout [perl] $pid = open(PIPE, “$command |”) or die $!; eval { local $SIG{ALRM} = sub { die “TIMEOUT” }; alarm($timeout); while (<PIPE>) { $data = $_; }; close(PIPE); alarm(0); }; if ($@) { die $@ unless $@ =~ /TIMEOUT/; kill 15, $pid; close(PIPE); $? ||= […]
Unix find and replace text within all files within a directory Trying to do a search on an entire directory, finding all the instances of the text “applicationX” and replacing all instances with the text “applicationY”. find /path/to/start/from/ -type f | xargs perl -pi -e ‘s/applicationX/applicationY/g’
Get Parent PID [perl] my $parent_id = getppid(); print “$parent_id\n”; [/perl] NAME getppid – get parent process ID SYNOPSIS getppid DESCRIPTION Returns the process id of the parent process. DISCLAIMER We are painfully aware that these documents may contain incorrect links and misformatted HTML. Such bugs lie in the automatic translation process that automatically created […]
Get PID (process id) from Perl Script $$ or $PID
Perl – Lock Files (flock) The following script is an example of using lock files in Perl. This prevents more than one user or process accessing a file at a time. [perl] use Fcntl qw(:DEFAULT :flock); use Fcntl ‘:flock’;¬†¬†¬†¬† #LOCK_* constants my($lockneedsremoving)=0; my($lockfilename)=”/var/.pwd.lock”; unless (createlock()) { print “Lock has been created”; print “This is only […]
Perl Substr substr This function supports three sets of passed values as follows: [perl] substr (STRING,OFFSET,LEN,REPLACEMENT) substr (STRING,OFFSET,LEN) substr (STRING,OFFSET) [/perl] The function: substr (STRING,OFFSET) returns all characters in the string after the designated offset from the start of the passed string. The function: substr (STRING,OFFSET,LEN) returns all characters in the string after the designated […]
Perl Lower/Upper Case First Character ucfirst Takes a string and retruns it with the first character in upper case. [perl] ucfirst Str1 [/perl] lcfirst Takes a string and retruns it with the first character in lower case. [perl] lcfirst Str1 [/perl]
Perl Upper Case uc Converts all characters in the string to upper case. [perl] uc Str [/perl]