About the Perl Chr Function

About the Perl Chr Function thumbnail
Many Web scripts are written in Perl.

The programming language Perl is particularly suited to the creation of Web scripts. Perl helps developers build Web pages that respond to user input. Perl's text handling abilities make it suitable to the task of generating HTML and the "chr" function is one of several tools that provides Perl's text manipulation capability.

  1. Functions

    • A function is a block of code that can be part of one program, or can be held in a library and be accessed by several different programs. Chr is one of Perl's standard functions and so is available as a tool to any Perl programmer.

      A function declaration follows the function name with a list of arguments in brackets. The arguments are holders for values that are passed to the function when it is called. A function can have one, many or no arguments and it can return a value when it finishes processing. Functions that return values need to be called as part of an equation with a variable ready to accept the returned value, like “$a_var = a_function($b_var).”

    Purpose

    • “Chr” is an abbreviation for “character.” It is common practice in computing to represent characters as numbers. The earliest table of character numbers was called ASCII, which stands for the American Standard Code for Information Interchange. ASCII assigns a code to each character and the chr function works on this principle. Given a number, chr returns the corresponding character, so chr(65) returns the character “A”.

    Unicode

    • Perl uses Unicode numbers instead of the ASCII character set. Unicode is an expansion of ASCII and any number used in ASCII maps to the same character in Unicode. The version of Unicode that Perl uses is Unicode 8. The International Standards Organization published an encoding standard called the Universal Character Set, or UCS. ISO gave Unicode 8 the name UTF-8 and it stands for UCS Transformation Format.

    Inverse

    • Perl includes a function that performs the reverse of chr, called ord. Ord takes a character and returns the equivalent Unicode number. So ord(“A”) returns 65. Chr and ord are important elements in text handling. For example, it is easier to compare two words by examining the numerical values of the characters they contain, or upper case can be converted to lower case characters by performing mathematical operations on the Unicode number.

Related Searches:

References

  • Photo Credit Hemera Technologies/AbleStock.com/Getty Images

Comments

Related Ads

Featured