IO Streams, Files and Directories

44
Perl Programming Perl Programming Course Course Krassimir Berov IO, Streams, Files and Directories IO, Streams, Files and Directories I-can.eu

description

This is the sixth set of slightly updated slides from a Perl programming course that I held some years ago. I want to share it with everyone looking for intransitive Perl-knowledge. A table of content for all presentations can be found at i-can.eu. The source code for the examples and the presentations in ODP format are on https://github.com/kberov/PerlProgrammingCourse

Transcript of IO Streams, Files and Directories

Page 1: IO Streams, Files and Directories

Perl Programming Perl Programming CourseCourse

Krassimir Berov

IO, Streams, Files and DirectoriesIO, Streams, Files and Directories

I-can.eu

Page 2: IO Streams, Files and Directories

ContentsContents

1.1. Input and output of scalar dataInput and output of scalar data

2.2. FilehandlesFilehandles

3.3. STDIN, STDOUT and STDERRSTDIN, STDOUT and STDERR

4.4. Input and output of complex dataInput and output of complex data

5.5. Opening, closing, reading and writing filesOpening, closing, reading and writing files

6.6. File manipulationsFile manipulations

7.7. Directory browsing and manipulations Directory browsing and manipulations

8.8. File-test operatorsFile-test operators

Page 3: IO Streams, Files and Directories

IO of scalar dataIO of scalar data

• Line-input operator (Diamond Operator)Line-input operator (Diamond Operator)<> == <STDIN><> == <STDIN>

• NOTE: tries first ARGV then STDINNOTE: tries first ARGV then STDIN

print 'Tell me something';print 'Tell me something';my $line_input = <>;my $line_input = <>;if ($line_input eq $/){if ($line_input eq $/){ print 'You just pressed "Enter"'print 'You just pressed "Enter"'}else {}else { chomp $line_input;chomp $line_input; print 'You wrote:"' .$line_input print 'You wrote:"' .$line_input .'" and pressed "Enter".';.'" and pressed "Enter".';}}

Page 4: IO Streams, Files and Directories

IO and scalar dataIO and scalar data

• Line-input operatorLine-input operator<> == <STDIN><> == <STDIN>11

• print, printf, sprintf, sayprint, printf, sprintf, say22

$SIG{'INT'} = sub {print 'Oh you tried to kill me. '};$SIG{'INT'} = sub {print 'Oh you tried to kill me. '};

print 'Go on, enter something';print 'Go on, enter something';while (my $line_input = <STDIN>){while (my $line_input = <STDIN>){ chomp $line_input;chomp $line_input; print 'You wrote:"' .$line_input print 'You wrote:"' .$line_input .'" and pressed "Enter".';.'" and pressed "Enter".';}}

(2)

1. ...well, actually first ARGV is consulted2. Appeared in Perl 5.10

Page 5: IO Streams, Files and Directories

IO and scalar dataIO and scalar data

• print, printf, sprintf, sayprint, printf, sprintf, say#line_input2.pl#line_input2.pl$SIG{'INT'} = sub {print 'Oh you tried to kill me. '};$SIG{'INT'} = sub {print 'Oh you tried to kill me. '};

print 'Go on, enter something';print 'Go on, enter something';while (my $input = <STDIN>){while (my $input = <STDIN>){ chomp $input;chomp $input; print 'Go on, enter something' and next unless $input;print 'Go on, enter something' and next unless $input; printf( 'You wrote:"%s" and pressed "Enter"'.$/, $input);printf( 'You wrote:"%s" and pressed "Enter"'.$/, $input); if($input =~ /\d+/){if($input =~ /\d+/){ $input =~ s/[^\d]+//g;$input =~ s/[^\d]+//g; print sprintf('%d looks like number.', $input);print sprintf('%d looks like number.', $input); print 'Enter some string';print 'Enter some string'; }else {}else { printf $/printf $/ .'"%s",Goood. now enter some number:', $input;.'"%s",Goood. now enter some number:', $input; } } }}

(3)

Page 6: IO Streams, Files and Directories

IO and scalar dataIO and scalar data

• print FILEHANDLE LISTprint FILEHANDLE LISTprint LISTprint LISTprintprint

• Prints a string or a list of stringsPrints a string or a list of strings

• Returns true if successfulReturns true if successful

• FILEHANDLE may be a scalar variable nameFILEHANDLE may be a scalar variable name

• If LIST is also omitted, prints $_ to the If LIST is also omitted, prints $_ to the currently selected output channelcurrently selected output channel

• To set the default output channel to To set the default output channel to something other than STDOUT use the something other than STDOUT use the selectselect operation operation

Page 7: IO Streams, Files and Directories

IO and scalar dataIO and scalar data

• sprintf FORMAT, LISTsprintf FORMAT, LIST

• Returns a string formatted as described in Returns a string formatted as described in FORMATFORMAT

• You can also use an explicit format You can also use an explicit format parameter index, such as 1$, 2$.parameter index, such as 1$, 2$.

• Perl does its own sprintf formattingPerl does its own sprintf formatting

• it emulates the C function sprintf, but it it emulates the C function sprintf, but it doesn't use it doesn't use it

• Exception: standart formatters for floating-Exception: standart formatters for floating-point numberspoint numbers

print sprintf("%2$02d.%1$02d",45,12); #12.45print sprintf("%2$02d.%1$02d",45,12); #12.45

Page 8: IO Streams, Files and Directories

IO and scalar dataIO and scalar data

• sprintf FORMAT, LISTsprintf FORMAT, LIST

• permits the following universally-known permits the following universally-known conversions:conversions:

• See perlfunc/sprintf for moreSee perlfunc/sprintf for more

%% a percent sign%% a percent sign%c a character with the given number%c a character with the given number%s a string%s a string%d a signed integer, in decimal%d a signed integer, in decimal%u an unsigned integer, in decimal%u an unsigned integer, in decimal%o an unsigned integer, in octal%o an unsigned integer, in octal%x an unsigned integer, in hexadecimal%x an unsigned integer, in hexadecimal%e a floating-point number, in scientific notation%e a floating-point number, in scientific notation%f a floating-point number, in fixed decimal notation%f a floating-point number, in fixed decimal notation%g a floating-point number, in %e or %f notation%g a floating-point number, in %e or %f notation

(2)

Page 9: IO Streams, Files and Directories

IO and scalar dataIO and scalar data

• printf FILEHANDLE FORMAT, LISTprintf FILEHANDLE FORMAT, LISTprintf FORMAT, LISTprintf FORMAT, LIST

• Equivalent to Equivalent to print FILEHANDLE sprintf(FORMAT, LIST)print FILEHANDLE sprintf(FORMAT, LIST), , except that $\except that $\11 is not appended is not appended

• DO NOT use a printf when a simple print DO NOT use a printf when a simple print would dowould do

1. the output record separator

Page 10: IO Streams, Files and Directories

IO and scalar dataIO and scalar data

• say FILEHANDLE LISTsay FILEHANDLE LISTsay LISTsay LISTsaysay

• Perl 5.10.0Perl 5.10.0

• Implicitly appends a newline. Implicitly appends a newline.

• say LIST == { local $\ = "\n"; print LIST } say LIST == { local $\ = "\n"; print LIST }

• not enabled by defaultnot enabled by default

Page 11: IO Streams, Files and Directories

FilehandlesFilehandles

• What is a Filehandle?What is a Filehandle?

• The name of a The name of a connectionconnection between a perl between a perl program and the outside worldprogram and the outside world

• Filehandles are named like other Perl Filehandles are named like other Perl identifiersidentifiers

• Filehandles do not have a sigil.Filehandles do not have a sigil.

• use all uppercase letters in the name of use all uppercase letters in the name of your filehandle to avoid collisions with your filehandle to avoid collisions with future reserved wordsfuture reserved words

• or use Indirect Filehandles (SCALARS)or use Indirect Filehandles (SCALARS)

Page 12: IO Streams, Files and Directories

FilehandlesFilehandles

• There are SIX special filehandlesThere are SIX special filehandles

• STDIN, STDOUT, STDERR, DATA, ARGV, STDIN, STDOUT, STDERR, DATA, ARGV, and ARGVOUTand ARGVOUT

• DO NOT use these names for your own DO NOT use these names for your own filehandlesfilehandles

perl -e'$\=$/; \perl -e'$\=$/; \print shift @ARGV while @ARGV' arg1 arg2 arg3print shift @ARGV while @ARGV' arg1 arg2 arg3

Page 13: IO Streams, Files and Directories

STDIN, STDOUT and STDERRSTDIN, STDOUT and STDERR

• STDIN – STDIN – standard input streamstandard input stream

• The connection between your program The connection between your program and its inputand its input

• Generally the keyboardGenerally the keyboard

• May also be a file or the output of another May also be a file or the output of another program trough a pipeprogram trough a pipe

perl -e'print while <>'perl -e'print while <>'perl -e'print while <>' |ls #unixperl -e'print while <>' |ls #unixperl -e"print while <>" |dir #windowsperl -e"print while <>" |dir #windows

perl -e'print while <>' <somefileperl -e'print while <>' <somefileperl -e'print while <STDIN>'perl -e'print while <STDIN>'

Page 14: IO Streams, Files and Directories

STDIN, STDOUT and STDERRSTDIN, STDOUT and STDERR

• STDOUT – STDOUT – standard output streamstandard output stream

• The screen by defaultThe screen by default

• Can be redirected to a file or another Can be redirected to a file or another programprogram

perl -e'print STDOUT while <STDIN>'perl -e'print STDOUT while <STDIN>'perl -e'print while <>'perl -e'print while <>'

Page 15: IO Streams, Files and Directories

STDIN, STDOUT and STDERRSTDIN, STDOUT and STDERR

• STDERR – standard error streamSTDERR – standard error stream

• The screen by defaultThe screen by default

• Can be redirected to a file or another Can be redirected to a file or another programprogram

Page 16: IO Streams, Files and Directories

STDIN, STDOUT and STDERRSTDIN, STDOUT and STDERR

• Example:Example:$SIG{'INT'} = sub {warn 'Oh you tried to kill me. '};$SIG{'INT'} = sub {warn 'Oh you tried to kill me. '};print STDERR 'Go on, enter something';print STDERR 'Go on, enter something';while (my $input = <STDIN>){while (my $input = <STDIN>){ chomp $input;chomp $input; print STDERR 'Go on, enter something' and next unless print STDERR 'Go on, enter something' and next unless $input;$input; printf STDOUT ( 'You wrote:"%s" and pressed "Enter"'.$/, printf STDOUT ( 'You wrote:"%s" and pressed "Enter"'.$/, $input);$input); if($input =~ /\d+/){if($input =~ /\d+/){ $input =~ s/[^\d]+//g;$input =~ s/[^\d]+//g; print STDOUT sprintf('%d looks like number.', print STDOUT sprintf('%d looks like number.', $input);$input); print STDERR 'Enter some string';print STDERR 'Enter some string'; }else {}else { printf STDOUT $/printf STDOUT $/ .'"%s",Goood.', $input;.'"%s",Goood.', $input; print STDERR 'Now enter some number:'print STDERR 'Now enter some number:' } } }}

Page 17: IO Streams, Files and Directories

IO of complex dataIO of complex data

• Parsing arguments to your programParsing arguments to your program

• ARGV – filehandleARGV – filehandle• iterates over command-line filenames in @ARGViterates over command-line filenames in @ARGV

• Usually written as the null filehandle in the Usually written as the null filehandle in the diamond operator <>diamond operator <>

• $ARGV - contains the name of the current $ARGV - contains the name of the current file when reading from <>file when reading from <>

• @ARGV command-line arguments @ARGV command-line arguments intended for the scriptintended for the script

• use Getopt::Long;use Getopt::Long;

Page 18: IO Streams, Files and Directories

IO of complex dataIO of complex data

• Parsing argumentsParsing arguments

• Example:Example:#sum.pl#sum.plmy $sum = 0;my $sum = 0;for(@ARGV){for(@ARGV){ s/[\D]+//g; #sanitize inputs/[\D]+//g; #sanitize input $_ ||= 0; #be sure we have a number$_ ||= 0; #be sure we have a number

print 'adding '. $_ . ' to ' . $sum if $sum;print 'adding '. $_ . ' to ' . $sum if $sum; $sum += $_;$sum += $_; print 'Result: ' . $sum;print 'Result: ' . $sum;}}

Page 19: IO Streams, Files and Directories

IO of complex dataIO of complex data

• Parsing arguments (Advanced)Parsing arguments (Advanced)

• Getopt::Long - Extended processing of Getopt::Long - Extended processing of command line optionscommand line options• parses the options from the global array @ARGVparses the options from the global array @ARGV

• can be used to parse options from an arbitrary can be used to parse options from an arbitrary stringstring

• thread safe when using ithreads as of Perl 5.8thread safe when using ithreads as of Perl 5.8

• encourages the use of Pod::Usage to produce encourages the use of Pod::Usage to produce help messageshelp messages

• can be used in an object oriented waycan be used in an object oriented way

Page 20: IO Streams, Files and Directories

IO of complex dataIO of complex data

• Parsing arguments (Advanced)Parsing arguments (Advanced)

• Example:Example:

#calc.pl#calc.pluse Getopt::Long;use Getopt::Long;use Pod::Usage;use Pod::Usage;use Data::Dumper;use Data::Dumper;our %opts;our %opts;GetOptions (\%opts, GetOptions (\%opts, 'action|do=s','action|do=s', 'params|p=s@','params|p=s@', 'verbose','verbose', 'help|?' 'help|?' ););

print Dumper(\%opts) if $opts{verbose};print Dumper(\%opts) if $opts{verbose};#...#...

Page 21: IO Streams, Files and Directories

IO of complex dataIO of complex data

• Parsing arguments (Advanced)Parsing arguments (Advanced)

• Example:Example:

#calc.pl continued#calc.pl continuedpod2usage(2) if $opts{help};pod2usage(2) if $opts{help};

if($opts{action} eq 'sum') {if($opts{action} eq 'sum') { sum()sum()}}elsif ($opts{action} eq 'subtract') {elsif ($opts{action} eq 'subtract') { subtract(); subtract(); }}else {else { print 'The action ' . $opts{action} print 'The action ' . $opts{action} .' is not implemented.';.' is not implemented.';}}

(2)

Page 22: IO Streams, Files and Directories

Opening, closing, reading Opening, closing, reading and writing filesand writing files

• open FILEHANDLE,EXPRopen FILEHANDLE,EXPRopen FILEHANDLE,MODE,EXPRopen FILEHANDLE,MODE,EXPR

• Opens the file whose filename is given by Opens the file whose filename is given by EXPR, and associates it with FILEHANDLE.EXPR, and associates it with FILEHANDLE.

• See also: sysopen, POSIX::openSee also: sysopen, POSIX::openopen(FILE,'<','/etc/sudoers') open(FILE,'<','/etc/sudoers') or die 'Can\'t open sudoers for reading. '.$!;or die 'Can\'t open sudoers for reading. '.$!;open(FILE,'>','/etc/sudoers') open(FILE,'>','/etc/sudoers') or die 'Can\'t open sudoers for wriring. '.$!;or die 'Can\'t open sudoers for wriring. '.$!;open(FILE,'>>','/etc/sudoers') open(FILE,'>>','/etc/sudoers') or die 'Can\'t open sudoers for appending. '.$!;or die 'Can\'t open sudoers for appending. '.$!;

Page 23: IO Streams, Files and Directories

Opening, closing, reading Opening, closing, reading and writing filesand writing files

• close FILEHANDLEclose FILEHANDLEcloseclose

• Closes the file or pipe associated with the file Closes the file or pipe associated with the file handle, handle,

• flushes the IO buffers, flushes the IO buffers,

• closes the system file descriptor. closes the system file descriptor.

• Returns true on success and if no error was Returns true on success and if no error was reported by any PerlIO layer. reported by any PerlIO layer.

• Closes the currently selected filehandle if the Closes the currently selected filehandle if the argument is omitted.argument is omitted.

Page 24: IO Streams, Files and Directories

Opening, closing, reading Opening, closing, reading and writing filesand writing files

• Reading and writingReading and writing

my $FH;my $FH;my $file = 'test.txt';my $file = 'test.txt';if (open($FH,'<', $file) ) {if (open($FH,'<', $file) ) { local $/; # slurp modelocal $/; # slurp mode my $text = <$FH>;my $text = <$FH>; print "Content of $file:\n$text";print "Content of $file:\n$text";}}else {else { open($FH,'>', $file);#create itopen($FH,'>', $file);#create it print $FH 'Hello!';print $FH 'Hello!';}}close $FHclose $FH

Page 25: IO Streams, Files and Directories

Opening, closing, reading Opening, closing, reading and writing filesand writing files

• Reading and writingReading and writing

my $FH;my $FH;my $file = 'test.txt';my $file = 'test.txt';if (open($FH,'<', $file) ) {if (open($FH,'<', $file) ) { my @lines = <$FH>;my @lines = <$FH>; print "Content of $file:\n";print "Content of $file:\n"; print $_ foreach (@lines);print $_ foreach (@lines);}}else {else { print 'Creating '. $file.$/;print 'Creating '. $file.$/; open($FH,'>', $file);open($FH,'>', $file); print $FH $_.':Hello!'.$/ for (1..4);print $FH $_.':Hello!'.$/ for (1..4);}}close $FH;close $FH;

(2)

Page 26: IO Streams, Files and Directories

Opening, closing, reading Opening, closing, reading and writing filesand writing files

• Reading and writing – OO wayReading and writing – OO way

use IO::File;use IO::File;my $file = 'test.txt';my $file = 'test.txt';my $fh = IO::File->new("< $file");my $fh = IO::File->new("< $file");my @lines;my @lines;$fh->binmode;#see binmode in perlfunc$fh->binmode;#see binmode in perlfunc@lines = $fh->getlines;@lines = $fh->getlines;print "Content of $file:\n";print "Content of $file:\n";print $_ foreach (@lines);print $_ foreach (@lines);$fh->close;$fh->close;

(3)

Page 27: IO Streams, Files and Directories

File manipulationsFile manipulations

• unlinkunlink

• renamerename

• movemove

Page 28: IO Streams, Files and Directories

File manipulationsFile manipulations

• unlink LISTunlink LISTunlinkunlink

• Deletes a list of files. Deletes a list of files. Returns the number of files successfully Returns the number of files successfully deleted.deleted.

unlink qw /test.txt errors.txt file.txt/unlink qw /test.txt errors.txt file.txt/

Page 29: IO Streams, Files and Directories

File manipulationsFile manipulations

• rename OLDNAME,NEWNAMErename OLDNAME,NEWNAME

• Changes the name of a file; Changes the name of a file; an existing file NEWNAME will be clobbered. an existing file NEWNAME will be clobbered. Returns true for success, false otherwise.Returns true for success, false otherwise.

• can be used as can be used as movemove implementation implementation

• See also See also FFile::Copyile::Copy

Page 30: IO Streams, Files and Directories

File manipulationsFile manipulations

• movemove

• File::CopyFile::Copy

• provides two basic functions, provides two basic functions, copycopy and and movemove

• See perldoc File::CopySee perldoc File::Copyuse File::Copy "cp";#alias for 'copy'use File::Copy "cp";#alias for 'copy'cp("file.txt","file2.txt") or die "Copy failed: $!";cp("file.txt","file2.txt") or die "Copy failed: $!";

Page 31: IO Streams, Files and Directories

Directory browsing Directory browsing and manipulationsand manipulations

• opendir/closediropendir/closedir

• readdir/rewinddirreaddir/rewinddir

• mkdir/rmdirmkdir/rmdir

• IO::DirIO::Dir

• File::PathFile::Path

Page 32: IO Streams, Files and Directories

Directory browsing Directory browsing and manipulationsand manipulations

• opendir DIRHANDLE,EXPRopendir DIRHANDLE,EXPR

• Opens a directory named EXPR for processing Opens a directory named EXPR for processing by readdir, telldir, seekdir, rewinddir, and by readdir, telldir, seekdir, rewinddir, and closedir. closedir.

• Returns true if successful.Returns true if successful.

• NOTE: DIRHANDLEs have their own namespace NOTE: DIRHANDLEs have their own namespace separate from FILEHANDLEsseparate from FILEHANDLEs

• closedirclosedir

• Closes a directory opened by opendir and Closes a directory opened by opendir and returns the success of that system callreturns the success of that system call

Page 33: IO Streams, Files and Directories

Directory browsing Directory browsing and manipulationsand manipulations

• opendir DIRHANDLE,EXPRopendir DIRHANDLE,EXPR

• Opens a directory named EXPR for processing Opens a directory named EXPR for processing by readdir, telldir, seekdir, rewinddir, and by readdir, telldir, seekdir, rewinddir, and closedir. closedir.

• Returns true if successful.Returns true if successful.

• NOTE: DIRHANDLEs have their own namespace NOTE: DIRHANDLEs have their own namespace separate from FILEHANDLEsseparate from FILEHANDLEs

• closedirclosedir

• Closes a directory opened by opendir and Closes a directory opened by opendir and returns the success of that system callreturns the success of that system call

Page 34: IO Streams, Files and Directories

Directory browsing Directory browsing and manipulationsand manipulations

• readdir DIRHANDLEreaddir DIRHANDLE

• Returns the next directory entry for a directory Returns the next directory entry for a directory opened by opendir. opened by opendir.

• In list context, returns all the rest of the entries In list context, returns all the rest of the entries in the directory. in the directory.

• If there are no more entries, returns an undefined If there are no more entries, returns an undefined value in scalar context or a null list in list value in scalar context or a null list in list context.context.

• rewinddir DIRHANDLErewinddir DIRHANDLE

• Sets the current position to the beginning of the Sets the current position to the beginning of the directory for the readdir routine on DIRHANDLEdirectory for the readdir routine on DIRHANDLE

Page 35: IO Streams, Files and Directories

Directory browsing Directory browsing and manipulationsand manipulations

• mkdir FILENAME,MASKmkdir FILENAME,MASKmkdir FILENAMEmkdir FILENAMEmkdirmkdir

• Creates the directory specified by FILENAME, Creates the directory specified by FILENAME, with permissions specified by MASK with permissions specified by MASK (as modified by umask).(as modified by umask).

• On success returns true, otherwise returns false On success returns true, otherwise returns false and sets $! (errno). and sets $! (errno).

• If omitted, MASK defaults to 0777. If omitted, MASK defaults to 0777.

• If omitted, FILENAME defaults to $_. If omitted, FILENAME defaults to $_.

Page 36: IO Streams, Files and Directories

Directory browsing Directory browsing and manipulationsand manipulations

• rmdir FILENAMErmdir FILENAMErmdir rmdir

• Deletes the directory specified by Deletes the directory specified by FILENAME if that directory is empty.FILENAME if that directory is empty.

• On success returns true, otherwise On success returns true, otherwise returns false and sets $! (errno). returns false and sets $! (errno).

• If FILENAME is omitted, uses $_.If FILENAME is omitted, uses $_.

Page 37: IO Streams, Files and Directories

Directory browsing Directory browsing and manipulationsand manipulations

• File::Path and IO::DirFile::Path and IO::Dir

• File::Path – Create or remove directory File::Path – Create or remove directory treestrees

• provides provides mkpathmkpath and and rmtreermtree to create, or to create, or remove, respectively whole directory pathsremove, respectively whole directory paths

• IO::Dir – supply object methods for IO::Dir – supply object methods for directory handlesdirectory handles

• just wrappers around perl's built in just wrappers around perl's built in directory reading routinesdirectory reading routines

Page 38: IO Streams, Files and Directories

Directory browsing Directory browsing and manipulationsand manipulations

• Example:Example:

#see 06_io/directories.pl#see 06_io/directories.pl

Page 39: IO Streams, Files and Directories

File-testsFile-tests

• -X FILEHANDLE -X FILEHANDLE -X EXPR-X EXPR-X DIRHANDLE-X DIRHANDLE-X -X

• A file test, where X is one of the letters listed below.A file test, where X is one of the letters listed below.

• tests the associated file to see if something is true tests the associated file to see if something is true about itabout it

• If the argument is omitted, tests $_, except for -t, If the argument is omitted, tests $_, except for -t, which tests STDINwhich tests STDIN

• returns 1 for true and '' for false, or the undefined returns 1 for true and '' for false, or the undefined value if the file doesn't exist.value if the file doesn't exist.

Page 40: IO Streams, Files and Directories

File-testsFile-tests

• -X can be any of:-X can be any of:

-r File is readable by effective uid/gid.-r File is readable by effective uid/gid.-w File is writable by effective uid/gid.-w File is writable by effective uid/gid.-x File is executable by effective uid/gid.-x File is executable by effective uid/gid.-o File is owned by effective uid.-o File is owned by effective uid.

-R File is readable by real uid/gid.-R File is readable by real uid/gid.-W File is writable by real uid/gid.-W File is writable by real uid/gid.-X File is executable by real uid/gid.-X File is executable by real uid/gid.-O File is owned by real uid.-O File is owned by real uid.

-e File exists.-e File exists.-z File has zero size (is empty).-z File has zero size (is empty).-s File has nonzero size (returns size in bytes).-s File has nonzero size (returns size in bytes).

Page 41: IO Streams, Files and Directories

File-testsFile-tests

• -X can be any of:-X can be any of:-f File is a plain file.-f File is a plain file.-d File is a directory.-d File is a directory.-l File is a symbolic link.-l File is a symbolic link.-p File is a named pipe (FIFO), or Filehandle is a pipe.-p File is a named pipe (FIFO), or Filehandle is a pipe.-S File is a socket.-S File is a socket.-b File is a block special file.-b File is a block special file.-c File is a character special file.-c File is a character special file.-t Filehandle is opened to a tty.-t Filehandle is opened to a tty.

-u File has setuid bit set.-u File has setuid bit set.-g File has setgid bit set.-g File has setgid bit set.-k File has sticky bit set.-k File has sticky bit set.

-T File is an ASCII text file (heuristic guess).-T File is an ASCII text file (heuristic guess).-B File is a "binary" file (opposite of -T).-B File is a "binary" file (opposite of -T).

-M Script start time minus file modification time, in days.-M Script start time minus file modification time, in days.-A Same for access time.-A Same for access time.-C Same for inode change time (Unix, may differ for other -C Same for inode change time (Unix, may differ for other platforms)platforms)

(2)

Page 42: IO Streams, Files and Directories

PerlIOPerlIO

• The notion of “text” changed during last The notion of “text” changed during last yearsyears

• 1 byte is not always 1 character1 byte is not always 1 character

• PerlIO is in the COREPerlIO is in the CORE

• On the fly Charset convertionOn the fly Charset convertion

• transparenttransparent

• efficientefficient

Page 43: IO Streams, Files and Directories

PerlIOPerlIO

• See: perluniintro, perlunifaq, perlunicode, perlunitutSee: perluniintro, perlunifaq, perlunicode, perlunitut

use utf8;#comment this lineuse utf8;#comment this lineif ($^O =~/win32/i) {if ($^O =~/win32/i) { require Win32::Locale;require Win32::Locale; binmode(STDOUT, ":encoding(cp866)")binmode(STDOUT, ":encoding(cp866)") if(Win32::Locale::get_language() eq 'bg')if(Win32::Locale::get_language() eq 'bg')}}else{#assume some unixelse{#assume some unix binmode(STDOUT, ':utf8') if $ENV{LANG} =~/UTF-8/;binmode(STDOUT, ':utf8') if $ENV{LANG} =~/UTF-8/;}}my ($малки, $големи) = ("BOB\n", "боб$/");my ($малки, $големи) = ("BOB\n", "боб$/");print lc $малки, uc($големи) ;print lc $малки, uc($големи) ;print chomp($малки, $големи) ;print chomp($малки, $големи) ;print length $малки,'|',length $големи ;print length $малки,'|',length $големи ;print $малки, $големи ;print $малки, $големи ;

(2)

Page 44: IO Streams, Files and Directories

IO, Streams, IO, Streams, Files and DirectoriesFiles and Directories

Questions?Questions?