Summary

Today's lesson has covered a few topics that might be considered somewhat esoteric, but will become more important as the remainder of this book unfolds. The first half of this lesson discussed aspects of variables and scope, including what a global variable means when there are packages to restrict it from being truly global, and making sure your global variables are local to a script by defining them with my.

Then, we turned to local variables, and you learned more about the use of my variables inside blocks and subroutines, as well as defining local variables with local.

Given all these choices for declaring and using different variables, it's confusing to know what's right. Among Perl programmers, there are general rules and practices that are commonly used for dealing with variables and scope:

  • Don't declare raw global variables. Declare all global variables using my, and use use strict to make sure you're doing it right.

  • Use my variables over local unless you have specific reasons for doing so. Local local variables suffer from many of the same reusability and debugging problems that global variables do, and confuse what's an otherwise clean distinction between the concepts of local and global.

  • Try to avoid using local variables that have the same name as globals unless you have a good reason for doing so

In the second half of the lesson, we looked at the use function, and how to use it to turn on pragmas—hints to Perl for how to compile and run your scripts—and to import and use code contained in modules either in the standard library or from CPAN. The module part of this lesson was potentially the most important thing you'll learn today; we'll be using modules throughout the rest of this book.

The functions and other commands we explored today include:

  • package, to switch between different packages

  • my, to define a local variable or a non-package version of a global variable

  • use strict, to make sure you're not using any stray global variables

  • local, another way of defining local variables (use my instead)

  • use, in general, to import a pragma or module

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.201.209