X hits on this document

PDF document

Running and Debugging Perl - page 20 / 30

83 views

0 shares

0 downloads

0 comments

20 / 30

Chapter 9

For this reason, it's recommended that you use the standard modules Getopt::Std or Getopt::Long instead. Appendix D gives a brief rundown of all perl's standard modules or for more detailed information, refer to the perlmod manpage.

  • -

    I and @INC

Perl knows where to look for modules and Perl files included via do or require by looking for them in the locations given by a special variable, the array @INC. You can add directories to this search path on the command line, by using the -I option:

perl –I/private/perl program

will cause Perl to look in the directory /private/perl for any files it needs to find besides those in @INC. For more details on working with @INC up close, just have a look in the next chapter.

  • -

    a and -F

One of perl's ancestors is the UNIX utility awk. The great thing about awk was that when reading data in a tabular format, it could automatically split each column into a separate variable for you. The perl equivalent would use an array and would look something like this:

while (<>) { my @array = split; ...

}

The -a switch, used with -n and -p, does this kind of split for you. It splits to the array @F, so

>perl -an '...'

is equivalent to:

LINE: while (defined($_

= <ARGV>))

{

@F = split; 'Your code here';

}

So, to get the first word of every line in a file, you could say this:

>perl -ane 'print $F[0],"\n"' chapter9.txt Running

By

You

Everyone >

298

Document info
Document views83
Page views83
Page last viewedThu Dec 08 08:54:11 UTC 2016
Pages30
Paragraphs723
Words10014

Comments