Документация
HTML CSS PHP PERL другое

Chapter 4.  Subroutines

 
Previous
Table of Contents
Next

Chapter 4. Subroutines

You've seen and used some of the built-in system functions, such as chomp, reverse, and print. But, as other languages do, Perl has the ability to make subroutines, which are user-defined functions.[*] These let us recycle one chunk of code many times in one program.[Chapter 4.  Subroutines] The name of a subroutine is another Perl identifier (letters, digits, and underscores, but it can't start with a digit) occasionally with an optional ampersand (&) in front. There's a rule about when you can omit the ampersand and when you cannot; you'll see that rule by the end of the chapter. For now, we'll use it every time it's allowed, which is always a safe rule. We'll tell you every place where it's forbidden, of course.

[*] In Perl, we don't generally make the distinction that Pascal programmers are used to, i.e., between functions, which return a value, and procedures, which don't. A subroutine is always user-defined, but a function may or may not be. That is, the word function may be used as a synonym for subroutine, or it may mean one of Perl's built-in functions. That's why this chapter is titled Subroutines: because it's about the ones you can define and not the built-ins. Mostly.

[Chapter 4.  Subroutines] The code examples used in this book are recycled from at least 40% post-consumer programming and are at least 75% recyclable into your programs when properly decomposed.

The subroutine name comes from a separate namespace, so Perl won't be confused if you have a subroutine called &fred and a scalar called $fred in the same program, though there's no reason to do that under normal circumstances.

    Previous
    Table of Contents
    Next
    © 2000- NIV