Upload
buikhuong
View
231
Download
2
Embed Size (px)
Citation preview
History of Scripting Languages
Two principal sets of ancestors
Command interpreters (shells)
IBM JCL, MS-DOS command interpreter, Unix sh and csh
Various text processing/report generation tools
IBM RPG, Unix sed and awk
Later, general purpose scripting languages
Rexx (IBM) – Restructured Extended Executor, 1979
Perl (Larry Wall) – most widely used, late 1980s
Tcl (“tickle”), Python, Ruby, JavaScript (ECMA)
VBScript (Windows), AppleScript (Mac)
2
Characteristics of Scripting Languages
Both batch and interactive use
Interpret or compile line by line
Economy of expression
Avoid extensive declarations, top-level structures
Lack of declarations; simple scoping rules
Everything is global (Perl), or local (PHP, Tcl) by default
Flexible dynamic typing
Differently interpreted in different context
$a = “4”; print $a . 3 . “ ”; print $a + 3 . “\n”; => 43 7
Easy access to system facilities
Sophisticated pattern matching and string manipulation
Extended regular expression
High level data types
Support types such as sets, bags, dictionaries, lists, and tuples
3
Shell Script
Features designed for interactive use
Refer to manuals for sh, bash, csh, tcsh
Mechanisms
Filename and Variable Expansion
Filename matching with regular expressions, shell variables
Tests, Queries, and Conditions
Condition test, queries on file properties (creation time, ...)
Pipes and Redirection
Glue two applications input and output, replace console input/output
Quoting and Expansion
User defined functions
The #! Convention – script file can be executed with the specified shell
First line of script: #!/bin/bash
4
Sed - text processing and report gen.
Sed – stream editor
Extracting all headers in an HTML file
Pattern is matched with a regular expression
sed –e’/^[[:space:]]*$/d’ input-file (delete blank lines)
5
Awk - text processing and report gen.
Awk – Aho, Weinberger, and Kernighan designed in 1977
A sequence of patterns, each of which has an associated action
line-at-a-time filter model of computation (same as sed)
$0 represents a current line – getline reads into this variable
Each input line is parsed into $1, $2, ... (delimiter is white space by default)
6
Perl - text processing and report gen.
Perl – originally developed by Larry Wall in 1987
Original version was an attempt to combine sed, awk, and sh
Primarily for text processing (Practical Extraction and Report Language)
<> are the readline – reads into $_
.* matches the longest possible string of characters (maximal matching)
.*? matches the shortest possible string of characters (minimal matching)
s/pattern/replace/{option} – substitute
Option : i – ignore case, s – let . match newline, g – globally find all matches
7
Glue Lang. & General Purpose Scripting
Prepare input to, and parse output from, subsidiary processes
Perl, Tcl, Python, Ruby, …
Tcl – developed by John Ousterhout in the late 1980s
Tool command language is used as an extension language that
could be embedded in all the tools
Tk – a set of extension for graphical user interface
programming
In comparison to Perl, Tcl is more verbose
8
Perl – glue language
Interactive “force-quit” except the current program
9
# $$ is the current process ID
(SIGKILL)
(check if signal may be sent)
Python – glue language
Developed by Guido van Rossum, in the early 1990s
Object-oriented, rich standard library
11
# import standard library modules
Ruby – glue language
Developed by Yukihiro “Matz” Matsumoto in early 1990s
Pure object-oriented, Ruby on Rails – web development framework
12
ARGV.length() == 1 or begin
$stderr.print(“usage: #{$0} pattern\n”); exit(1)
end
pat = Regexp.new(ARGV[0])
IO.popen(“ps –w –w –x –o’pid,command’”) {|PS|
PS.gets # discard header line
PS.each {|line|
proc = line.split[0].to_i
if line =~ pat and proc != Process.pid then
print line.chomp
begin
print “? “
answer = $stdin.gets
end until answer =~ /^[yn]/i
if answer =~ /^y/i then
Process.kill(9, proc)
sleep(1)
begin # expect exception (process gone)
Process.kill(0, proc)
$stderr.print(“unsuccessful; sorry\n”); exit(1)
rescue # handler – do nothing
end
end
end
}
}
Scripting the World Wide Web
CGI Scripts
The original mechanism for server-side web scripting is the Common
Gateway Interface (CGI)
A CGI script is an executable program residing in a special directory
When a client requests the URI corresponding to such a program, the
server executes the program and sends its output back to the client
This output is what the browser will understand: typically HTML
CGI scripts may be written in any language available
Perl is particularly popular:
String-handling and “glue” mechanisms are suited to generating HTML
Already widely available during the early years of the web
13
CGI Script in Perl
Server executes the script and send the standard output to
the web browser (client)
e.g., http://arcs.skku.edu/cgi-bin/status.perl
14
/var/www/html/cgi/status.perl
/etc/httpd/conf/httpd.conf
ScriptAlias /cgi-bin/ “/var/www/html/cgi/”
Disadvantages of CGI scripts
Widely used once but CGI scripts have disadvantages
Web server must launch each script as a separate program
Potentially significant overheads
Process creation, context switches
Must be installed in a trusted directory
Only trusted system administrators can do this
Static and dynamic pages look different to end users
Printing extra tags makes scripts more difficult to write
Not only dynamic content, but also the HTML tags
15
Embedded Server-Side Scripts
“Module loading” mechanism in most web server
Allows interpreters for one or more scripting languages
Server side script can be embedded in “ordinary” web pages
Popular embedded server-side scripts
PHP
Visual Basic (on microsoft servers)
Ruby
Cold Fusion
Java (servlets)
16
PHP – embedded server-side script
PHP script – <?php scripts ?>
17
<HTML><BODY><P>
<?php
for ($i = 0; $i < 20; $i++) {
if ($i % 2) { ?>
<B><?php echo “ $i”; ?>
</B><?php
} else echo “ $i”;
}
?>
</P></BODY></HTML>
Disadvantages of Server-Side Scripts
Embedded server-side scripts can be slow
Communication across the Internet is still too slow for
interactive pages
Servers can be overloaded with many requests and server-
side scripts
18
Client-Side Scripts
Distribute the scripting loads to clients
Require interpreters on client’s machines
Web designers want their pages to be viewable by as wide
an audience as possible – result in a convergence in client-
side scripting languages
Pages intended for the general public almost always use
JavaScript for interactive features
19
Convergence in Languages
Diverse languages vs. Convergence in languages
Since embedded server-side scripts run on the web server
site, they can be written in many different languages – all the
client ever sees is standard HTML
Client side scripts require web browsers to have interpreter
to see the page – powerful incentive for convergence
20
JavaScript
Executed on the client to create dynamic pages
Microsoft developed JScript to support JavaScript, but introduced a
number of incompatibilities
21
Java Applets vs. Servlets
Java applets
Programs designed to run inside some other program
The term is most often used for Java programs that display
their output in (a portion of) a web page
To support the execution of applets, most modern browsers
contain a Java virtual machine
Java servlets
Java programs are executing in a special secure environment
Servlet-support web servers
Dynamically loaded to extend the functionality of the server
Alternative to CGI scripts for dynamic web content
22
Names and Scoping of Scripting Lang.
Data type Most do not require types of variables to be declared
Perl and JavaScript, permit optional declarations
With or without declarations, most scripting languages
use dynamic typing
The interpreter can perform type checking at run time, or
coerce values when appropriate
Nesting and scoping conventions vary quite a bit Some allow nested subroutines and blocks
Most use static scoping but Tcl uses dynamic scoping
Scopes of undeclared variables are determined with
different default scoping rules (e.g., Perl: global, PHP: local)
23
String Pattern Manipulation
String and Pattern Manipulation
Regular expressions are present in many scripting languages
Two main groups of RE implementation
REs defined in the POSIX standard
awk, egrep, regex (C standard library), older versions of
Tcl
Advance REs in the lead of Perl
Perl, PHP, Python, Ruby, JavaScript, Emacs Lisp, Java, C#,
recent versions of Tcl
24
High-level Data Types
Mappings – directories, hashes, associative arrays
awk first provides this associative arrays
Perl, Python, Ruby also provide hashes
Arrays vs. hashes
Perl examples
@colors = (“red”, “green”, “blue”);
%complements = (“red” => “cyan”, “green” => “magenta”,
“blue” => “yellow”);
print $colors[1], $complements{“blue”}; # green yellow
25