新灵空间

I will go wherever god leads me to!
Some Perl modules

Intermediate Perl notes

shpeacelover posted @ 2014年3月31日 21:21 in Perl , 358 阅读

1.  The use  statement executes at compile time, so it looks at the module search path, @INC , at compile time. That can break our program in hard-to-understand ways unless we take @INC  into consideration. We need to make our @INC  modifications before we try to load modules.

2.  Think of use lib  as not “use this library,” but rather “use this path to find my libraries (and modules).”

3.  The FindBin module finds the full path to the script directory so we can use it to build paths:

use FindBin qw($Bin);

 Now, in $Bin  is the path to the directory that holds our script. If we have our libraries in the same directory, our next line can be:

use lib $Bin;

 If we have the libraries in a directory close to the script directory, we put the right path components together to make it work:

use lib "$Bin/lib"; # in a subdirectory

use lib "$Bin/../lib"; # up one, then down into lib

4.  The use lib  has a big drawback. We have to put the library path in the source. We might have our local modules installed in one place, but our coworkers have them in another. We don’t want to change the source every time we get it from a teammate, and we don’t want to list everyone’s locations in the source.  

5.  A list is an ordered collection of scalars. Lists are the values themselves, and sometimes we store lists in arrays, the container that holds an ordered list.  

6.  The grep  operator takes a “testing expression” and a list of values. It takes each item from the list in turn and places it into the $_  variable. It then evaluates the testing expression in scalar context. If the expression evaluates to a true value, grep  passes $_  on to the output list.

7.  In list context, the grep  operator returns a list of all such selected items. In scalar context, grep  returns the number of selected items.

8.  While the grep  is running, it shadows any existing value in $_ , which is to say that grep  borrows the use of this variable, but restores the original value when it’s done. The variable $_  isn’t a mere copy of the data item, though; it is an alias for the actual data element, similar to the control variable in a foreach  loop.

8.  The map  operator transforms one list into another. It has a syntax identical to the grep  operator’s and shares a lot of the same operational steps. For example, it temporarily places items from a list into $_  one at a time, and the syntax allows both the expression block forms.

9.  Sometimes we don’t care about the value because we want to use the hash as an easier way to check that an element is in a list. In that case, we can give the keys any value just to have the key in the hash. Using is a good value to use:

my %hash = map { $_, 1 } @castaways;
my $person = 'Gilligan';
if( $hash{$person} ) {
   print "$person is a castaway.\n";
}

10.  The do  block is one powerful but overlooked feature of Perl. It provides a way to group statements as a single expression that we can use in another expression. It’s almost like an inline subroutine. As with subroutines, the result of do  is the last evaluated expression.

11.  A Perl scalar variable holds a single value. An array holds an ordered list of scalars. A hash holds an unordered collection of scalars as values, keyed by strings.

12. Here below is the code to check if a particular item exists in a list:

my @required = qw(preserver sunscreen water_bottle jacket);
my %skipper = map { $_, 1 } qw(blue_shirt hat jacket preserver sunscreen);

foreach my $item (@required) {
  unless ( $skipper{$item} ) { # not found in list?
      print "Skipper is missing $item.\n";
  }
}

13.  A reference to the array is like a pointer: it points at the array, but is not the array itself.

14.  Both of these lines refer to the entire array:

 

@ skipper
@{ $items }

 whereas both of these refer to the second item of the array:

$ skipper [1]
${ $items }[1]

 

 

blog comments powered by Disqus