Scripting%20Languages - PowerPoint PPT Presentation

View by Category
About This Presentation



line input op gives you an undef value when you reach eof handy for ... Tinker with _at_ARGV. Exp: process three specific files regardless of user choice at CL ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 26
Provided by: Curren
Learn more at:


Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Scripting%20Languages

Scripting Languages
  • Chapter 6 I/O Basics

Input from STDIN
  • Weve been doing so with ltSTDINgt
  • line ltSTDINgt
  • chomp(line)
  • Same as
  • chomp(line ltSTDINgt)
  • line input op gives you an undef value when you
    reach eof handy for dropping out of loops
  • while (defined(line ltSTDINgt))
  • print I saw line

  • Were reading the input into a variable
    checking to see if its reached the eof (meaning
    its defined) if its defined we run the while
  • Inside the loop well see each line one after
  • Since this is something that is done quite often
    Perl has a shortcut.
  • while (ltSTDINgt)
  • print I saw _
  • This only works if you put a line-input operator
    in the condition of the while loop.

List context
  • The previous examples were evaluated in scalar
  • Now for list context
  • foreach (ltSTDINgt)
  • print I saw _
  • gives you all the remaining lines as a list
    each element of the list is one line

Whats the difference
  • A while loop in Perl reads a line of input puts
    it into a variable runs the body of the loop.
  • Then it goes back to fine another line of input.
  • A foreach loop --- the line-input operator is
    being used in a list context foreach needs a
    list to iterate through so it has to read all
    the input before the loop starts running.
  • For large files its best to use the while loop
    for better performance.

Input from Diamond Operator
  • lt gt is another way to read input
  • it uses invocation arguments to accept input
  • invocation arguments are command line arguments
  • Exp
  • ./ x y z
  • means to run myprogram and it should process file
    x followed by file y and then file z
  • If you give it no invocation args program
    should process the standard input stream or x
    means stdin as well.

  • you can choose where the program gets its input
    at run time
  • exp you wont have to rewrite the program to use
    it in a pipeline ( more later).
  • makes it easy for you to write your programs that
    work like standard Unix utilities even on
    non-Unix machines.
  • portability of code

  • Exp
  • while ( defined(line ltgt))
  • chomp (line)
  • print It was line that I saw \n
  • run this with x, y, z invocation args
  • It was a line from file x that I saw
  • It was a line from file x that I saw
  • -- eof then y then z -- no break with using
    the ltgt its as if all the files are merged into

Same Shortcut
  • may also use the same shortcut
  • while (ltgt)
  • chomp
  • print It was _ that I saw!\n
  • Typically used for all of your input mistake to
    use it in more than one place in your program.

Unix Invocation Arguments
  • Typically, the diamond operator isnt looking at
    the invocation args its works with the _at_ARGV
  • It is a special array preset by Perl
    interpreter to be a list of command line args.
  • When program starts _at_ARGV is stuffed full of the
    list of invocation args.
  • Use it like any other array shift things off or
    foreach through it.
  • If list is empty in _at_ARGV -- ltgt looks uses STDIN
    stream otherwise it uses contents of that array

Tinker with _at_ARGV
  • Exp process three specific files regardless of
    user choice at CL
  • _at_ARGV qw! Monday Tuesday Wednesday !
  • while (ltgt)
  • chomp
  • print It is _ \n

Output to Standard Output
  • print operator takes a list of values and sends
    them as a string to stdout one after
  • doesnt add any extra characters before, after or
    in between by default.
  • perlvar man page gives you more information about
    changing defaults
  • if you want spaces between items and a newline at
    the end you have to add them

Output - STDOUT
  • name Bob Barker
  • print Hello there, name, did you know that 45
    is , 45, ?\n
  • difference between printing an array and
    interpolating an array
  • print _at_array print a list of items
  • print _at_array print a string (containing and
    interpolated array)

Print Statment
  • First print statement -- one item after another
    no spaces
  • Second statement will print exactly one item,
    which is the string you get by interpolating
    _at_array into the empty string the contents of
    the array separated by spaces.

Print Contd
  • But what if _at_array is a list of unchomped lines
    of input?
  • Contain trailing newline character
  • First print statement
  • Monday
  • Tuesday
  • Wednesday
  • Second one
  • Monday
  • Tuesday
  • Wednesay
  • its interpolating the array so it puts spaces
    between elements this is what happens when you
    put an array in double quotes

So .
  • So if your strings contain newlines
  • print _at_array
  • no newlines
  • print _at_array\n

Print Contd
  • print is looking for a list of strings to print
  • its arguments are evaluated in list context
  • ltgt will return a list of lines in list context
  • so
  • print lt gt similar to the cat command
  • print sort ltgt similar to Unix sort command

Revisit Parenthesis
  • print (Hello, World\n)
  • print Hello, World\n
  • if the invocation of print looks like a function
    call, then it is a function call
  • print (54) prints 20 but also contains a
    return value true / false
  • usually always succeeds unless I/O error

Return Value Contd
  • print (54) 3
  • error prints 20 but return value is 1 1 3 is
    4 logic error.
  • If it looks like a function it is a function
  • applies to all Perl functions

Formatted Output with printf
  • Cs printf is similar to Perls printf
  • take a format string followed b a list of things
    to print
  • the format string is a fill-in-the-blanks
    template with the desired form of output.
  • printf Hello, s your password expires in d
    days!\n, user, days_to_expire

  • conversions begin with
  • should be the same number of items in the list as
    there are conversions
  • dont match dont work
  • common conversions
  • s string, g auto chooses int, fp or exp
  • d decimal point value is truncated not

printf Contd
  • field width
  • printf 6d\n, 42 - - - -42
  • printf 2d\n, 2e3 1.95 2001
  • s string
  • it interpolates the given value as a string
  • printf 10s\n, perl ------perl
  • negative field left justifies
  • printf -15s\n, perl perl-----------

  • rounds off its output as needed
  • even lets you request number of digits after the
    decimal pt.
  • printf 12f\n, 67 2/3 ---42.666667
  • printf 12.3f\n, 67 2/3 ------42.667
  • printf 12.0f\n, 67 2/3 ----------43
  • to print a real sign use

Arrays and printf
  • You wont use an array as an argument to printf
  • my _at_items qw( soap shampoo conditioner)
  • my format The items are \n . (10s\n x
  • printf format, _at_items
  • This uses the x operator to replicate the given
    string a number of times given by _at_items
  • Thats 3 in this case look at our array values
  • output prints item on its own line right
    justified in a ten character column
  • We use _at_items once in a list context and once in
    a scalar context

  • Perform 2, 3 exercises on page 97