Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Contact Us
Online Help
Domain Status
Man Pages

Virtual Servers

Topology Map

Server Agreement
Year 2038

USA Flag



Man Pages

Manual Reference Pages  -  PARANOID::INPUT (3)

.ds Aq ’


Paranoid::Input - Paranoid input functions



$Id:,v 0.20 2011/04/13 22:01:43 acorliss Exp $


  use Paranoid::Input;

  FSZLIMIT  = 64 * 1024;
  LNSZLIMIT = 2 * 1024;

  $rv = slurp($filename, \@lines);

  $rv = sip($filename, \@lines);
  $rv = sip($filename, \@lines, 1);
  $rv = tail($filename, \@lines);
  $rv = tail($filename, \@lines, -100);
  $rv = tail($filename, \@lines, -100, 1);
  $rv = closeFile($filename);

  addTaintRegex("telephone", qr/\(\d{3}\)\s+\d{3}-\d{4}/);
  $rv = detaint($userInput, "login", \$val);

  $rv = stringMatch($input, @strings);


The modules provide safer routines to use for input activities such as reading files and detainting user input.

The <B>sipB> and <B>tailB> functions keep open file handles. Even so, it’s specifically built to be safe for use in <B>forkB> scenarios. You can being a tail or sip in a parent, fork children, and all process can independently continue sipping with no confusion between processes. This is possible because we check to see if the PID matches the PID in effect with the file was opened. If not, we reopen the file and seek to the same position so we can pick up where we left off.

The <B>slurpB> function isn’t affected by this since it reads entire files in a single call, no filehandles are kept open between calls.

All file-reading functions use and obey <B>flockB>.

<B>addTaintRegexB> is only exported if this module is used with the <B>:allB> target.



The value returned/set by this lvalue function is the maximum file size that will be read into memory. This affects functions like <B>slurpB> (documented below). Unless explicitly set this defaults to 16KB.


The valute returned/set by this lvalue function is the maximum line length supported by functions like <B>sipB> (documented below). Unless explicitly set this defaults to 2KB.


  $rv = slurp($filename, \@lines);

This function allows you to read a text file in its entirety into memory, the lines of which are placed into the passed array reference. This function will only read files up to <B>FSZLIMITB> in size. Flocking is used (with <B>LOCK_SHB>) and the read is a blocking read.

An optional third argument sets a boolean flag which, if true, determines if all lines are automatically chomped. If chomping is enabled this will strip both UNIX and DOS line separators.

The return value is false if the read was unsuccessful or the file’s size exceeded <B>FSZLIMITB>. In the latter case the array reference will still be populated with what was read. The reason for the failure can be retrieved <B>from Paranoid::ERRORB>.


    $rv = sip($filename, \@lines);
    $rv = sip($filename, \@lines, 1);

This function allows you to read a text file into memory in chunks, the lines of which are placed into the passed array reference. The chunks are read in at up to <B>FSZLIMITB> in size at a time. Like <B>slurpB> file locking is used and autochomping is also supported.

This function returns true if there was input read, but if any or all of the input splits into lines greater than <B>LNSZLIMITB> it will discard that input and return -1 (which is still technically boolean true).

The reason why we now care about line lengths is because it’s very likely that line boundaries will not fall neatly along our chunk boundaries, so we need to take trailing portions of unterminated lines and store them to be joined with the remainder from the next sip.

When sip comes up to then end of the file it does not close the file, you’re required to close it explicitly with <B>closeFileB>. This is done intentionally to allow the process to continue to effectively <B>tailB> a growing file. Unlike the <B>tailB> function provided here, though, it does perform any additional checks to see if the file you’re reading was truncated or replaced.

An optional third argument tells sip whether or not to chomp all the read lines before returning.


    $rv = tail($filename, \@lines);
    $rv = tail($filename, \@lines, -100);
    $rv = tail($filename, \@lines, -100, 1);

The only difference between this function and <B>sipB> is that tail opens the file and immediately seeks to the end. If an optional third argument is passed it will seek backwards to extract and return that number of lines (if possible). Depending on the number passed one must be prepared for enough memory to be allocated to store <B>LNSZLIMITB> * that number.

This function returns true if the file is successfully open, regardless of whether any new input was there to be read. It only returns false if there was a problem opening or reading the file.

Tail should be called with the third argument for the first tail of a file. Continuing to use it for subsequent calls will cause the number of lines returned to be truncated to fit within that limit.

Like <B>sipB>, one must explicitly close a file with <B>closeFileB>.


  $rv = closeFile($filename);

This function closes any open file descriptors that may have been opened via <B>sipB> or <B>tailB> for the named file. This returns the value of the <B>closeB> function if the file was open, otherwise it returns true.


  addTaintRegex("telephone", qr/\(\d{3}\)\s+\d{3}-\d{4}/);

This adds a regular expression which can used by name to detaint user input via the <B>detaintB> function. This will allow you to overwrite the internally provided regexes or as well as your own.


  $rv = detaint($userInput, "login", \$val);

This function populates the passed reference with the detainted input from the first argument. The second argument specifies the type of data in the first argument, and is used to validate the input before detainting. The following data types are currently known:

  alphabetic            ^([a-zA-Z]+)$
  alphanumeric          ^([a-zA-Z0-9])$
  email                 ^([a-zA-Z][\w\.\-]*\@
  filename              ^[/ \w\-\.:,@\+]+\[?$
  fileglob              ^[/ \w\-\.:,@\+\*\?\{\}\[\]]+\[?$
  hostname              ^(?:[a-zA-Z0-9][a-zA-Z0-9\-]*\.)*
  ipaddr                ^(?:\d+\.){3}\d+$
  netaddr               ^(?:\d+\.){3}\d+(?:/(?:\d+|
  login                 ^([a-zA-Z][\w\.\-]*)$
  nometa                ^([^\`\$\!\@]+)$
  number                ^([+\-]?[0-9]+(?:\.[0-9]+)?)$

If the first argument fails to match against these regular expressions the function will return 0. If the string passed is either undefined or a zero-length string it will also return 0. And finally, if you attempt to use an unknown (or unregistered) data type it will also return 0, and log an error message in <B>Paranoid::ERRORB>.

<B>NOTEB>: This is a small alteration in previous behavior. In previous versions if an undef or zero-length string was passed, or if the data type was unknown the code would croak. That was, perhaps, a tad overzealous on my part.


  $rv = stringMatch($input, @strings);

This function does a multiline case insensitive regex match against the input for every string passed for matching. This does safe quoted matches (\Q$string\E) for all the strings, unless the string is a perl Regexp (defined with qr//) or begins and ends with /.

<B>NOTEB>: this performs a study in hopes that for a large number of regexes will be performed faster. This may not always be the case.


o Fcntl
o Paranoid
o Paranoid::Debug


If you fork a process that’s already opened a file with <B>sipB> or <B>tailB> a new file descriptor will be opened for the child process. But what may be less obvious is that with a newly opened file descriptor you will be starting back from the beginning (or end, in the case of <B>tailB>) of the file, rather than from where ever you were before the fork.


Arthur Corliss (


This software is licensed under the same terms as Perl, itself. Please see for more information.

(c) 2005, Arthur Corliss (

Search for    or go to Top of page |  Section 3 |  Main Index

perl v5.20.3 PARANOID::INPUT (3) 2011-04-13

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.