Home‎ > ‎scripting‎ > ‎

Perl Cookbook

Venerable Variables:

  1. passVariables: take an variable, and put it's reference into another variable, and then print out the value.

Messing with Arrays:

  1. passArray: Make an array in one subroutine, and pass it to another subroutine. Then separate out the variables in the array with commas and print.
  2. passArrayOfArray: make a 2d array, pass it to another subroutine, and separate the variables of each array with colons and print
  3. copyFileToAofA: enter in a file that is tab deliminated, this script will break apart the tab deliminated cells, and convert each cell to variables. From all that it will create an array of arrays (2D array) and pass it to another subroutine that will print out all the variables with colon's in between.
  4. RefOfArrays: ways you can use references of arrays.
  5. Arrays to Lists: what happens when you try and convert an array to a list.
  6. ref/unref-ing Arrays: how to reference arrays and get them back
  7. transposeArray: How to rotate a matrix
  8. transposeArray2: Use the transpose method to convert columns in a spreadsheet into separate sheets
  9. grabnPrint: In one sub open a file and put into an array, pass the reference of the array to another sub and do stuff

Flailing around with Hashes:

  1. matchHashes-a+b: take two hash tables; a and b. make a new hash table that is the combined value of the two. If a and b have the same variables, use the value of the variable in the b hash as the value for the new hash table.
  2. matchHashes-a_with_b_values: here we take the two hashes a and b, and add values from b to a IF the variable exists in a. Thus the output has only the items from a, but if b had the same item, then we get b's value for that idem overwritten over a.
  3. StringHashes: with two hashs, one is the key for the other, and string them together
  4. passHash: make two different hashes, in one make a normal hash and pass it out of a sub, in the other make an anonymous hash and pass it out of the subroutine. (they both end up being anonymous after they are passed out of the sub. Then accept it into another sub and print two ways; one in an OO way, and the other has a more normal looking code way of doing it.
  5. passHash2: Create a hash, and run two different subroutines that add to the hash. Do this by passing references to a hash around.
  6. copyFileToHash: Take a tab deliminated text file, and convert it into a hash table. Then pass the reference for that hash table to another subroutine and print out the hash table.
  7. passArraytoHash: take an array, stick it into a hash, then erase the contents of the array and refill it up with something different, and then stick the new value into the array. (thus making a long list of arrays). Then print everything out.

Ogling over OO:

  1. Making and Referencing Modules: Discusses how to create a module and how to link to it.  (less of a code example, and more of a how-to)
  2. Extending the library path: Perl Modules normally have to go in very specific places.  This tells you want to do if you want to store them someplace else. 

Opening/Printing Stuff

  1. doOrDie: Open a file and properly exit if the file is not there.
  2. listDir: Look at a directory via perl, and print it out.
  3. listDirViaOpen: Look at a directory by running a shell command within perl, and print results.
  4. readInFile: Open a file in one subroutine, then print it out within another.
  5. usage: All scripts should have usage statements, here's an example for how to do this.


  1. readGET: grab a variable in the uri, and present it on the page
  2. uriVars: how to feed in multiple variables in a uri
  3. listDir2: Look at a directory and print it out. 
  4. AutoHTML: using CGI:: to automatically enter in your html headers
  5. FormSubmit: provide a form, submit it and do action against it. 

Random tools and functions

  • Time: uses the localtime function to come up with date, time, second information.
  • Code2HTML: tool that converts perl script to color coded html.  Used in these pages to document code.  There is also tohtml

Other Code Examples

Other stuff that is not worth using as examples, but needs a place to be kept.

Subpages (31): View All