Week Four Agenda

Post on 07-Feb-2016

37 views 0 download

description

Week Four Agenda. Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines Lab assistance, questions and answers. Link of the Week. Object Code - PowerPoint PPT Presentation

transcript

Week Four Agenda•Announcements•Link of the week•Review week three lab assignment•This week’s expected outcomes•Next lab assignment•Break-out problems•Upcoming deadlines•Lab assistance, questions and answers

Link of the WeekObject Code

• http://freedom-to-tinker.com/blog/felten/source-code-and-object-code

• What is object code?• Object File Format• What is Executable and Linking Format?

Link of the Week

SourceFile

SourceFile

SourceFile

SourceFile

SourceFile

Object File

Object File

ObjectFile

Object File

ObjectFile

LinkerRuntimeLibrary

ExecutableProgram

Source/Object/Executable Drawing

Link of the Week

Review Week Three Lab AssignmentScript CommentsScripts should contain comments to help the

user understand what the script is doing or not doing. The functionality comments should be detailed enough to give the user a good overall understanding about the script.

Execution contingencies:Script functionality:Script limitations:Input data:

Review Week Three Lab AssignmentA process associates a number with each file that

it has opened. This number is called a file descriptor. When you log in, your first process has the following three open files connected to your terminal.

Standard Input (stdin) : File descriptor 0 is open for reading. < really means <0

Standard Output (stdout): File descriptor 1 is open for writing. > really means 1>

Standard Error (stderr): File descriptor 2 is open reading. >> really means 1>>

Review Week Three Lab AssignmentPerl is a simple language that compiles and

executes like a shell or batch type file.Perl doesn’t impose special growth

limitations on an array or data stringsPerl is a composite of C, AWK, and Basic.Perl was originally developed to

manipulate text information.

Review Week Three Lab Assignment• Perl’s capabilities range from

- System administration- Web development- Network programming- GUI development

• Perl’s major features are- Procedural Programming makes use of simple

sequential steps, routines, subroutines, and methods.

- Object Oriented Programming (OOP) makes use of “objects”. The key elements of are inheritance, modularity, polymorphism, and encapsulation.

Review Week Three Lab AssignmentPerl and Shell Similarities

Perl scalar@ARGV ~ Shell $#Perl $ARGV[0] ~ Shell $1Perl $ARGV[1] ~ Shell $2Perl unless(scalar(@ARGV)==2) ~ Shell if [ $# != 2]All Perl statements are terminated with a “;”Perl exit 0 is returned if execution was

successful.Perl exit 1 is returned if execution fails.

Review Week Three Lab AssignmentPerl syntax$? - this variable contains the return value # - precedes a comment statement in Perl\n - new line syntax“ …” $strexp = “This text is considered as a

string”;‘ …’ $charexp = ‘a’;` …` $cmdexp = `ls –l`;@ARGV – array containing command line

arguments$_ - default implied scalar

Review Week Three Lab Assignment

There are two types of relational operators. One class operates on numeric values, the other on string values.

Relational operatorsNumeric String Meaning > gt Greater than >= ge Greater than or equal < lt Less than <= le Less than or equal

Review Week Three Lab Assignment

Equality OperatorsNumeric String Meaning == eq Equal to != ne Not equal to cmp Comparison, sign

results-1 if the left operand is less 0 If both operands equal 1 If the left operand is greater

Review Week Three Lab Assignment

Commandscal –y (display a calandar for the year)cal –j 2010 (display Julian dates)cal –m 2010 (display Monday first day)cal –s 2010 (display Sunday first day)cal 9 2010 (display September 2009 month)

Week Four Expected OutcomesLearning Outcomes• Write Perl scripts, including variables, control

flow, and regular expression syntax

Next Lab Assignment• Perl is designed to

- Process text data - Perform pattern matching (regular

expressions)- Utilize string handling tasks

• Perl is available on many platforms - UNIX- Linux- HP-UX

Next Lab AssignmentPerl utilizes two types of categories

- Singular variables that represent a single-value. The variable prefix symbol for a scalar is the $.

Example: $scalar_variable_name

- Plural variables are ones that contain multiple-values. Arrays and hashes are two multi-valued variables.

Example: @array_variable_name

Example: %hash_variable_name

Next Lab AssignmentPerl data types

$answer = 42; (an integer)$pi = 3.14159265; (a “real” number)$animal = “horse”; (string)$statement = “I exercise my $animal”; (string with

interpolation)$amount = ‘It cost me $5.00’; (string without

interpolation)$cwd = `pwd`; (string output from a

command)

Next Lab AssignmentDefinition: An array is an ordered list of scalars,

accessed by the scalar’s position in the list.

@garage = (“car”, “mower”, “broom”);

@persons = (“Will”, “Karim”, “Asma”, “Jay”);$count = @persons;

DemonstrateExecute week_four.pl script

Next Lab AssignmentOpen StatementThe open function can be used to create file

handles for different purposes (input, output, piping), you need to be able to specify which behavior you want.

Next Lab Assignmentopen function

# Input fileopen(file_handler, “file_name”)# Input fileopen(file_handler, “<file_name”)# Output fileopen (file_handler, “>file_name”)# Append fileopen (file_handler, “>>file_name”)

Next Lab AssignmentDefinition: Filehandle is utilized for both input and

output files. Most file names are cryptic and are meaningless to programmers. The purpose of a filehandle is to help the programmer remember a simple file name throughout a program.

A filehandle is a name given for a file, device, socket, or pipe.

Filehandle command line format:open(filehandle, file name, permissions,

chmod);

Example: open($FH,$file_name);

Next Lab Assignment

List Processing

Example: @math_array = (6 - 4, 4 * 4, 8 / 2, 9 - 8);while ( … ) {

… }

Next Lab AssignmentFor loop

Example: for (counter = 0; counter < 10; counter++) {

…}

Three expressions are contained in a for loop: 1. Set initial state of the loop variable2. Condition test the loop variable3. Modify the state of the loop variable

Next Lab AssignmentPerl SubroutinesIs a named piece of program that can be invoked from

elsewhere in the program in order to accomplish some sub-goal of the program.

Subroutine Format

sub NAME BLOCK  

Example: sub subroutine_name { print "Hello, ITEC 400 class. \n"; }

Next Lab Assignmentmy OperatorInitially, Perl observes the enclosed block to see what

variable(s) are declared in the same block with the “my” declaration. The variable is lexically scoped and doesn’t exist in any other package. It exists only in that lexical scope.

This operator declares one or more variables to exist only within the inner most enclosed block.

Example: sub subroutine_name { my $name = shift; print "Hello, my name is Gail. \n"; }

Next Lab AssignmentShift FunctionThe shift operator takes the next option in the active

array.

Example: sub subroutine_name {# Use the presently referenced array.

my $name = shift; print "Hello, my name is $name. \n"; }

use DeclarationThe use declaration loads in a module and imports

subroutines and variables into the current package from the named module.

Next Lab Assignment

use Declaration

Example: # All possible restrictions are assumed. use strict;

# Subroutine correct_msg sub correct_msg{ print_msg(" Good message. \n"); }

There are three possible options to be strict about. They are vars, refs, and subs. Strict without options is the safest, but most restrictive mode to operate.

Next Lab AssignmentForeach loop

Example: foreach VAR (List){

…}

DemonstrateExecute read_list.pl script

Next Lab AssignmentForeach loop

Example: @myNames = ('Larry', 'Curly', 'Moe');foreach (@myNames) {

print $_;}

DemonstrateExecute sum_list.pl script

Execute arry_sort.pl script

Next lab assignmentPerl Program Statement

#!/usr/bin/perl #!/usr/bin/perl –w

Print continuation statementprint "error: incorrect number of arguments", "\n", "usage: intlist a b (where a < b)", "\n";

Next lab assignment

Demonstrate Lab Assignment 4-1Execute linenum.pl and intlist.pl scripts.

Break-out problems1. $strexp = “This text is considered as a

string”;2. $intexp = 10;3. $floatptexp = 2.54;4. $charexp = ‘a’;5. $cmdexp = `ls –l`;6. $argexp = (“two”, “four”, “six”);7. @arrayexp = (“Jackie”, “Vicki”, “Alex”);8. $arrayexp[0] = “new value”;9. $a = $b + 5;10.$container = @container;11. ($map{blue}, $map{orange}, $map{jade}) = (0xff0000, 0x00ff00,

0x0000ff0);

Next Lab AssignmentProgramming Perl text book reading

Chapter OneChapter TwoChapter Three

Next Lab AssignmentThe following Perl scripts have been copied to

the /tmp system directory for you to copy and use personally:

substr.ploutput_msg.plsub_arg_pass.plread_list.plsum_list.plarry_sort.pl

Upcoming deadlines

• Lab Assignment 3-1, Advanced Scripting, due January 31, 2010.

• Lab Assignment 4-1, Simple Perl Exercise, due February 7, 2010.

• Read Module Three listed under the course Web site.

Questions and answers• Questions• Comments• Concerns

• After class, I will help students with their scripts.