Context.

Slides:



Advertisements
Similar presentations
Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
1/12 Steven Leung Very Basic Perl Tricks A Few Ground Rules File I/O and Formatting Operators, Flow Control Statements Regular Expression Subroutines Hash.
Operators and Expressions Operators are symbols that produce a result based on some rules. Examples: +, -, =, > and
Perl Arrays and Lists Software Tools. Slide 2 Lists l A list is an ordered collection of scalar data. l A list begins and ends with parentheses, with.
Copyright © by Curt Hill Expressions and Assignment Statements Part 2.
Input from STDIN STDIN, standard input, comes from the keyboard. STDIN can also be used with file re-direction from the command line. For instance, if.
Perl Arrays and Lists Learning Objectives: 1. To understand the format and the declaration of Arrays & Lists in Perl 2. To distinguish the difference between.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
Scalar Variables Start the file with: #! /usr/bin/perl –w No spaces or newlines before the the #! “#!” is sometimes called a “shebang”. It is a signal.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
"Loose ends"CS-2301 D-term “Loose Ends” CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming Language, 2 nd edition,
Introduction to Perl. How to run perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Your program/script.
Perl Lecture #1 Scripting Languages Fall Perl Practical Extraction and Report Language -created by Larry Wall -- mid – 1980’s –needed a quick language.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Prog.Perl Most operators have numeric and string version –remember Perl will convert variable.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
Perl File I/O and Arrays. File I/O Perl allows to open a file to read, write, or append As well as pipe input or output to another program. —We get to.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
Variables and Constants
Control Structures. Important Semantic Difference In all of these loops we are going to discuss, the braces are ALWAYS REQUIRED. Even if your loop/block.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
CS 105 Perl: Data Types Nathan Clement 15 May 2014.
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
Perl Chapter 6 Functions. Subprograms In Perl, all subprograms are functions – returns 0 or 1 value – although may have “side-effects” optional function.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
CS 105 Perl: Basic I/O, Context, Strings, Lists Nathan Clement January 22, 2014.
A Few More Functions. One more quoting operator qw// Takes a space separated sequence of words, and returns a list of single-quoted words. –no interpolation.
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
Expressions and Data Types Professor Robin Burke.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Camel –perldoc perlop Many operators have numeric and string version –remember Perl will.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
CGS 3066: Web Programming and Design Spring 2017
A variable is a name for a value stored in memory.
VBScript Session 1 Dani Vainstein.
VBScript Session 1 IT Professional Academy.
Input from STDIN STDIN, standard input, comes from the keyboard.
Introduction to Python
2.5 Another Java Application: Adding Integers
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Perl Variables: Array Web Programming.
Conditions and Ifs BIS1523 – Lecture 8.
Control Structures: if Conditional
Control Structures: for & while Loops
Lesson 2. Control structures File IO - reading and writing Subroutines
Conditions and Boolean Expressions
Perl I/O Learning Objectives:
CSE 303 Concepts and Tools for Software Development
By C. Shing ITEC Dept Radford University
CISC101 Reminders Assignment 2 due today.
VBScript Session 1.
Selection Statements Chapter 3.
Subroutines.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
INTRODUCTION to PERL PART 1.
Class code for pythonroom.com cchsp2cs
CGS 3066: Web Programming and Design Spring 2016
Lecture 9: JavaScript Syntax
Presentation transcript:

Context

Context Every expression in Perl is evaluated in a specific “context”. mode, manner, meaning return value of expression can change depending on its context Perl variables and functions are evaluated in whatever context Perl is expecting for that situation Two *major* contexts – Scalar & List

Scalar Context $x = EXPR; if (EXPR < 5) { … } Perl is “expecting” a scalar, EXPR is evaluated in scalar context assign to a scalar variable, or use an operator or function that takes a scalar argument force scalar context by scalar keyword @array = scalar EXPR;

Scalar Sub-contexts Scalar values can be evaluated in Boolean, String, or Numeric contexts Boolean if(EXPR), while(EXPR), etc only two values, true and false 0, '0', '', and undef are all false (zero, string containing zero, empty string, undefined) anything else is true String print EXPR, $x = substr(EXPR,0,5), etc Numeric $x = EXPR + 5, if(EXPR < $val), etc Perl will *automatically* convert to and from each of these contexts for you. Almost never need to concern yourself with them.

Automatic Conversions If a number is used as a string, the conversion is straight forward. 853 becomes '853' -4.7 becomes '-4.7' If a string is used as a number, Perl will convert the string based on the first character(s) If first non-space character is ‘numeric’ (ie, number, period (decimal), or negative (hyphen)), converted number reads from start to first non-numeric character. '-534.4ab32'  -534.4 If first character is non-numeric, converted number is 0. 'a4332.5'  0 If a scalar is used in a conditional (if, while), it is treated as a boolean value

When does this happen? my $foo = 4; print "Enter a number\n"; my $bar = <STDIN>; #note no chomp my $sum = $foo + $bar; Note that $bar is unaffected. It’s just used as a number in that one statement Method for checking input for numeric data involves Regular Expressions ie, don’t worry about it for now

List Context @x = EXPR; $str = join (' ', EXPR); Assign to a list/array, or use in a function or operator that is expecting a list There is no analogy to the scalar keyword for lists. If you use a scalar in any kind of list context, it is “promoted” to a list. @array = 5; @array gets value: (5)

Context Fun arrays evaluated in scalar context produce the size of that array my @x = (4, 8, 12); my $size = @x; $size gets value 3. print "@x has " . @x . " values.\n"; The . operator expects a string on either side strings are scalars, so @x is evaluated in scalar context "4 8 12 has 3 values." Contrast: print "@x has ", @x, " values.\n"; Comma operator expects a list on either side. @x evaluated in list context Just printing a list of five values, so separated by $, (empty string) "4 8 12 has 4812 values" my @x = ('a', 'b', 'c'); my $y = @x; my ($z) = @x; same as my ($z) = ('a', 'b', 'c'); $y  3, $z  'a'

undef Any scalar variable which exists but is not defined has default value undef my ($a,$b,$c)=(15,20); # $c -> undef In string context, undef  '' In numeric context, undef  0 In boolean context, undef  false use warnings; will warn you about using an undefined value! warning message is misleading, refers to an "uninitialized" value. Can actually specifically set a scalar to undef. Array variables not given a value get the empty list my @bar; # @bar contains () my @foo = undef; #probably wrong! @foo contains one element: the undefined value To clear an existing array: @foo = (); If you need to do this, you probably declared your variable in too large a scope