Vous êtes sur la page 1sur 21

(References and File Handling)

PERL

AGENDA

References

File Operators
Reading From a File Writing into File

Appending the File

Page 1

1
REFERENCES

Page 2

References - Preface
Need for more complex data structures, than simple variables

and lists

Perls 3 built-in types combine with references, gives arbitrarily complex and powerful data structures A selection between an elegant program doing its job quickly and a program that consumes system resources voraciously

Page 3

What is a Reference?

A reference is a scalar value that points to a memory location of another variable in memory.

Reference 0x305108

Referent 0x351f00 5

$a

$b

Page 4

Creating a Reference
A reference of a variable can be got when a backslash is put in front of it.
$a = \$b; 0x305108 0x351f00 5

$a

SCALAR (0X351f00)

$b

The address in memory is a scalar, so it can be assigned to one

Page 5

Creating a Reference Continued...


This address can be used to alter information at that

location
$$a = 3;

0x305108

0x351f00 3

$a

SCALAR (0X351f00)

$b

print $$a $b; 33

Page 6

Where can it be used?


References are used to

create multidimensional arrays/structures pass complex data types to subroutines

Page 7

Accessing References
In order to access a reference, you have to tell Perl to access the information at the address instead of the address itself. An extra dollar sign ($) can be used for that.

e.g., $current_value = 5773; $ref_current_value = \$current_value; $new_value = 1387; $$ref_current_value = $new_value;

Page 8

References

Reference Assignment $refScalar = \$scalar;

How to Dereference ${$refScalar} is a scalar value.

$refArray = \@array;

@{$refArray} is an array value.

$refHash = \%hash;

%{$refHash} is a hash value.

$refFunction = \&function;

&{$refFunction} is a function location.

Page 9

2
File Handle

Page 10

File Test Operators


-d to check if it is a directory

-e
-f -M

for existence check


to check if it is a file returns the age of file/directory

-r
-s -w -x -z
Page 11

to check if it is readable
returns the size of the file to check if it is writable to check if it is executable to check if the size is zero

Example
$file=C:\\file1.txt; if (-e $file) { $size= -s $file; print $size , " bytes \n"; if (-z $file) { print" There is no data in $file \n"; } }

Page 12

FILE Handling
A file is opened using open function
To read
open(FILE_HANDLE, file-path\\filename) or warn Can not open filename: $!;

To create\overwrite
open(FILE_HANDLE, >file-path\\filename) or die Can not create filename: $!;

To append
open(FILE_HANDLE, >>$filename) or die Can not append $filename: $!;

To read and write


open(FILE_HANDLE, +>$filename) or die Can not read-write $filename: $!;

To close an open file


close FILE_HANDLE;

Page 13

FILE Handling Contd

Open a file
Syntax

open(<Filehandle>, <path>/<filename>);
Example

open(INFO, ">$file");

Page 14

FILE Handling Contd

Reading from a file


Syntax

open(INFO, ">$file"); # Open for output


Example:

open(F,"test.pl"); while(<F>) { print $_; } close F;


Page 15

FILE Handling Contd


Seek
Syntax

seek FILEHANDLE, POSITION, OPTION


Example

open FILE,"C:\\file1.txt" or die "oops: $!"; seek FILE,10,0; @line = <FILE>; print @line; close(FILE);

Page 16

FILE Handling Contd

Writing into a file


Syntax

open(INFO, >$file); # Open for input


Example

open FILE,>C:\\file1.txt" or die "oops: $!"; print FILE Hello world; close (FILE);

Page 17

Reading and Writing into a file Contd

Appending the data into a file


Syntax

open(INFO, ">>$file"); # Open for appending Example


open FILE,>>C:\\file1.txt" or die "oops: $!"; print FILE Hello world; close (FILE);

Page 18

FILE Handling Contd

Close file
Syntax

Close (<Filehandle>)
Example

open FILE,"C:\\file1.txt" or die "oops: $!"; close(FILE);

Page 19

Imagination
Page 20

Action

Joy

Vous aimerez peut-être aussi