Perl Coding Hints

[ Much of this information came from perldoc.com.]

Case Conversion

Use the uc and lc functions to convert text to uppercase or lowercase. For example, $tmp = uc($tmp); will make the text in $tmp all uppercase.

Writing Files

It's IMPOSSIBLE to open a file and insert text at the beginning or middle of the file, without overwriting text that's already in the file. This is a limitation of computer file systems, not Perl -- no language can do this. You need to use some kind of technique like writing your output to a temp file, then overwriting the original file with the temp file when you're done. The PerlFAQ 5 has this covered, in a rather assuming-you-already-know-the-answer way.

Random How-Tos

To round a number to 3 decimal places:

use POSIX;
$score = sprintf "%.3f", ($hits/$total)*100;


Hash sorting: you can't actually sort a hash itself, but you can sort one INTO an array: # Hash sorted by keys: @keys = sort keys %SomeHash; # Hash sorted in reverse by keys: @keys = sort { $b cmp $a } keys %SomeHash; # Hash sorted by values: @values = sort values %SomeHash; # Hash sorted in reverse by values: @values = sort { $b cmp $a } values %SomeHash;
To sort a hash in ascending order by its values and print the associated keys in the same sorted order with the values: ( from http://www.devdaily.com/perl/edu/qanda/plqa00016/ ) foreach $key (sort { $myhash{$a} <=> $myhash{$b} } (keys(%myhash))) { print "$myhash{$key}, $key"; } # Note that $a and $b are global variables, you don't need to set them. # And, just switch the $a and $b to sort in descending order. This will sort by keys, and populate an array instead of printing: foreach $key (sort (keys(%myhash))) { push @sortedHash, "$myhash{$key}, $key"; }
To get the IP address when you know the hostname: use Socket; my $ipaddress = inet_ntoa(scalar gethostbyname("www.google.com")); To get the hostname when you know the IP address: use Socket; my $hostname = gethostbyaddr(inet_aton("216.239.33.101"), AF_INET); Or use one of these scripts, which are longer but don't require any external modules like Socket: iphostqueries (command prompt version) iphostqueries (web-based version)
To cause your fatal error messages (die) go to the browser, in addition to the logfile: use CGI::Carp qw(fatalsToBrowser);
Strip leading spaces from a string: $string =~ s/^\s+//; Strip trailing spaces from a string: $string =~ s/\s+$//;
To find the current working directory: On Linux: my $wd = `cwd`; On Windows: use Cwd; my $wd = cwd;
Chop off the first 4 characters from the string in $a: $a = substr($a, 4); Save only the first 4 characters from the string in $a: $a = substr($a, 0, 4); Chop off the last 4 characters from the string in $a: $a = substr($a, 0, -4); Save only the last 4 characters from the string in $a: $a = substr($a, -4);
To expand the variables within another variable: $a =~ s/(\$\w+)/$1/eeg; die if $@; This is useful when you use this format for defining a variable: $a = <<'STOPHERE'; this stuff will be inside my variable. $me will be too! "You can use quotes without escaping them when you declare a variable this way." STOPHERE
To detect resolution on Windows: use Win32::GUI; $Main = Win32::GUI::Window->new(-name => 'Main',-text => 'Perl',); $DesktopWin = $Main->GetDesktopWindow(); (undef,undef,$DesktopWidth,$DesktopHeight) = Win32::GUI::GetWindowRect+($DesktopWin); print "Screen Resolution is $DesktopWidth x $DesktopHeight";
To create a new process / "fork" a new process on Windows, and then return control to your script without waiting for the new process to complete, use the Windows start command along with the Perl system() function: system("start /B someprogram.exe") and die "$0: $!\n"; # do other stuff here someprogram.exe will run, and your script will immediately pick up doing other stuff, without having to wait for someprogram.exe to finish whatever it's doing. Note that the /B switch to the start command causes someprogram.exe to run without opening a new command-prompt window.

To install perl modules, read this.