Programming in Perl File and directory handling Peter Verhás January 2002.

Slides:



Advertisements
Similar presentations
Debugging What can debuggers do? Run programs Make the program stops on specified places or on specified conditions Give information about current variables’
Advertisements

1 PHP Storing and Retrieving Data. string fgets(resource handle [, int length]) Reads and returns one line from a file; moves file pointer to next line.
COMP234 Perl Printing Special Quotes File Handling.
Perl I/O Learning Objectives: 1. To understand how to perform input from standard Input & how to process the input 2. To understand how to perform input.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
Perl File I/O Learning Objectives: 1. To understand the usage of file handler in Perl 2. To learn the file error handling / testing in Perl.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
17-Jun-15 Assorted Ruby Details. The command line irb starts an interactive Ruby interpreter ruby starts Ruby, with input from the command line End with.
1 File Systems Chapter Files 6.2 Directories 6.3 File system implementation 6.4 Example file systems.
Perl I/O Software Tools. Lecture 15 / Slide 2 Input from STDIN Reading from STDIN is easy, and we have done it many times. $a = ; In a scalar context,
Perl File I/O Software Tools. Slide 2 Filehandles l A filehandle is the name for an I/O connection between your Perl program and the outside world. STDIN.
I/O while ($line= ){ #remove new line char \n chomp($line); if($line eq “quit”){ exit(1); } while ( ){ #remove new line char \n chomp($_); if($_ eq “quit”){
Unix Shell Scripts. What are scripts ? Text files in certain format that are run by another program Examples: –Perl –Javascript –Shell scripts (we learn.
File/Directory manipulation. Opening a File To read from a file, must use a file handle. –by convention, all caps open a file (for reading): –open FILE,
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Introduction to Programming the WWW I CMSC Summer 2004 Lecture 12.
Standard Input and Output. Overview Data communication with a C program and the outside world is performed through files Files are a non-volatile way.
Introduction to Programming the WWW I CMSC Summer 2004 Lecture 11.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
Chapter 11 File Systems and Directories. 2 File Systems File: A named collection of related data. File system: The logical view that an operating system.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
Programming in Perl regular expressions and m,s operators Peter Verhás January 2002.
Matching in list context (Chapter 11 = ($str =~ /pattern/); This stores the list of the special ($1, $2,…) capturing variables into the.
I/O and Redirection. Standard I/O u Standard Output (stdout) –default place to which programs write u Standard Input (stdin) –default place from which.
1 Perl Syntax: control structures Learning Perl, Schwartz.
Introduction to Unix – CS 21
1 Chapter 7 – Object-Oriented Programming and File Handling spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information.
Programming in Perl predefined variables Peter Verhás January 2002.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Storing and Retrieving Data
1 Introduction to Perl CIS*2450 Advanced Programming Techniques.
Introduction to Programming the WWW I CMSC Winter 2003.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Iteration While / until/ for loop. While/ Do-while loops Iteration continues until condition is false: 3 important points to remember: 1.Initialise condition.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
Lecture 8a: File I/O BJ Furman 21MAR2011. Learning Objectives Explain what is meant by a data stream Explain the concept of a ‘file’ Open and close files.
Python – reading and writing files. ??? ???two ways to open a file open and file ??? How to write to relative and absolute paths?
Introduction to Perl Part II By: Bridget Thomson McInnes 22 January 2004.
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
16. Python Files I/O Printing to the Screen: The simplest way to produce output is using the print statement where you can pass zero or more expressions,
A Few More Functions. One more quoting operator qw// Takes a space separated sequence of words, and returns a list of single-quoted words. –no interpolation.
Files Tutor: You will need ….
Object Serialization. Sequential-access Text Files Sequential-access files store records In order by the record-key field Java imposes no structure on.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
תוכנה 1 תרגול מס ' 4 שימוש במחלקות קיימות : קלט / פלט (IO)
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
CSC 4630 Meeting 17 March 21, Exam/Quiz Schedule Due to ice, travel, research and other commitments that we all have: –Quiz 2, scheduled for Monday.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
CPTG286K Programming - Perl Chapter 9: Misc. Control Structures Chapter 10: Filehandles & File tests.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
Perl Subroutines User Input Perl on linux Forks and Pipes.
Flock in PHP By Chao Liang. Basic Flock Concept What is flock? Abbreviation for file locking Why flock? Data consistency. Prevent file corruption. Different.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 10: Files.
CIRC Summer School 2016 Baowei Liu
The OS Module (windows and unix)
Input from STDIN STDIN, standard input, comes from the keyboard.
CIRC Summer School 2017 Baowei Liu
CIRC Winter Boot Camp 2017 Baowei Liu
Programming in Perl Introduction
Perl Variables: Array Web Programming.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
بسمه تعالی کارگاه ارزشیابی پیشرفت تحصیلی
Programming in Perl Tutorial Introduction
Perl I/O Learning Objectives:
Regular Expressions and Grep
Module 12 Input and Output
CSC 352– Unix Programming, Fall 2012
CIT 383: Administrative Scripting
Presentation transcript:

Programming in Perl File and directory handling Peter Verhás January 2002.

File handling open(handle,”filename”) close handle read a record from file print handle expressionlist read, write, seek, truncate, flock, binmode See all these in detail.

Opening and closing a file open(F,”f.txt”) to read open(F,”>f.txt”) to write a new file open(F,”>>f.txt”) to append open(F,”+<f.txt”) read/write open(F,”+>f.txt”) read/write but first truncate Return value is non-zero or undef in case of error. close F closes the file

binmode There are no text files. There are no binary files. The handling of a file can be binary or text. The two conventions: –Windows \r\n –UNIX \n Using binmode is safe and portable.

Reading record(s) from file $/ specifies the record separator, \n by default reads a line (bytes until the next record = gives all the lines $/=undef; $file= ; reads the whole file

Reading in a loop open(F,"test.pl"); while( ){ print $_; } close F; By default it reads into $_ Returns undef when end of file

Printing to a file print HANDLE expression list –print STDERR ”error output” –print STDOUT ”just output” –print ”to STDOUT default” –print F ”a line into a file\n”

Getting rid of the new-line character open(F,"test.pl"); while( ){ chomp; print ”$_\n”; } close F; reads the whole line including the new line at the end of the line. chomp chops it off safely.

truncate, seek, flock $LOCK_SH = 1; $LOCK_EX = 2; $LOCK_NB = 4; $LOCK_UN = 8; open(F,"+ counter.txt"); flock F,$LOCK_EX; seek F,0,0;# seek to the start of the file $counter = ; $counter++;# change here to -- seek F,0,0;# seek to the start of the file again truncate F,0;# comment this out when decrementing print F $counter; flock F,$LOCK_UN; close F; print $counter; Shared lock, Exclusive lock Non-blocking lock Unlock Try go up to 10 and change $counter-- with #truncate commented out. You will see: 10, 9, 89

Handling directories opendir(D,”dirname”); readdir(D); closedir D; You can not open a directory for writing

Example: getting the list of the files = readdir(D); closedir = (); for ){ $_ if -f $_; } print "$_\n"; }

Example: getting all files recursively $StartDirectory = '.'; = readdir(D); = grep( (-d "$StartDirectory/$_") && ){ $_ = "$StartDirectory/$_" } while( $#dirs > -1 ){ $cdir = = readdir(D); = grep( (-d "$StartDirectory/$cdir/$_") && ){ "$cdir/$_"; } ){ "$StartDirectory/$cdir/$_"; }

Thank you for your kind attention.