10/22/08 What are scripting languages? • Unix shells: sh, ksh, bash Scripting Languages – job control • Perl – Slashdot, bioinformatics, financial data processing, cgi • Python – System administration at Google – BitTorrent file sharing system Kathleen Fisher • Ruby – Various blogs, data processing applications • PHP – Yahoo web site • JavaScript “The glue that holds – Google maps the web together” Characteristics Design philosophy Often people, especially computer engineers, focus on • Interpreted (no compilation step) the machines. They think, "By doing this, the • Dynamically typed machine will run faster. By doing this, the machine will run more effectively. By doing this, the machine • High-level model of underlying machine will something something something." They are focusing on the machines. But in fact we need to • Garbage collected focus on humans, on how humans care about doing programming or operating the application of the • Don’t have to declare variables machines. We are the masters. They are the slaves . Yukihiro “Matz” Matsumoto Creator of Ruby Designed to support “quick programming” #!/sw/bin/ruby require 'uri'; require 'net/http' Demo: Getting the homework uri= URI.parse(ARGV[0]) h=Net::HTTP.new(uri.host,80) • What if I don’t want to go to the web site resp,data = h.get(uri.path) to see if I have cs242 homework? hwk = {} • Write a script to check for me! if resp.message == "OK" data.scan(/Homework (\d*) \(due (\d*)\/(\d*)\)/)\ {|x,y,z| hwk[x] = Time.local(2005,y,z)} end > hwk http://www.stanford.edu/class/cs242/handouts/index.html hwk.each{| assignment, duedate| Hwk 1 was due on Wednesday, October 05. if duedate < (Time.now - 60 * 60 * 24) Hwk 2 was due on Wednesday, October 12. puts "Hwk #{assignment} was due on #{duedate.strftime("%A, %B %d")}." Hwk 3 is due on Wednesday, October 19. else puts "Hwk #{assignment} is due on #{duedate.strftime("%A, %B %d")}." end } 1
10/22/08 #!/sw/bin/ruby #!/sw/bin/ruby require 'uri'; require 'net/http' require 'uri'; require 'net/http' “Shebang” Many useful uri= URI.parse(ARGV[0]) uri = URI.parse(ARGV[0]) libraries h=Net::HTTP.new(uri.host,80) h = Net::HTTP.new(uri.host,80) resp,data = h.get(uri.path) resp,data = h.get(uri.path) hwk = {} hwk = {} if resp.message == "OK" if resp.message == "OK" data.scan(/Homework (\d*) \(due (\d*)\/(\d*)\)/)\ data.scan(/Homework (\d*) \(due (\d*)\/(\d*)\)/)\ {|x,y,z| hwk[x] = Time.local(2005,y,z)} {|x,y,z| hwk[x] = Time.local(2005,y,z)} end end hwk.each{| assignment, duedate| hwk.each{| assignment, duedate| if duedate < (Time.now - 60 * 60 * 24) if duedate < (Time.now - 60 * 60 * 24) puts "Hwk #{assignment} was due on #{duedate.strftime("%A, %B %d")}." puts "Hwk #{assignment} was due on #{duedate.strftime("%A, %B %d")}." else else puts "Hwk #{assignment} is due on #{duedate.strftime("%A, %B %d")}." puts "Hwk #{assignment} is due on #{duedate.strftime("%A, %B %d")}." end end } } #!/sw/bin/ruby #!/sw/bin/ruby require 'uri'; require 'net/http' require 'uri'; require 'net/http' Powerful regular Associative uri= URI.parse(ARGV[0]) uri= URI.parse(ARGV[0]) arrays expression support h=Net::HTTP.new(uri.host,80) h=Net::HTTP.new(uri.host,80) resp,data = h.get(uri.path) resp,data = h.get(uri.path) hwk = {} hwk = {} if resp.message == "OK" if resp.message == "OK" data.scan(/Homework (\d*) \(due (\d*)\/(\d*)\)/)\ data.scan(/Homework (\d*) \(due (\d*)\/(\d*)\)/)\ {|x,y,z| hwk[x] = Time.local(2005,y,z)} {|x,y,z| hwk[x] = Time.local(2005,y,z) } end end hwk.each{| assignment, duedate| hwk.each{| assignment, duedate| if duedate < (Time.now - 60 * 60 * 24) if duedate < (Time.now - 60 * 60 * 24) puts "Hwk #{assignment} was due on #{duedate.strftime("%A, %B %d")}." puts "Hwk #{assignment} was due on #{duedate.strftime("%A, %B %d")}." else else puts "Hwk #{assignment} is due on #{duedate.strftime("%A, %B %d")}." puts "Hwk #{assignment} is due on #{duedate.strftime("%A, %B %d")}." end end } } #!/sw/bin/ruby require 'uri'; require 'net/http' Shebang uri= URI.parse(ARGV[0]) h=Net::HTTP.new(uri.host,80) • In Unix systems, shebang tells the O/S how to evaluate an executable text file. resp,data = h.get(uri.path) String processing hwk = {} #! interp-path if resp.message == "OK" doit: data.scan(/Homework (\d*) \(due (\d*)\/(\d*)\)/)\ prog-text {|x,y,z| hwk[x] = Time.local(2005,y,z)} > interp-path doit args end > ./doit args hwk.each{| assignment, duedate| if duedate < (Time.now - 60 * 60 * 24) puts "Hwk #{assignment} was due on #{duedate.strftime("%A, %B %d")}." else • Advantages: Don’t need file extensions, puts "Hwk #{assignment} is due on #{duedate.strftime("%A, %B %d")}." program looks built-in, and can change end implementation transparently. } 2
10/22/08 Large standard library Contributing users • Ruby Application Archive (RAA) • Date, ParseDate • File, Tempfile – http://raa.ruby-lang.org/ • GetoptLong: processing command line switches – 144 library categories, 833 libraries available • profile: automatic performance profiling – eg: URI library, database access • Pstore: automatic persistence • Comprehensive Perl Archive Network (CPAN) • BasicSocket, IPSocket, TCPSocket, TCPServer, UDPSocket, – http://www.cpan.org/ Socket • Net::FTP, Net::HTTP, Net::HTTPResponse, Net::POPMail, – 8853 Perl modules from 4655 authors Net::SMTP, Net::Telnet – “With Perl, you usually don’t have to write much • CGI: cookies, session mngt. code: just find the code that somebody else has already written to solve your problem.” Example: URI and HTTP libs Example: URI and HTTP libs require 'uri'; require 'net/http' require 'uri'; require 'net/http' Require clauses URI.parse converts uri = URI.parse(ARGV[0]) uri = URI.parse(ARGV[0]) cause Ruby to load argument string into a h = Net::HTTP.new(uri.host,80) h = Net::HTTP.new(uri.host,80) named libraries. uri object, with host resp,data = h.get(uri.path) resp,data = h.get(uri.path) and path components (among other things). Example: URI and HTTP libs Example: URI and HTTP libs require 'uri'; require 'net/http' require 'uri'; require 'net/http' Net::HTTP.new creates h.get asks to retrieve uri = URI.parse(ARGV[0]) uri = URI.parse(ARGV[0]) an http connection the headers and h = Net::HTTP.new(uri.host,80) h = Net::HTTP.new(uri.host,80) object, ready to content of the given resp,data = h.get(uri.path) converse with the resp,data = h.get(uri.path) path from the site specified host on the associated with h. It indicated port. returns a pair of the response code and the payload data. 3
10/22/08 Strings Powerful regular expressions • Strings are just objects: • Regular expressions are patterns that match against strings, possibly creating bindings in “hermione”.length yields 8 the process. Uses greedy matching. • Strings can include expressions with # operator: • In Ruby, regular expressions are objects created with special literal forms: “3 + 4 = #{3+4}” yields “3 + 4 = 7” /reg-exp/ or %r{reg-exp} • Plus operator concatenates strings: “Hermione” + “ Granger” yields “Hermione Granger” • Examples: • Many more operations (more than 75!). /arr/ matches strings containing arr /\s*\|\s*/ matches a | with optional white space Simple matches Compound matches All characters except .|()[\^{+$*? match themselves re* Matches 0 or more occurrences of re. re+ Matches 1 or more occurrences of re. .|()[\^{+$*? Precede by \ to match directly re{m,n} Matches at least m and no more than . Matches any character n occurrences of re. [characters] Matches any single character in […] re? Matches zero or one occurrence of re. May include ranges; Initial ^ negates re1 | re2 Matches either re1 or re2 \d Matches any digit (…) Groups regular expressions and \w Matches any “word” character directs interpretor to introduce bindings \s Matches any whitespace for intermediate results. ^ Matches the beginning of a line $ Matches the end of a line Introducing bindings Using regular expressions Matching a string against a regular expression We can use these bindings to write functions to display the results of a match: causes interpretor to introduce bindings: def showre(str,regexp) def showone(str,regexp) $` Portion of string that preceded match. if str =~ regexp if str =~ regexp "#{$`}--->#{$&}<---#{$'}" "#{$1}" $& Portion of string that matched. else else "match failed" "match failed" $’ Portion of string after match. end end end end $1, $2, Portion of match within i th set of … parentheses. showre(“hello”, /l+/) yields “he--->ll<---o” showone(“hello”, /(l+)/) yields “ll” 4
Recommend
More recommend