Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
 Discussion Forums
 HTML, XML, JavaScript...
 Software Reviews
 Editors,Others...
 Top100
 JavaScript Tutorials, ...
 Tutorials
 ASP, CSS, Databases...
 Discussion List
 FAQ, Roundup, Configure ...
 Authoring
 HTML, JavaScript, CSS...
 Design
 Layout, Navigation,...
 Graphics
 Tools, Colors, Images...
 Software
 Browsers, Editors, XML...
 Internet
 Domains, E-Commerce, ...
 WDVL Resources
  Intermdiate, Tutorials,...
 WDVL
 Discussion Lists, Top 100,...
 Technology Jobs


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Top 10 Articles
  1. Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
  2. JavaScript Tutorial for Programmers
  3. Design
  4. JavaScript Tutorial for Programmers - Objects
  5. JavaScript Tutorial for Programmers - JavaScript Grammar
  6. JavaScript Tutorial for Programmers - Versions of JavaScript
  7. Cascading Style Sheets
  8. JavaScript Tutorial for Programmers - Embedding JavaScript
  9. JavaScript Tutorial for Programmers - Functions
  10. Authoring JavaScript
Domain Name Lookup
Search to find the availability of a domain name. Just enter the complete domain name with extension (.com, .net, .edu)

Perl Functions

April 26, 1999

A function is like sending out for pizza ... you call a third-party (the pizza shop), provide some parameters (your name, your order), and in 30 minutes or less the results appear at the door. Functions, though, work much more quickly and don't need to be tipped.

Perl comes with many, many functions built-in. These functions range widely in use, from manipulating numbers and strings to working with files on disk. Some functions require extra information to tell them how to do their job -- these are called parameters. Some functions are used inside of larger expressions while others can stand alone. Many functions can be any of the above.

Consider the humble lc function. This function accepts one parameter, a string, and returns a copy of the string in all lowercase letters:

$str="HELLO";
$lowerStr=lc($str);

In the above, the variable $lowerStr would contain the value "hello". Notice how the lc function is used within an assignment operator; after all, using lc by itself...

lc($str);

...would not achieve anything since its returned value has nowhere to go and would evaporate. Notice, though, that the lc function does not change the content of $str at all -- it merely returns a modified copy of $str. Some functions act directly on the variable passed to them -- these functions happily stand alone. For example, the chop function hacks off the last character in a string:

$str="one,two,three,";
chop($str);

The chop call actually modifies the value of $str to "one, two,three". Although chop can be used standalone, that doesn't mean it doesn't return any information. In fact, if used in an expression, chop returns the character that it severed off the original string. Thus:

$str="one,two,three,";
$chopped=chop($str);

The above would both chop the final comma from $str and assign that comma to $chopped. Typically there is no need to save the hacked off part of the string, which is why chop is commonly used without being part of a larger expression.

Functions don't necessarily have to accept their parameters inside parentheses. You may well see examples of functions which are called without any parentheses, such as "chop $str". In fact, you've already seen such an example in this article -- the print function. As a rule of thumb, though, it is best to keep using the parentheses to enclose parameters being passed to the function -- print is an exception in that it is widely accepted without parentheses. Perl lets you get away with much syntactical sloppiness, but that doesn't mean it is a good idea.

Using functions is straightforward, and an essential part of Perl. The good news is that Perl includes a huge number of built-in functions ready and waiting to make programming easier, from handling mathematic calculations to hacking up strings into various bits and pieces to managing times and dates to writing to and reading from data files. The bad news is that there are simply too many functions to summarize in this article. The extra good news is that these functions have already been summarized and categorized, and you can find them in the perlfunc documentation at CPAN, the Comprehensive Perl Archive Network.

Perl Variables: Hashes
The Perl You Need to Know
Perl Program Flow: Conditionals and Loops


Up to => Home / Authoring / Languages / Perl / PerlfortheWeb




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers