scripting languages l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Scripting Languages PowerPoint Presentation
Download Presentation
Scripting Languages

Loading in 2 Seconds...

play fullscreen
1 / 20

Scripting Languages - PowerPoint PPT Presentation


  • 212 Views
  • Uploaded on

Scripting Languages. CS351 – Programming Paradigms. Common Characteristics of Scripting languages. Both Batch and Interactive use. Economy of Expression. Lack of declarations; simple scoping rules Flexible dynamic typing. Easy access to other programs. Sophisticated Pattern matching.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Scripting Languages' - sol


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
scripting languages

Scripting Languages

CS351 – Programming Paradigms

common characteristics of scripting languages
Common Characteristics of Scripting languages.
  • Both Batch and Interactive use.
  • Economy of Expression.
  • Lack of declarations; simple scoping rules
  • Flexible dynamic typing.
  • Easy access to other programs.
  • Sophisticated Pattern matching.
  • High-level data types.
1 batch and interactive use
1 – Batch and Interactive Use
  • Perl has a JIT compiler that reads the entire program before execution.
  • Other languages are quite happy to only execute a line as soon as it has been read.
  • Python, Tcl and Ruby will also accept commands from the keyboard.
2 economy of expression
2 – Economy of Expression
  • To support rapid development and interactive use, scripting languages require very little boilerplate code.
  • This can be illustrated very clearly by the following example:

public static void main(String args[])

{

System.out.println(“Hello!”);

}

print “Hello!\n”

  • Another example is reading a file word by word.
3 lack of declarations and simple scoping rules
3 – Lack of Declarations and simple scoping rules
  • Most scripting languages dispense with declarations.
  • They also use simple rules to govern the scopes of names ( identifiers ).
  • In Perl every name is global by default.
  • In Tcl and PHP, everything is local by default.
  • Python has a unique implementation in that any variable that is assigned a value is local to the block in which the assignment appears.
  • Special syntax is required to access the variable from a surrounding scope.
4 flexible dynamic typing
4 – Flexible Dynamic Typing
  • Most scripting languages are dynamically typed.
  • In PHP, Ruby and Python, the type of a variable is checked immediately prior to use.
  • In others such as Tcl and Perl, a variable can be interpreted differently in different contexts.
  • E.g.

$a = “4”;

print $a . 3 . “\n”;

print $a + 4 . “\n”;

5 easy access to other programs
5 – Easy access to other programs
  • Most programming languages provide a way to interact with the underlying commands of the OS.
  • However this can be quite convoluted, as anyone who has had to do work with the Runtime.exec command knows.
  • Scripting languages support these OS commands and their outputs much more cleanly and directly.
  • Perl provides 100 built-in commands for the OS while the os module in Python is an excellent library for executing commands.
6 pattern matching
6 – Pattern Matching
  • As scripting language ancestors were used for text processing, the support for pattern matching in text files is impressive.
  • Scripting languages have very good facilities for pattern matching, searching and string manipulation.
  • This is all based upon the notion of extended regular expressions.
  • sed is a very powerful scripting language for text processing.
  • What does sed '1!G;h;$!d' do?
7 high level data types
7 – High level data types
  • High level data types such as sets, bags, dictionaries, lists and tuples are some of the very convenient features provided by scripting languages.
  • In C++, it is possible to use ``operator overloading’’ to create advanced user-defined types but scripting languages go one step further by building high-level types into the syntax and semantics.
  • E.g. in Python a dictionary can map a key to a value:

dict([(x, x**2) for x in (2, 4, 6)])

{2: 4, 4: 16, 6: 36}

scripting languages evolution
Scripting Languages Evolution
  • Due to the dynamism of the open source community and the freedom from working within an ISO published standard, scripting languages have been at the recent forefront of programming language development.
  • Most scripting languages have had only a single person driving the initial development.
  • All of the most interesting features and powerful features appear in Python:
  • E.g. true iterators, array slices, multiway assignment, anonymous first-class functions and functional type lists.
problem domains for scripting languages
Problem Domains for Scripting Languages
  • Some languages such as Perl, Python and Ruby are intended for general purpose use as they support features such as modules, separate compilation, reflection etc.
  • However a lot of scripting languages are intended for use within a well defined domain.
  • We will look in detail at the following:
    • Shell Languages
    • Text Processing
    • Glue Languages
shell languages
Shell Languages
  • In the early 1970’s a command language for automating the control of Unix programs was written.
  • This was known as ``shell’’ or sh.
  • This was extended to allow for variables and control flow and was known as bash. This is the default Unix command shell.
  • Shell languages allow for many operations including manipulating file names, arguments and commands and for tying other programs together.
shell languages cont
Shell Languages cont…
  • Some features provided by a shell languages.
      • List all file ending in .txt -> ls *.txt
  • This feature is known as filename expansion or globbing.
  • There are other alternatives to this command:
      • ls fig?.eps
      • ls fig[0-9].eps
      • ls fig3.{eps,pdf}
  • What do each of these commands do?
shell languages cont14
Shell Languages cont…
  • Such simple commands do not even hint at the flexibility provided by shell languages.
  • It is possible to provide loops within shell languages.
  • These can be very useful.
  • E.g.

for file in *.eps

do

ps2pdf $file

done

  • What is going on here?
shell languages cont15
Shell Languages cont…
  • We can also use conditionals within our bash scripts:

for file in *.eps

do

if [ ! test –e ${file%.eps}.pdf ]

then

ps2pdf $file

fi

done

  • The test command has many features, type man test to get a list of them all.
shell languages cont16
Shell Languages cont…
  • It is possible to pass the results of one program as the input to another very easily through the use of pipes.

find . –name *.txt | ls | wc –l

  • What does this do?
  • It is is also possible to send output to and from files using re-directs.

ls *.txt > files

for file in *.txt; do echo $file >> files; done

  • What do these commands do?
shell languages cont17
Shell Languages cont…
  • There are some simple rules to bear in mind when using shell scripts.

foo=bar

single=‘$foo’

double=“$foo”

echo $single $double

  • What is printed?
  • When using other programs in a shell script, they must be placed within backticks.

bname=`basename $file .txt`

text processing
Text Processing
  • Shell languages are heavily string oriented.
  • Commands are strings parsed into single words.
  • All variables are string-valued and there are elaborate quoting conventions.
  • However shell languages cannot be used like a text editor.
  • How can we account for interactive features such as insertion, deletion, replacement, bracket-matching etc.
  • Through the use of special ``online’’ editors such as sed and awk.
slide19
sed
  • Stands for stream editor.
  • Very powerful and can be used to write complex scripts but is more commonly used for one-line programs.
  • E.g.

sed –e ‘/^[[:space:]]*$/d’

  • awk is an attempt to modify sed to look more like a programming language.
glue languages
Glue Languages
  • These general purpose scripting languages inherit a rich set of features form both the shell languages and the text processing languages.
  • Imagine writing a script to kill a named process in *nix.
  • We could do it ( hack it ) via a bash script or we could use a general purpose language to do it.
  • We will discuss Python in detail on Thursday.