practical extraction and report language perl is a
play

Practical Extraction and Report Language Perl is a language of - PDF document

Practical Extraction and Report Language Perl is a language of getting your job done There is more than one way to do it Larry Wall VI, October 2006 Page 1 Perl Outline : Filehandles & File Tests Subroutines (functions)


  1. Practical Extraction and Report Language « Perl is a language of getting your job done » « There is more than one way to do it » Larry Wall VI, October 2006 Page 1 Perl Outline : Filehandles & File Tests Subroutines (functions) References passing arguments to functions nested data structure Packages/Modules Namespace/@INC VI, October 2006 Page 2

  2. Perl Filehandles A filehandle is the name in a Perl program for an I/O connection between your Perl process and the outside world. ( Good practice: use all uppercase letters in the name of your filehandle ) Perl special file handles There are three connections that always exist and are always "open" when your program starts: STDIN , STDOUT , and STDERR . Actually, these names are file handles . File handles are variables used to manipulate files . STDIN reads from standard input which is usually the keyboard in normal Perl script (or input from a Browser in a CGI script. Cgi-lib.pl reads from this automatically.) STDOUT ( standard output ) and STDERR ( standard error ) by default write to a console (or a browser in CGI). We have been using the STDOUT file handle without knowing it for every print() statement during Perl presentations. The print() function uses STDOUT as the default if no other file handle is specified. VI, October 2006 Page 3 Perl Filehandles How to get a value from the keyboard into a Perl program ? The simplest way is to use the line-input operator: <STDIN> Each time we use <STDIN> in a place where a scalar value is expected, Perl reads the next complete text line up to the first newline from the keyboard (unless you modified it). #!/usr/bin/perl print "Please enter your Lastname: "; my $lastname = <STDIN>; #<> Please enter your Lastname: Ioannidis chomp $lastname; print "Please enter your Firstname: "; Vassilios my $firstname = <STDIN>; #<> Please enter your Firstname: chomp $firstname; Hello Vassilios Ioannidis, I hope you like Perl programming ! print "Hello $firstname $lastname,\n I hope you like Perl programming !\n"; exit; VI, October 2006 Page 4

  3. Perl Filehandles vioannid$ cat listparticipants.csv "Barkow","Simon","ETHZ","8057","Mr." "Basle","Arnaud","University of Basel","4056","Dr. (Mr.)" "Blevins","Todd","FMI","4058","Mr." "Bodenhausen","Natacha","University of Lausanne","1015","Mrs." "Botta","Francesca","University of Fribourg","6601","Mrs." "Kerschgens","Jan","EPFL","1015","Mr." "Keusch","Jeremy","FMI","4058","Dr. (Mr.)" "Kutter","Claudia","FMI","4058","Mrs." "Livingstone","Magdalena","ETHZ","8057","Mrs." "Meury","Marcel","University of Basel","4056","Mr." "Moore","James","University of Basel","4056","Dr. (Mr.)" "Muller","Joachim","University of Bern","3012","Dr. (Mr.)" "Mungpakdee","Sutada","other","5008","Mrs." "Nipitwattanaphon","Mingkwan","University of Lausanne","CH - 1015","Mrs." "Padavattan","sivaraman","University of Basel","4056","Dr. (Mr.)" "Paul","Ralf","University of Basel","4056","Dr. (Mr.)" "Tobler","Kurt","University of Zurich","8057","Dr. (Mr.)" "Vanoaica","Liviu","EPFL","1066","Mr." "Vellore Palanivelu","Dinesh","University of Basel","4056","Dr. (Mr.)" "von Castelmur","Eleonore","University of Basel","4056","Mrs." "Wassmann","Paul","University of Basel","4056","Mr." "Yadetie","Fekadu","other","N-5008","Dr. (Mr.)" vioannid$ VI, October 2006 Page 5 Perl Filehandles The invocation argument: @ARGV #!/usr/bin/perl use strict; use warnings; open (FILE, "listparticipants.csv") or die "Error. Could not open the file !\n"; while (<FILE>) { if (m/^\"(.*)\",\"(.*)\",\"(.*)\",\"(.*)\",\"(.*)\"/) { print "Hello $5 $2 $1 from $4, $3 !\n"; } else {} vioannid$ ./argv.pl } Hello Mr. Simon Barkow from 8057, ETHZ ! Hello Dr. (Mr.) Arnaud Basle from 4056, University of Basel ! exit; Hello Mr. Todd Blevins from 4058, FMI ! . . . Hello Dr. (Mr.) Fekadu Yadetie from N-5008, other ! vioannid$ VI, October 2006 Page 6

  4. Perl Filehandles The invocation argument: @ARGV Technically, the diamond <> operator is not looking literally at the invocation argument. It works from the @ARGV array. This is a special array that is preset by Perl to be a list of the invocation arguments. When the program starts, @ARGV contains the list of invocation arguments. And can be handled as a just like any other array ! #!/usr/bin/perl vioannid$ ./argv3.pl listparticipants use strict; use warnings; print @ARGV; listparticipants my $nb_arg = @ARGV; my $argument = $ARGV[0]; print "\n$nb_arg\n"; 1 print "The invocation argument is: The invocation argument is: listparticipants $argument\n"; vioannid$ exit; VI, October 2006 Page 7 Perl Filehandles The invocation argument: @ARGV #!/usr/bin/perl use strict; use warnings; my $filename = $ARGV[0]; open (FILE, "$filename") or die "Error. Could not open the file $filename !\n"; while (<FILE>) { if (m/^\"(.*)\",\"(.*)\",\"(.*)\",\"(.*)\",\"(.*)\"/) { print "Hello $5 $2 $1 from $4, $3 !\n"; } else {} vioannid$ ./argv.pl listparticipants.csv } Hello Mr. Simon Barkow from 8057, ETHZ ! Hello Dr. (Mr.) Arnaud Basle from 4056, University of Basel ! exit; Hello Mr. Todd Blevins from 4058, FMI ! . . . Hello Dr. (Mr.) Fekadu Yadetie from N-5008, other ! vioannid$ VI, October 2006 Page 8

  5. Perl Filehandles You can open a file for input or output using the open() function. open(INFILE, "input.txt") or die "Can't open input.txt: $!"; open(OUTFILE, ">output.txt") or die "Can't open output.txt: $!"; open(LOGFILE, ">>logfile") or die "Can't open logfile: $!"; You can use your own naming instead of "INFILE", "OUTFILE" or "LOGFILE". When you're done with your filehandles, you should close() them (though Perl will clean up after you if you forget…): close INFILE; print() can also take an optional first argument specifying which filehandle to print to: print STDERR "This is your final warning\n"; print OUTFILE $record; print LOGFILE $logmessage; use whatever name you like BUT: STDIN, STDOUT, STDERR, ARGV ! VI, October 2006 Page 9 Perl Filehandles File test Meaning . . . -r File is readable by effective user/group. -w File is writable by effective user/group. if (-e $filename) { -x File is executable by effective user/group. #do something -o File is owned by effective user. } -R File is readable by real user/group. -W File is writable by real user/group. . . . -X File is executable by real user/group. -O File is owned by real user. -e File exists. File test Meaning -z File has zero size. -p File is a named pipe (FIFO). -s File has nonzero size (returns size). -S File is a socket. -f File is a plain file. -b File is a block special file. -d File is a directory. -c File is a character special file. -l File is a symbolic link. -t Filehandle is opened to a tty. -u File has setuser bit set. -g File has setgroup bit set. -k File has sticky bit set. -T File is a text file. -B File is a binary file (opposite of -T). -M Age of file (at startup) in days since modification. -A Age of file (at startup) in days since last access. -C Age of file (at startup) in days since inode change. VI, October 2006 Page 10

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend