CSCI 4152/6509 Natural Language Processing Lab 1: FCS Computing Environment, Perl Tutorial 1 Lab Instructor: Dijana Kosmajac, Tukai Pain Faculty of Computer Science Dalhousie University 15/17-Jan-2020 (1) CSCI 4152/6509 1
Lab Overview • An objective: Make sure that all students are familiar with their CSID and how to login to the bluenose server • Refresh your memory about Unix-like Command-Line Interface • Introduction to Perl • Note 1: Replace CSID with your CSID (Dalhousie CS id, which is different from your Dalhousie id) • Note 2: If you do not know your CSID, you can look it up and check its status at: https://www.cs.dal.ca/csid 15/17-Jan-2020 (1) CSCI 4152/6509 2
Step 1: Logging in to server bluenose • You can choose Windows, Mac or Linux environment in some labs • Windows: you will use PuTTY program • On Mac: open a Terminal and type: ssh CSID @bluenose.cs.dal.ca (instead of CSID use your CS userid ) • On Linux: similarly to Mac, you open the terminal and type the same command: ssh CSID @bluenose.cs.dal.ca 15/17-Jan-2020 (1) CSCI 4152/6509 3
Running PuTTY • If you use Windows, then one option is to use PuTTY to login to the bluenose server • Double-click the PuTTY icon, and the following window should appear: 15/17-Jan-2020 (1) CSCI 4152/6509 4
15/17-Jan-2020 (1) CSCI 4152/6509 5
Review of Some Linux Commands Step 2: pwd man pwd Step 3: mkdir csci4152 or mkdir csci6509 ls chmod go-rx csci6509 or chmod go-rx csci4152 Step 4: cd csci6509 or cd csci4152 • Make directory lab1 and change your current directory to it. 15/17-Jan-2020 (1) CSCI 4152/6509 6
Step 5: Using emacs prepare hello.pl Use an editor: emacs or some other (e.g., vi , pico . . . ) #!/usr/bin/perl print "Hello world!\n"; Step 6: Running a Perl program perl hello.pl Another way to run the program: chmod u+x hello.pl ./hello.pl 15/17-Jan-2020 (1) CSCI 4152/6509 7
Perl Tutorial • Over next couple of labs we will go over a basic Perl tutorial • Learn basics of Perl programming language • You already wrote and ran a simple Perl program hello.pl 15/17-Jan-2020 (1) CSCI 4152/6509 8
Finding Help • Web: perl.com , CPAN.org , perlmonks.org , . . . • man perl , man perlintro , . . . • books: the “Camel” book: “Learning Perl, 4th Edition” by Brian D. Foy; Tom Phoenix; Randal L. Schwartz (2005) Available on-line on Safari at Dalhousie 15/17-Jan-2020 (1) CSCI 4152/6509 9
Step 7: Basic Interaction with Perl • You can check the Perl version on bluenosee by running ‘ perl -v ’ command: perl -v This is perl 5, version 16, subversion 3 (v5.16.3)... • If you use the official Perl documentation from perl.com documentation site, choose the right version. • Test your assignment programs on bluenose if you developed them somewhere else. 15/17-Jan-2020 (1) CSCI 4152/6509 10
Executing Command from Command-Line • You can execute Perl commands directly from the command line • Example, type: perl -e ’print "hello world\n"’ • and the output should be: hello world • A more common way is to write programs in a file 15/17-Jan-2020 (1) CSCI 4152/6509 11
Write Program in a File • The program hello.pl should be already in the directory • Run the program using: perl hello.pl • You can also run it directly • First, make it executable: chmod u+x hello.pl and then you can run it using: ./hello.pl • Submit the program hello.pl using submit-nlp 15/17-Jan-2020 (1) CSCI 4152/6509 12
Direct Interaction with an Interpreter • Not common to use, but available • Command: perl -d -e 1 • Enter Perl statements, for example: print "hello\n"; print 12*12; • Enter ‘ q ’ to exit debugger • To learn more about debugger: command ‘ q ’ • Or, from the command line: man perldebug 15/17-Jan-2020 (1) CSCI 4152/6509 13
Syntactic Elements of Perl • statements separated by semi-colon ‘ ; ’ • white space does not matter except in strings • line comments begin with ‘ # ’; e.g. # a comment until the end of line • variable names start with $, @, or %: $a — a scalar variable @a — an array variable %a — an associative array (or hash) However: $a[5] is 5th element of an array @a , and $a{5} is a value associated with key 5 in hash %a • the starting special symbol is followed either by a name (e.g., $varname ) or a non-letter symbol (e.g., $! ) • user-defined subroutines are usually prefixed with &: &a — call the subroutine a (procedure, function) 15/17-Jan-2020 (1) CSCI 4152/6509 14
Step 8: Example Program 2 • Enter the following program as example2.pl : #!/usr/bin/perl use warnings; print "What is your name? "; $name = <>; chomp $name; print "Hello $name!\n"; • ‘ use warnings; ’ enables warnings — recommended! • chomp removes the trailing newline from $name if there is one. However, changing the special variable $/ will change the behaviour of chomp too. • Test example2.pl and you will need to submit it later 15/17-Jan-2020 (1) CSCI 4152/6509 15
Example 3: Declaring Variables The declaration “ use strict; ” is useful to force more strict verification of the code. If it is used in the previous program, Perl will complain about variable $name not being declared, so you can declare it with: ‘ my $name ’ We can call this program example3.pl : #!/usr/bin/perl use warnings; use strict; my $name; print "What is your name? "; $name = <>; chomp $name; print "Hello $name!\n"; 15/17-Jan-2020 (1) CSCI 4152/6509 16
Example 4: Declare a variable and assign its value in the same line #!/usr/bin/perl use warnings; use strict; print "What is your name? "; my $name = <>; chomp $name; print "Hello $name!\n"; 15/17-Jan-2020 (1) CSCI 4152/6509 17
Step 9: Example 5: Copy standard input to standard output We can call this program example5.pl #!/usr/bin/perl use warnings; use strict; while (my $line = <>) { print $line; } The operator <> reads a line from standard input, or—if the Perl script is called with filenames as arguments—from the files given as arguments. 15/17-Jan-2020 (1) CSCI 4152/6509 18
Try different ways of running this program: • Reading from standard input, which by default is the keyboard: ./example5.pl In this case the program will read the lines introduced from the keyboard until it receives the Ctrl-D combination of keys, which ends the input. • Reading the content of files, whose names are given as arguments of the script Create two simple text documents a.txt b.txt with a few arbitrary lines each (you can use a text editor to do that). Then run the Perl script with the names of these files are arguments: ./example5.pl a.txt b.txt Submit: Submit the program ‘ example5.pl ’ using: ˜vlado/public/submit-nlp 15/17-Jan-2020 (1) CSCI 4152/6509 19
Example 6: Default variable Special variable $_ is the default variable for many commands, including print and expression while (<>) , so another version of the program example5.pl would be: #!/usr/bin/perl while (<>) { print } This is equivalent to: #!/usr/bin/perl while ($_ = <>) { print $_ } Even shorter version of the program would be: #!/usr/bin/perl -p 15/17-Jan-2020 (1) CSCI 4152/6509 20
Variables • no need to declare them unless “ use strict; ” is in place • use strict; is a good practice for larger projects • variable type is not declared (it is inferred from context) • the main variable types: 1. Scalars – numbers (integers and floating-point) – strings – references (pointers) 2. Arrays of scalars 3. Hashes (associative arrays) of scalars 15/17-Jan-2020 (1) CSCI 4152/6509 21
Single-Quoted String Literals print ’hello\n’; # produces ’hello\n’ print ’It is 5 o\’clock!’; # ’ has to be escaped print q(another way of ’single-quoting’); # no need to escape this time print q< and another way >; print q{ and another way }; print q[ and another way ]; print q- and another way with almost arbitrary character (e.g. not q)-; print ’A multi line string (embedded new-line characters)’; print <<’EOT’; Some lines of text and more $a @b EOT 15/17-Jan-2020 (1) CSCI 4152/6509 22
Double-Quoted String Literals print "Backslash combinations are interpreted in double-quoted strings.\n"; print "newline after this\n"; $a = ’are’; print "variables $a interpolated in double-quoted strings\n"; # produces "variables are interpolated" etc. @a = (’arrays’, ’too’); print "and @a\n"; # produces "and arrays too" and a newline print qq{Similarly to single-quoted, this is also a double-quoted string, (etc.)}; 15/17-Jan-2020 (1) CSCI 4152/6509 23
Scalar Variables • name starts with $ followed by: 1. a letter and a sequence of letters, digits or underscores, or 2. a special character such as punctuation or digit • contains a single scalar value such as a number, string, or reference (a pointer) • do not need to worry whether a number is actually a number or string representation of a number $a = 5.5; $b = " $a "; print $a+$b; (11) 15/17-Jan-2020 (1) CSCI 4152/6509 24
Recommend
More recommend