Advanced Programming Principles CM20214/CM20221
Macro languages
Purpose: to improve readability of other code, abstraction, textual manipulation
Examples: Cpp, LATEX, M4, macros in Lisp Notable features: usually lexical (character or text) based, with some exceptions (Lisp, Rust)
1 / 103 Macro languages Feet
• LATEX: \documentclass[12pt]{article} \usepackage{latexgun,latexshoot} \begin{document} See how easy it is to shoot yourself in the foot? \\ \gun[leftfoot]{shoot} \\ \pain \end{document}
2 / 103 Often used in conjunction with another language, e.g., the C preprocessor
So not often thought about in great detail, but are used to great effect
Particularly conditional macros whose expansion depends on other factors
Macro languages
These are used widely, in a huge variety of contexts
3 / 103 So not often thought about in great detail, but are used to great effect
Particularly conditional macros whose expansion depends on other factors
Macro languages
These are used widely, in a huge variety of contexts
Often used in conjunction with another language, e.g., the C preprocessor
4 / 103 Particularly conditional macros whose expansion depends on other factors
Macro languages
These are used widely, in a huge variety of contexts
Often used in conjunction with another language, e.g., the C preprocessor
So not often thought about in great detail, but are used to great effect
5 / 103 Macro languages
These are used widely, in a huge variety of contexts
Often used in conjunction with another language, e.g., the C preprocessor
So not often thought about in great detail, but are used to great effect
Particularly conditional macros whose expansion depends on other factors
6 / 103 Then if we use NUMBER everywhere in our code NUMBER x; ... it takes only a single change to make our code use short rather than int: very useful for source code portability between architectures
Macro languages
So, in C, we can write code like
#ifdef SMALLINT #define NUMBER short #else #define NUMBER int #endif
7 / 103 Macro languages
So, in C, we can write code like
#ifdef SMALLINT #define NUMBER short #else #define NUMBER int #endif
Then if we use NUMBER everywhere in our code NUMBER x; ... it takes only a single change to make our code use short rather than int: very useful for source code portability between architectures
8 / 103 C #define _ F-->00||-F-OO--; int F=00,OO=00;main(){F_OO();printf("%1.3f\n",4.*-F/OO/OO);}F_OO() { _-_-_-_ _-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_ _-_-_-_ }
An enthusiastic use of C macros by Brian Westley
9 / 103 And the macro language is Lisp itself, not a separate language: recall that programs are data! (defmacro head (l) (list ’car l))
defmacro defines the code to be executed (in the compiler or interpreter) when expanding the macro named head
When compiling (head x), the compiler first expands the macro head by executing (list ’car l), where l has the value x, to get (car x)
The compiler then compiles (car x)
Macro languages
In contrast to most languages, Lisp macros are not text based, but expression based
10 / 103 (defmacro head (l) (list ’car l))
defmacro defines the code to be executed (in the compiler or interpreter) when expanding the macro named head
When compiling (head x), the compiler first expands the macro head by executing (list ’car l), where l has the value x, to get (car x)
The compiler then compiles (car x)
Macro languages
In contrast to most languages, Lisp macros are not text based, but expression based
And the macro language is Lisp itself, not a separate language: recall that programs are data!
11 / 103 defmacro defines the code to be executed (in the compiler or interpreter) when expanding the macro named head
When compiling (head x), the compiler first expands the macro head by executing (list ’car l), where l has the value x, to get (car x)
The compiler then compiles (car x)
Macro languages
In contrast to most languages, Lisp macros are not text based, but expression based
And the macro language is Lisp itself, not a separate language: recall that programs are data! (defmacro head (l) (list ’car l))
12 / 103 When compiling (head x), the compiler first expands the macro head by executing (list ’car l), where l has the value x, to get (car x)
The compiler then compiles (car x)
Macro languages
In contrast to most languages, Lisp macros are not text based, but expression based
And the macro language is Lisp itself, not a separate language: recall that programs are data! (defmacro head (l) (list ’car l)) defmacro defines the code to be executed (in the compiler or interpreter) when expanding the macro named head
13 / 103 The compiler then compiles (car x)
Macro languages
In contrast to most languages, Lisp macros are not text based, but expression based
And the macro language is Lisp itself, not a separate language: recall that programs are data! (defmacro head (l) (list ’car l)) defmacro defines the code to be executed (in the compiler or interpreter) when expanding the macro named head
When compiling (head x), the compiler first expands the macro head by executing (list ’car l), where l has the value x, to get (car x)
14 / 103 Macro languages
In contrast to most languages, Lisp macros are not text based, but expression based
And the macro language is Lisp itself, not a separate language: recall that programs are data! (defmacro head (l) (list ’car l)) defmacro defines the code to be executed (in the compiler or interpreter) when expanding the macro named head
When compiling (head x), the compiler first expands the macro head by executing (list ’car l), where l has the value x, to get (car x)
The compiler then compiles (car x)
15 / 103 (defmacro baz (l m) (if (> m 0) (list ’car l) (list ’cdr l)))
Will expand baz at compile time to a car or a cdr
Macro languages
The full power of Lisp applies to macroexpansion
16 / 103 Macro languages
The full power of Lisp applies to macroexpansion
(defmacro baz (l m) (if (> m 0) (list ’car l) (list ’cdr l)))
Will expand baz at compile time to a car or a cdr
17 / 103 These slides are written in LATEX \begin{frame} \frametitle{Macro languages} \framesubtitle{\LaTeX}
Sometimes macros are used in their own right, e.g., \LaTeX, the typesetting language is macro based
\paruncover2{These slides are written in \LaTeX} \end{frame}
Macro languages LATEX
Sometimes macros are used in their own right, e.g., LATEX, the typesetting language is macro based
18 / 103 \begin{frame} \frametitle{Macro languages} \framesubtitle{\LaTeX}
Sometimes macros are used in their own right, e.g., \LaTeX, the typesetting language is macro based
\paruncover2{These slides are written in \LaTeX} \end{frame}
Macro languages LATEX
Sometimes macros are used in their own right, e.g., LATEX, the typesetting language is macro based
These slides are written in LATEX
19 / 103 Macro languages LATEX
Sometimes macros are used in their own right, e.g., LATEX, the typesetting language is macro based
These slides are written in LATEX \begin{frame} \frametitle{Macro languages} \framesubtitle{\LaTeX}
Sometimes macros are used in their own right, e.g., \LaTeX, the typesetting language is macro based
\paruncover2{These slides are written in \LaTeX} \end{frame}
20 / 103 For example, a new chapter needs a new page, a big heading, lots of space before the next text
So you define a macro, say \chapter, that does all this
Usually, someone else has created an entire macro package so you don’t have to
Macro languages LATEX
The basic datatype is text and you define macros as convenient shorthands for things you like to do to that text
21 / 103 So you define a macro, say \chapter, that does all this
Usually, someone else has created an entire macro package so you don’t have to
Macro languages LATEX
The basic datatype is text and you define macros as convenient shorthands for things you like to do to that text
For example, a new chapter needs a new page, a big heading, lots of space before the next text
22 / 103 Usually, someone else has created an entire macro package so you don’t have to
Macro languages LATEX
The basic datatype is text and you define macros as convenient shorthands for things you like to do to that text
For example, a new chapter needs a new page, a big heading, lots of space before the next text
So you define a macro, say \chapter, that does all this
23 / 103 Macro languages LATEX
The basic datatype is text and you define macros as convenient shorthands for things you like to do to that text
For example, a new chapter needs a new page, a big heading, lots of space before the next text
So you define a macro, say \chapter, that does all this
Usually, someone else has created an entire macro package so you don’t have to
24 / 103 They are called “scripts” as they are (were originally) lists of things to be done
Often, but not exclusively, interpreted rather than compiled
Scripting Languages
Purpose: control of other elements of a system, e.g., programs, “glue” to join elements together
Examples: DOS batch, sh, Python, Sed, Perl, Ruby, JavaScript . . .
Notable features: not particularly good at classical number crunching; generally lots of string processing and process manipulation
25 / 103 Often, but not exclusively, interpreted rather than compiled
Scripting Languages
Purpose: control of other elements of a system, e.g., programs, “glue” to join elements together
Examples: DOS batch, sh, Python, Sed, Perl, Ruby, JavaScript . . .
Notable features: not particularly good at classical number crunching; generally lots of string processing and process manipulation
They are called “scripts” as they are (were originally) lists of things to be done
26 / 103 Scripting Languages
Purpose: control of other elements of a system, e.g., programs, “glue” to join elements together
Examples: DOS batch, sh, Python, Sed, Perl, Ruby, JavaScript . . .
Notable features: not particularly good at classical number crunching; generally lots of string processing and process manipulation
They are called “scripts” as they are (were originally) lists of things to be done
Often, but not exclusively, interpreted rather than compiled
27 / 103 • Sh, csh, bash: You can’t remember the syntax for anything so you spend five hours reading man pages then your foot falls asleep. You then shoot the computer and switch to Perl • Perl: You shoot yourself in the foot, but nobody can understand how you did it. Six months later, neither can you • Ruby: Your foot is ready to be shot in roughly five minutes, but you just can’t find anywhere to shoot it • JavaScript: You’ve perfected a robust, rich user experience for shooting yourself in the foot. You then find that bullets are disabled on your gun
Scripting Languages Feet • DOS batch: You aim the gun at your foot and pull the trigger, but only a weak gust of warm air hits your foot
28 / 103 • Perl: You shoot yourself in the foot, but nobody can understand how you did it. Six months later, neither can you • Ruby: Your foot is ready to be shot in roughly five minutes, but you just can’t find anywhere to shoot it • JavaScript: You’ve perfected a robust, rich user experience for shooting yourself in the foot. You then find that bullets are disabled on your gun
Scripting Languages Feet • DOS batch: You aim the gun at your foot and pull the trigger, but only a weak gust of warm air hits your foot • Sh, csh, bash: You can’t remember the syntax for anything so you spend five hours reading man pages then your foot falls asleep. You then shoot the computer and switch to Perl
29 / 103 • Ruby: Your foot is ready to be shot in roughly five minutes, but you just can’t find anywhere to shoot it • JavaScript: You’ve perfected a robust, rich user experience for shooting yourself in the foot. You then find that bullets are disabled on your gun
Scripting Languages Feet • DOS batch: You aim the gun at your foot and pull the trigger, but only a weak gust of warm air hits your foot • Sh, csh, bash: You can’t remember the syntax for anything so you spend five hours reading man pages then your foot falls asleep. You then shoot the computer and switch to Perl • Perl: You shoot yourself in the foot, but nobody can understand how you did it. Six months later, neither can you
30 / 103 • JavaScript: You’ve perfected a robust, rich user experience for shooting yourself in the foot. You then find that bullets are disabled on your gun
Scripting Languages Feet • DOS batch: You aim the gun at your foot and pull the trigger, but only a weak gust of warm air hits your foot • Sh, csh, bash: You can’t remember the syntax for anything so you spend five hours reading man pages then your foot falls asleep. You then shoot the computer and switch to Perl • Perl: You shoot yourself in the foot, but nobody can understand how you did it. Six months later, neither can you • Ruby: Your foot is ready to be shot in roughly five minutes, but you just can’t find anywhere to shoot it
31 / 103 Scripting Languages Feet • DOS batch: You aim the gun at your foot and pull the trigger, but only a weak gust of warm air hits your foot • Sh, csh, bash: You can’t remember the syntax for anything so you spend five hours reading man pages then your foot falls asleep. You then shoot the computer and switch to Perl • Perl: You shoot yourself in the foot, but nobody can understand how you did it. Six months later, neither can you • Ruby: Your foot is ready to be shot in roughly five minutes, but you just can’t find anywhere to shoot it • JavaScript: You’ve perfected a robust, rich user experience for shooting yourself in the foot. You then find that bullets are disabled on your gun
32 / 103 The original scripts were the job control languages for the early mainframes
For example, IBM’s JCL was used to describe a list of programs to be loaded and run: recall batch processing
Scripting Languages
Also widely used
33 / 103 For example, IBM’s JCL was used to describe a list of programs to be loaded and run: recall batch processing
Scripting Languages
Also widely used
The original scripts were the job control languages for the early mainframes
34 / 103 Scripting Languages
Also widely used
The original scripts were the job control languages for the early mainframes
For example, IBM’s JCL was used to describe a list of programs to be loaded and run: recall batch processing
35 / 103 Scripting Languages Feet
• JCL: You send your foot down to MIS with a 4000-page document explaining how you want it to be shot. Three years later, your foot comes back deep-fried
36 / 103 Simple textual lists of things (programs) to be done
#!/bin/sh setxkbmap -option "compose:menu" -option "ctrl:nocaps" dispwin -L [ "$XAUTHORITY" ] && cp -f "$XAUTHORITY" ~/.Xauthority
Scripting Languages Sh
The Unix command line language, originally sh (the Bourne Shell), lately bash (the Bourne Again shell)
37 / 103 #!/bin/sh setxkbmap -option "compose:menu" -option "ctrl:nocaps" dispwin -L [ "$XAUTHORITY" ] && cp -f "$XAUTHORITY" ~/.Xauthority
Scripting Languages Sh
The Unix command line language, originally sh (the Bourne Shell), lately bash (the Bourne Again shell)
Simple textual lists of things (programs) to be done
38 / 103 Scripting Languages Sh
The Unix command line language, originally sh (the Bourne Shell), lately bash (the Bourne Again shell)
Simple textual lists of things (programs) to be done
#!/bin/sh setxkbmap -option "compose:menu" -option "ctrl:nocaps" dispwin -L [ "$XAUTHORITY" ] && cp -f "$XAUTHORITY" ~/.Xauthority
39 / 103 Originally reasonably simple but they have grown more complex over time
Crucially, they do not require a GUI so can be deployed automatically over large numbers of machines
Somewhat low-level, so not so good for more complex tasks, or less complex programmers
Scripting Languages Sh
Shell scripts are widely used to automate repetitive and complex tasks
40 / 103 Crucially, they do not require a GUI so can be deployed automatically over large numbers of machines
Somewhat low-level, so not so good for more complex tasks, or less complex programmers
Scripting Languages Sh
Shell scripts are widely used to automate repetitive and complex tasks
Originally reasonably simple but they have grown more complex over time
41 / 103 Somewhat low-level, so not so good for more complex tasks, or less complex programmers
Scripting Languages Sh
Shell scripts are widely used to automate repetitive and complex tasks
Originally reasonably simple but they have grown more complex over time
Crucially, they do not require a GUI so can be deployed automatically over large numbers of machines
42 / 103 Scripting Languages Sh
Shell scripts are widely used to automate repetitive and complex tasks
Originally reasonably simple but they have grown more complex over time
Crucially, they do not require a GUI so can be deployed automatically over large numbers of machines
Somewhat low-level, so not so good for more complex tasks, or less complex programmers
43 / 103 String manipulation: sed is a stream editor
Reads files one line at a time (as a stream) and edits them, one line at a time, according to given rules sed -e ’s/hello/goodbye/’ Substitutes goodbye for occurrences of hello in its input
Scripting Languages Sed
Example: Sed. A very simple scripting language, capable of just one task
44 / 103 Reads files one line at a time (as a stream) and edits them, one line at a time, according to given rules sed -e ’s/hello/goodbye/’ Substitutes goodbye for occurrences of hello in its input
Scripting Languages Sed
Example: Sed. A very simple scripting language, capable of just one task
String manipulation: sed is a stream editor
45 / 103 sed -e ’s/hello/goodbye/’ Substitutes goodbye for occurrences of hello in its input
Scripting Languages Sed
Example: Sed. A very simple scripting language, capable of just one task
String manipulation: sed is a stream editor
Reads files one line at a time (as a stream) and edits them, one line at a time, according to given rules
46 / 103 Scripting Languages Sed
Example: Sed. A very simple scripting language, capable of just one task
String manipulation: sed is a stream editor
Reads files one line at a time (as a stream) and edits them, one line at a time, according to given rules sed -e ’s/hello/goodbye/’ Substitutes goodbye for occurrences of hello in its input
47 / 103 Can edit files too large to fit into memory all at once
Usually used as a component in shell scripts
Limited to line-by-line editing
More general is awk, but better is Perl
Scripting Languages Sed
Uses regular expressions for patterns
48 / 103 Usually used as a component in shell scripts
Limited to line-by-line editing
More general is awk, but better is Perl
Scripting Languages Sed
Uses regular expressions for patterns
Can edit files too large to fit into memory all at once
49 / 103 Limited to line-by-line editing
More general is awk, but better is Perl
Scripting Languages Sed
Uses regular expressions for patterns
Can edit files too large to fit into memory all at once
Usually used as a component in shell scripts
50 / 103 More general is awk, but better is Perl
Scripting Languages Sed
Uses regular expressions for patterns
Can edit files too large to fit into memory all at once
Usually used as a component in shell scripts
Limited to line-by-line editing
51 / 103 Scripting Languages Sed
Uses regular expressions for patterns
Can edit files too large to fit into memory all at once
Usually used as a component in shell scripts
Limited to line-by-line editing
More general is awk, but better is Perl
52 / 103 The basic datatype is the string
The basic operation is pattern matching
It is also procedural, has first class functions, has objects and so on
The syntax is the usual C/Java/whatever, but with a few features
Scripting Languages Perl
Perl is/was used a lot in Web page creation and manipulation, amongst many other things
53 / 103 The basic operation is pattern matching
It is also procedural, has first class functions, has objects and so on
The syntax is the usual C/Java/whatever, but with a few features
Scripting Languages Perl
Perl is/was used a lot in Web page creation and manipulation, amongst many other things
The basic datatype is the string
54 / 103 It is also procedural, has first class functions, has objects and so on
The syntax is the usual C/Java/whatever, but with a few features
Scripting Languages Perl
Perl is/was used a lot in Web page creation and manipulation, amongst many other things
The basic datatype is the string
The basic operation is pattern matching
55 / 103 The syntax is the usual C/Java/whatever, but with a few features
Scripting Languages Perl
Perl is/was used a lot in Web page creation and manipulation, amongst many other things
The basic datatype is the string
The basic operation is pattern matching
It is also procedural, has first class functions, has objects and so on
56 / 103 Scripting Languages Perl
Perl is/was used a lot in Web page creation and manipulation, amongst many other things
The basic datatype is the string
The basic operation is pattern matching
It is also procedural, has first class functions, has objects and so on
The syntax is the usual C/Java/whatever, but with a few features
57 / 103 Scripting Languages Perl open IN, ’<’, ’infile’; open OUT, ’>outfile’; $count = 0; while (
58 / 103 • or joined onto the filename • IN: filestream variables are syntactically different from normal variables • $count: scalar (single value) variable names are prefixed by $ • array variable names are prefixed by @ • function variable names are prefixed by & • $f is separate from @f and &f • <>: operator returns a single line of the file each time it is called • and false at end of file • it assigns to the variable $
Scripting Languages Perl • open: takes strings ’<’ to indicate input and ’>’ to indicate output; then a file name
59 / 103 • IN: filestream variables are syntactically different from normal variables • $count: scalar (single value) variable names are prefixed by $ • array variable names are prefixed by @ • function variable names are prefixed by & • $f is separate from @f and &f • <>: operator returns a single line of the file each time it is called • and false at end of file • it assigns to the variable $
Scripting Languages Perl • open: takes strings ’<’ to indicate input and ’>’ to indicate output; then a file name • or joined onto the filename
60 / 103 • $count: scalar (single value) variable names are prefixed by $ • array variable names are prefixed by @ • function variable names are prefixed by & • $f is separate from @f and &f • <>: operator returns a single line of the file each time it is called • and false at end of file • it assigns to the variable $
Scripting Languages Perl • open: takes strings ’<’ to indicate input and ’>’ to indicate output; then a file name • or joined onto the filename • IN: filestream variables are syntactically different from normal variables
61 / 103 • array variable names are prefixed by @ • function variable names are prefixed by & • $f is separate from @f and &f • <>: operator returns a single line of the file each time it is called • and false at end of file • it assigns to the variable $
Scripting Languages Perl • open: takes strings ’<’ to indicate input and ’>’ to indicate output; then a file name • or joined onto the filename • IN: filestream variables are syntactically different from normal variables • $count: scalar (single value) variable names are prefixed by $
62 / 103 • function variable names are prefixed by & • $f is separate from @f and &f • <>: operator returns a single line of the file each time it is called • and false at end of file • it assigns to the variable $
Scripting Languages Perl • open: takes strings ’<’ to indicate input and ’>’ to indicate output; then a file name • or joined onto the filename • IN: filestream variables are syntactically different from normal variables • $count: scalar (single value) variable names are prefixed by $ • array variable names are prefixed by @
63 / 103 • $f is separate from @f and &f • <>: operator returns a single line of the file each time it is called • and false at end of file • it assigns to the variable $
Scripting Languages Perl • open: takes strings ’<’ to indicate input and ’>’ to indicate output; then a file name • or joined onto the filename • IN: filestream variables are syntactically different from normal variables • $count: scalar (single value) variable names are prefixed by $ • array variable names are prefixed by @ • function variable names are prefixed by &
64 / 103 • <>: operator returns a single line of the file each time it is called • and false at end of file • it assigns to the variable $
Scripting Languages Perl • open: takes strings ’<’ to indicate input and ’>’ to indicate output; then a file name • or joined onto the filename • IN: filestream variables are syntactically different from normal variables • $count: scalar (single value) variable names are prefixed by $ • array variable names are prefixed by @ • function variable names are prefixed by & • $f is separate from @f and &f
65 / 103 • and false at end of file • it assigns to the variable $
Scripting Languages Perl • open: takes strings ’<’ to indicate input and ’>’ to indicate output; then a file name • or joined onto the filename • IN: filestream variables are syntactically different from normal variables • $count: scalar (single value) variable names are prefixed by $ • array variable names are prefixed by @ • function variable names are prefixed by & • $f is separate from @f and &f • <>: operator returns a single line of the file each time it is called
66 / 103 • it assigns to the variable $
Scripting Languages Perl • open: takes strings ’<’ to indicate input and ’>’ to indicate output; then a file name • or joined onto the filename • IN: filestream variables are syntactically different from normal variables • $count: scalar (single value) variable names are prefixed by $ • array variable names are prefixed by @ • function variable names are prefixed by & • $f is separate from @f and &f • <>: operator returns a single line of the file each time it is called • and false at end of file
67 / 103 Scripting Languages Perl • open: takes strings ’<’ to indicate input and ’>’ to indicate output; then a file name • or joined onto the filename • IN: filestream variables are syntactically different from normal variables • $count: scalar (single value) variable names are prefixed by $ • array variable names are prefixed by @ • function variable names are prefixed by & • $f is separate from @f and &f • <>: operator returns a single line of the file each time it is called • and false at end of file • it assigns to the variable $ 68 / 103 • statement if (test); as well as the usual if (test) { statements; } (and with else) • // for pattern matching; in this case it looks to see if the default $ contains the string hello • s///: if the string contains world, replace it by the string everybody (again, using the default $ ) • $count++; lots of C-like features • untyped variables: a variable can hold numbers and strings and other types; so ++ has first to check if the value is a number or a string containing a number, which it then converts to a number • flexibility over () around function arguments
Scripting Languages Perl • $ is a default argument and can be left out of many places, e.g., print; is equivalent to print $ ;
69 / 103 • // for pattern matching; in this case it looks to see if the default $ contains the string hello • s///: if the string contains world, replace it by the string everybody (again, using the default $ ) • $count++; lots of C-like features • untyped variables: a variable can hold numbers and strings and other types; so ++ has first to check if the value is a number or a string containing a number, which it then converts to a number • flexibility over () around function arguments
Scripting Languages Perl • $ is a default argument and can be left out of many places, e.g., print; is equivalent to print $ ; • statement if (test); as well as the usual if (test) { statements; } (and with else)
70 / 103 • s///: if the string contains world, replace it by the string everybody (again, using the default $ ) • $count++; lots of C-like features • untyped variables: a variable can hold numbers and strings and other types; so ++ has first to check if the value is a number or a string containing a number, which it then converts to a number • flexibility over () around function arguments
Scripting Languages Perl • $ is a default argument and can be left out of many places, e.g., print; is equivalent to print $ ; • statement if (test); as well as the usual if (test) { statements; } (and with else) • // for pattern matching; in this case it looks to see if the default $ contains the string hello
71 / 103 • $count++; lots of C-like features • untyped variables: a variable can hold numbers and strings and other types; so ++ has first to check if the value is a number or a string containing a number, which it then converts to a number • flexibility over () around function arguments
Scripting Languages Perl • $ is a default argument and can be left out of many places, e.g., print; is equivalent to print $ ; • statement if (test); as well as the usual if (test) { statements; } (and with else) • // for pattern matching; in this case it looks to see if the default $ contains the string hello • s///: if the string contains world, replace it by the string everybody (again, using the default $ )
72 / 103 • untyped variables: a variable can hold numbers and strings and other types; so ++ has first to check if the value is a number or a string containing a number, which it then converts to a number • flexibility over () around function arguments
Scripting Languages Perl • $ is a default argument and can be left out of many places, e.g., print; is equivalent to print $ ; • statement if (test); as well as the usual if (test) { statements; } (and with else) • // for pattern matching; in this case it looks to see if the default $ contains the string hello • s///: if the string contains world, replace it by the string everybody (again, using the default $ ) • $count++; lots of C-like features
73 / 103 • flexibility over () around function arguments
Scripting Languages Perl • $ is a default argument and can be left out of many places, e.g., print; is equivalent to print $ ; • statement if (test); as well as the usual if (test) { statements; } (and with else) • // for pattern matching; in this case it looks to see if the default $ contains the string hello • s///: if the string contains world, replace it by the string everybody (again, using the default $ ) • $count++; lots of C-like features • untyped variables: a variable can hold numbers and strings and other types; so ++ has first to check if the value is a number or a string containing a number, which it then converts to a number
74 / 103 Scripting Languages Perl • $ is a default argument and can be left out of many places, e.g., print; is equivalent to print $ ; • statement if (test); as well as the usual if (test) { statements; } (and with else) • // for pattern matching; in this case it looks to see if the default $ contains the string hello • s///: if the string contains world, replace it by the string everybody (again, using the default $ ) • $count++; lots of C-like features • untyped variables: a variable can hold numbers and strings and other types; so ++ has first to check if the value is a number or a string containing a number, which it then converts to a number • flexibility over () around function arguments
75 / 103 • single quote is unevaluated: ’hello\n’ prints as hello\n • double quote is interpolated: "hello\n" prints as hello with a newline • "The count is $count" sticks the current value of $count into the string at that point
There are several very fat books on Perl
Its flexibility means it is used in a lot of places
And it is easy to write unreadable code in Perl
Scripting Languages Perl
• strings are both single and double quoted
76 / 103 • double quote is interpolated: "hello\n" prints as hello with a newline • "The count is $count" sticks the current value of $count into the string at that point
There are several very fat books on Perl
Its flexibility means it is used in a lot of places
And it is easy to write unreadable code in Perl
Scripting Languages Perl
• strings are both single and double quoted • single quote is unevaluated: ’hello\n’ prints as hello\n
77 / 103 • "The count is $count" sticks the current value of $count into the string at that point
There are several very fat books on Perl
Its flexibility means it is used in a lot of places
And it is easy to write unreadable code in Perl
Scripting Languages Perl
• strings are both single and double quoted • single quote is unevaluated: ’hello\n’ prints as hello\n • double quote is interpolated: "hello\n" prints as hello with a newline
78 / 103 There are several very fat books on Perl
Its flexibility means it is used in a lot of places
And it is easy to write unreadable code in Perl
Scripting Languages Perl
• strings are both single and double quoted • single quote is unevaluated: ’hello\n’ prints as hello\n • double quote is interpolated: "hello\n" prints as hello with a newline • "The count is $count" sticks the current value of $count into the string at that point
79 / 103 Its flexibility means it is used in a lot of places
And it is easy to write unreadable code in Perl
Scripting Languages Perl
• strings are both single and double quoted • single quote is unevaluated: ’hello\n’ prints as hello\n • double quote is interpolated: "hello\n" prints as hello with a newline • "The count is $count" sticks the current value of $count into the string at that point
There are several very fat books on Perl
80 / 103 And it is easy to write unreadable code in Perl
Scripting Languages Perl
• strings are both single and double quoted • single quote is unevaluated: ’hello\n’ prints as hello\n • double quote is interpolated: "hello\n" prints as hello with a newline • "The count is $count" sticks the current value of $count into the string at that point
There are several very fat books on Perl
Its flexibility means it is used in a lot of places
81 / 103 Scripting Languages Perl
• strings are both single and double quoted • single quote is unevaluated: ’hello\n’ prints as hello\n • double quote is interpolated: "hello\n" prints as hello with a newline • "The count is $count" sticks the current value of $count into the string at that point
There are several very fat books on Perl
Its flexibility means it is used in a lot of places
And it is easy to write unreadable code in Perl
82 / 103 Originally designed to be a scripting language to manage Web page content, it is now more likely to be thought of as a special-purpose language to enable dynamic Web content
Web applications like Google Maps are coded using JavaScript
In Firefox, the browser’s appearance, i.e., the buttons, toolbars and so on (the “chrome”), are programmed in JavaScript
This allows easy modification and extension (using “add-ons”)
Scripting Languages JavaScript
JavaScript is an interesting case
83 / 103 Web applications like Google Maps are coded using JavaScript
In Firefox, the browser’s appearance, i.e., the buttons, toolbars and so on (the “chrome”), are programmed in JavaScript
This allows easy modification and extension (using “add-ons”)
Scripting Languages JavaScript
JavaScript is an interesting case
Originally designed to be a scripting language to manage Web page content, it is now more likely to be thought of as a special-purpose language to enable dynamic Web content
84 / 103 In Firefox, the browser’s appearance, i.e., the buttons, toolbars and so on (the “chrome”), are programmed in JavaScript
This allows easy modification and extension (using “add-ons”)
Scripting Languages JavaScript
JavaScript is an interesting case
Originally designed to be a scripting language to manage Web page content, it is now more likely to be thought of as a special-purpose language to enable dynamic Web content
Web applications like Google Maps are coded using JavaScript
85 / 103 This allows easy modification and extension (using “add-ons”)
Scripting Languages JavaScript
JavaScript is an interesting case
Originally designed to be a scripting language to manage Web page content, it is now more likely to be thought of as a special-purpose language to enable dynamic Web content
Web applications like Google Maps are coded using JavaScript
In Firefox, the browser’s appearance, i.e., the buttons, toolbars and so on (the “chrome”), are programmed in JavaScript
86 / 103 Scripting Languages JavaScript
JavaScript is an interesting case
Originally designed to be a scripting language to manage Web page content, it is now more likely to be thought of as a special-purpose language to enable dynamic Web content
Web applications like Google Maps are coded using JavaScript
In Firefox, the browser’s appearance, i.e., the buttons, toolbars and so on (the “chrome”), are programmed in JavaScript
This allows easy modification and extension (using “add-ons”)
87 / 103 The name is unfortunate: their syntax is broadly similar but their semantics are wildly different
The name was originally chosen as JavaScript was intended to be “reminiscent” of Java, but now it’s just a source of confusion
It can be argued that JavaScript is more like Scheme/Lisp than Java in the way it behaves (first-class functions, etc.)
But you shouldn’t take this analogy too far
Scripting Languages JavaScript
Standard warning:
Java and JavaScript are different languages
88 / 103 The name was originally chosen as JavaScript was intended to be “reminiscent” of Java, but now it’s just a source of confusion
It can be argued that JavaScript is more like Scheme/Lisp than Java in the way it behaves (first-class functions, etc.)
But you shouldn’t take this analogy too far
Scripting Languages JavaScript
Standard warning:
Java and JavaScript are different languages
The name is unfortunate: their syntax is broadly similar but their semantics are wildly different
89 / 103 It can be argued that JavaScript is more like Scheme/Lisp than Java in the way it behaves (first-class functions, etc.)
But you shouldn’t take this analogy too far
Scripting Languages JavaScript
Standard warning:
Java and JavaScript are different languages
The name is unfortunate: their syntax is broadly similar but their semantics are wildly different
The name was originally chosen as JavaScript was intended to be “reminiscent” of Java, but now it’s just a source of confusion
90 / 103 But you shouldn’t take this analogy too far
Scripting Languages JavaScript
Standard warning:
Java and JavaScript are different languages
The name is unfortunate: their syntax is broadly similar but their semantics are wildly different
The name was originally chosen as JavaScript was intended to be “reminiscent” of Java, but now it’s just a source of confusion
It can be argued that JavaScript is more like Scheme/Lisp than Java in the way it behaves (first-class functions, etc.)
91 / 103 Scripting Languages JavaScript
Standard warning:
Java and JavaScript are different languages
The name is unfortunate: their syntax is broadly similar but their semantics are wildly different
The name was originally chosen as JavaScript was intended to be “reminiscent” of Java, but now it’s just a source of confusion
It can be argued that JavaScript is more like Scheme/Lisp than Java in the way it behaves (first-class functions, etc.)
But you shouldn’t take this analogy too far
92 / 103 • JavaScript: Sun/Mozilla/etc. • JScript: Microsoft • ActionScript: Adobe
Ecma: formerly European Computer Manufacturers Association, now just “Ecma International”, is a standards body
Scripting Languages JavaScript
JavaScript is more properly called ECMAScript and there are several close-but-not-perfectly-compatible versions around
93 / 103 Ecma: formerly European Computer Manufacturers Association, now just “Ecma International”, is a standards body
Scripting Languages JavaScript
JavaScript is more properly called ECMAScript and there are several close-but-not-perfectly-compatible versions around
• JavaScript: Sun/Mozilla/etc. • JScript: Microsoft • ActionScript: Adobe
94 / 103 Scripting Languages JavaScript
JavaScript is more properly called ECMAScript and there are several close-but-not-perfectly-compatible versions around
• JavaScript: Sun/Mozilla/etc. • JScript: Microsoft • ActionScript: Adobe
Ecma: formerly European Computer Manufacturers Association, now just “Ecma International”, is a standards body
95 / 103 Scripting Languages JavaScript function getdoc() { var input = document.getElementById("num"); var num = input.value;
if (parseInt(num) == num && num > 0 && num < 5000) { document.location = "http://www.rfc-editor.org/rfc/rfc" + num + ".txt"; } else { alert("Not a valid RFC number!"); input.value = ""; } return false; }
96 / 103 • variables declared by var, no type declarations • a variable can hold items of any type • syntax reminiscent of Java (and C and so on)
Scripting Languages JavaScript
• functions declared by function, no type declarations
97 / 103 • a variable can hold items of any type • syntax reminiscent of Java (and C and so on)
Scripting Languages JavaScript
• functions declared by function, no type declarations • variables declared by var, no type declarations
98 / 103 • syntax reminiscent of Java (and C and so on)
Scripting Languages JavaScript
• functions declared by function, no type declarations • variables declared by var, no type declarations • a variable can hold items of any type
99 / 103 Scripting Languages JavaScript
• functions declared by function, no type declarations • variables declared by var, no type declarations • a variable can hold items of any type • syntax reminiscent of Java (and C and so on)
100 / 103 It is OO, but in a very different way from other languages, particularly Java
We shall be talking more about JavaScript when we examine OO in detail
Scripting Languages JavaScript
JavaScript is easy to learn and use, good for prototyping
101 / 103 We shall be talking more about JavaScript when we examine OO in detail
Scripting Languages JavaScript
JavaScript is easy to learn and use, good for prototyping
It is OO, but in a very different way from other languages, particularly Java
102 / 103 Scripting Languages JavaScript
JavaScript is easy to learn and use, good for prototyping
It is OO, but in a very different way from other languages, particularly Java
We shall be talking more about JavaScript when we examine OO in detail
103 / 103