Command Line Arguments - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Command Line Arguments

Description:

ARGV contains list of arguments $0 contains program name. Examples: myscript.pl 15 4 hello ... terminates. At this point, _at_ARGV () any future reads to will ... – PowerPoint PPT presentation

Number of Views:95
Avg rating:3.0/5.0
Slides: 7
Provided by: PaulL155
Category:
Tags: arguments | argv | command | line

less

Transcript and Presenter's Notes

Title: Command Line Arguments


1
Command Line Arguments
2
Command Line Arguments
  • Arguments provided to your program via the
    command line.
  • Not read from user input
  • in C/C
  • argv contains program name and arguments
  • argc contains number of arguments plus one
  • in Perl
  • _at_ARGV contains list of arguments
  • 0 contains program name

3
Examples
  • myscript.pl 15 4 hello
  • _at_ARGV ? (15, 4, 'hello')
  • 0 ? 'myscript.pl'
  • scalar(_at_ARGV) ? 3
  • perl_hw2.pl
  • _at_ARGV ? ( )
  • 0 ? 'perl_hw2.pl'
  • scalar(_at_ARGV) ? 0

4
Notes
  • These are true globals no matter what package
    you're in, 0 always means main0, _at_ARGV always
    means _at_mainARGV
  • unless you've declared a lexical _at_ARGV
  • don't do that.
  • Depending on your system, 0 may contain just the
    script name, a relative path to the script, or an
    absolute path.

5
Some Magic
  • The standard ltgt operator has some magic built
    into it
  • When "empty", ltgt will open the first file
    specified on the command line, and begin reading
    it.
  • Once it's exhausted, will open the next file,
    etc.
  • If another call to ltgt occurs after all arguments,
    ltgt begins reading from STDIN
  • While ltgt is processing command line arguments,
    ARGV contains the name of the current argument.
  • If any file can't be opened, a warning is issued,
    and processing continues with the next one.

6
Magic Example
  • myscript.pl file1.txt sample
  • while (ltgt) chomp print "ARGV, line .
    _\n"
  • open file1.txt, print out all lines in that file
  • open sample, print out all lines in that file
  • loop terminates.
  • At this point, _at_ARGV ? ()
  • any future reads to ltgt will read from STDIN
Write a Comment
User Comments (0)
About PowerShow.com