Item 1: Know your namespaces.


There are seven separate kinds of variables or variable-like things in Perl: scalar variables, array variables, hash variables , subroutine names, format names , filehandles, and directory handles.

Each of these different kinds of variables has its own namespace . Changing the value of one kind of variable does not in any way affect the value of another kind of variable with the same name . For example, the scalar variable $a is independent of the array variable @a :

 $a = 42; 

Set scalar $a = 42 .

 @a = (1, 2, 3); 

@a = (1,2,3) , but $a is still 42 .

Also, each package (see Item 42) in a Perl program defines its own set of namespaces. For example, $a in package main is independent of $a in package foo :

 $a = 1; 

Assuming we start in package main , set scalar $main::a = 1 .

 package foo; 

Default package is now foo .

 $a = 3.1416; 

$foo::a is 3.1416 ; $main::a is still 1 .

You have to look to the right as well as the left of an identifier, as Perl does, to determine what kind of variable the identifier refers to. For example, the syntax for accessing elements of arrays and hashes begins with $ , not @ or % . The $ means that the result is a scalar value, not that you are referring to a scalar variable:

 $a = 1;  @a = (1, 2, 3);  %a = ('a' => 97, 'b' => 98); 

Set scalar $a = 1 .

Set array @a = (1,2,3) .

Set hash %a .

 $a[3] = 4;  $a{'c'} = 99; 

$a is still 1; @a is (1,2,3,4) now.

$a, @a still the same; %a has three key-value pairs now.

Not all variable-like things in Perl are prefixed with punctuation characters . Subroutine names can be prefixed with an ampersand, but the ampersand is generally optional. In some cases, parentheses around the subroutine arguments can also be omitted:

 sub hi {    $name = shift; "hi, $name\n"  } 

A subroutine named hi .

 print &hi("Fred"); 

The "old-style" syntax.

 print hi("Fred"); 

Parens following hi cause it to be recognized as a sub name.

 print hi "Fred"; 

Parens can also be omitted if hi is defined in the source code before it is used (see Item 10 ).

Filehandles, format names, and directory handles are not prefixed with punctuation characters, but are recognized in context:

The filehandle, format name, and dirhandle below are independent of one another, even though they are all named TEST.

 open TEST, ">$$.test";  print TEST "test data\n"; 

Open filehandle named TEST .

Print to filehandle TEST .

 format TEST =  @<<<<<<<<<<<<< @<<<< @<<<<  $name, $lo, $hi  . 

Format named TEST .

 opendir TEST, "."; 

Directory handle named TEST .

It's not necessarily bad programming style to take advantage of Perl's independent namespaces by giving two different kinds of variables the same name. Sometimes it even seems like the sensible thing to do:

 @who =    grep { /\bjoebloe\b/ } `who`; 

@who contains output from who command, one line per element.

 foreach $who (@who) {    # ... do something with $who  } 

Iterate over each line of output using variable $who .



Effective Perl Programming. Writing Better Programs with Perl
Effective Perl Programming: Writing Better Programs with Perl
ISBN: 0201419750
EAN: 2147483647
Year: 1996
Pages: 116

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net