All Snippets
Top 100 Snippets

By Language

GBIC >> Source Code >> Top 100 Snippets >> Perl
Top 100 Perl Snippets
These snippet titles reflect my cut at the most common functions a programmer needs to create Perl applications - for use on both Unix and Windows. Since Perl scripts tend to be small, you might find 20-30 of these in any one Perl program, but you're very unlikely to find them all in any one program.

Most snippets consists of relatively few lines of code - a tribute to the power and flexibilty of Perl.

Creating a Top 100 Perl list is a bit challenging in that Perl's primary use is on servers where a graphical interface isn't required (remote users can't see server monitors). So if you notice the lack of graphical/interface items on this list (whereas my other Top 100 lists do have graphical code), you'll know why.

I find that my opinion continues to shift on about 10%-20% of the list, so don't be surprised to find the list changing - or growing as I consider the list in more detail.

You can view the snippets online at this site or download my freeware gbCodeLib code librarian, which has the source code for all of these. It contains snippets and mini-tutorials for several languages such as VB, VB.net, VBA, JavaScript, Perl, Java, and HTML.

1. Start a new application and wait for it to finish
2. Maintain MRU file (most recent updated files)
3. Read/write INI files

4. Cycle througn an array
5. Randomly shuffle an array
6. Remove an element of an array (by content or by position)
7. Search an array

8. Copy or retrieve text and images from the clipboard

9. Capture any key (Enter, TAB, arrow keys, home/end, F-keys, ...)
10. Forces lower/upper case on text input

12. Compute elapsed time between 2 dates
13. Compute elapsed time between 2 events (ms accuracy)
14. Display time in multiple formats

15. Create valid full pathname from folder and file names
16. Extract file name from a full path
17. Determine if a file exists
18. Get file properties (size/date/attributes)
19. Delete a file to the recycle bin
20. Backup/restore a file

File - Text
21. Read file one line at a time or all at once
22. View content

File - Binary
23. Read/write data at a specific location
24. Read entire file at once
25. View contents in hex or binary-stripped format

26. Determine if a file exists
27. Delete a folder and its subfolders
28. Get list of dirve letters
29. Get type of drive
30. Create a fll path of folders


31. Convert between Long, RGB, VB, and web colors
32. Get image size without reading entire file (JPG/GIF/BMP)
33. Get screen resolution
34. Read/write/display/convert images (JPG/GIF/BMP)
35. Resize and image, keeping aspect ratio

36. Check for Internet connection
37. Create web page (HTML) from code
38. Download text/binary file, with progress indicator
39. Upload text/binary file, with progress indicator
40. Get remote file information (size/date0
41. Open a web page in a browser
42. Send email using default email application
43. Update file via FTP

44. Create random integer within specified range
45. Round number to specified number of decimals

46, Play CD
47. Play sound or video file (.wav, .mp3, ..mid, avi, .mpg)
48. Record sound or video

49. List all files and folders beneath a specified full path

50. Print images (positioned, scaled, or tiled)
51. Print preview
52. Color text printing
53. Send page eject to the printer
54. Print justified text (left/right)


55. Calculate checksum
56. Encrypt/decrypt a file/string (with or without a password)

57. Bubble sort
58. QuickSort

59. Convert strings to upper/lower case
60. Find/replace a string within a string
61. Split string into an array based on delimeters
62. Find text between two strings

63. Get computer name
64. Get user name
65. Get name of special folders (Windows, System, SendTo, Startup,
66. Get Windows version
67. Reboot / restart / logoff system