O'Reilly logo

A Practical Guide to Linux® Commands, Editors, and Shell Programming, Third Edition by Mark G. Sobell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Subroutines

All variables are package variables (page 521) unless you use the my function to define them to be lexical variables (page 522). Lexical variables defined in a subroutine are local to that subroutine.

The following program includes a main part and a subroutine named add(). This program uses the variables named $one, $two, and $ans, all of which are package variables: They are available to both the main program and the subroutine. The call to the subroutine does not pass values to the subroutine and the subroutine returns no values. This setup is not typical: It demonstrates that all variables are package variables unless you use my to declare them to be lexical variables.

The subroutine1.pl program assigns values to two variables ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required