Perl Training References File Handling

download Perl Training References File Handling

of 21

Transcript of Perl Training References File Handling

  • 8/13/2019 Perl Training References File Handling

    1/21

    PERL(References and File Handling)

  • 8/13/2019 Perl Training References File Handling

    2/21

    Page 1

    AGENDA

    References File Operators

    Reading From a File

    Writing into FileAppending the File

  • 8/13/2019 Perl Training References File Handling

    3/21

  • 8/13/2019 Perl Training References File Handling

    4/21

    Page 3

    References - Preface

    Need for more complex data structures, than simple variablesand 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 quicklyand a program that consumes system resources voraciously

  • 8/13/2019 Perl Training References File Handling

    5/21

    Page 4

    What is a Reference?

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

    $b$a 5

    0x305108 0x351f00

    Reference Referent

  • 8/13/2019 Perl Training References File Handling

    6/21

    Page 5

    Creating a Reference

    $b$a

    SCALAR

    (0X351f00) 5

    0x305108 0x351f00

    $a = \$b;

    A reference of a variable can be got when a backslash is put infront of it.

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

  • 8/13/2019 Perl Training References File Handling

    7/21

    Page 6

    Creating a Reference Continued...

    This address can be used to alter information at thatlocation

    $b$aSCALAR

    (0X351f00)3

    0x305108 0x351f00

    $$a = 3;

    print $$a $b;

    3 3

  • 8/13/2019 Perl Training References File Handling

    8/21

    Page 7

    Where can it be used?

    References are used to

    create multidimensional arrays/structures

    pass complex data types to subroutines

  • 8/13/2019 Perl Training References File Handling

    9/21

  • 8/13/2019 Perl Training References File Handling

    10/21

    Page 9

    References

    Reference Assignment How to Dereference

    $refScalar = \$scalar; ${$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.

  • 8/13/2019 Perl Training References File Handling

    11/21

    Page 10

    2

    File Handle

  • 8/13/2019 Perl Training References File Handling

    12/21

  • 8/13/2019 Perl Training References File Handling

    13/21

    Page 12

    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";

    }

    }

  • 8/13/2019 Perl Training References File Handling

    14/21

    Page 13

    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;

  • 8/13/2019 Perl Training References File Handling

    15/21

  • 8/13/2019 Perl Training References File Handling

    16/21

    Page 15

    FILE Handling Contd

    Reading from a fileSyntax

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

    Example:

    open(F,"test.pl");

    while()

    {print $_;

    }

    close F;

  • 8/13/2019 Perl Training References File Handling

    17/21

    Page 16

    FILE Handling Contd

    SeekSyntax

    seek FILEHANDLE, POSITION, OPTION

    Exampleopen FILE,"C:\\file1.txt" or die "oops: $!";

    seek FILE,10,0;

    @line = ;

    print @line;close(FILE);

  • 8/13/2019 Perl Training References File Handling

    18/21

    Page 17

    FILE Handling Contd

    Writing into a fileSyntax

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

    Example

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

    print FILE Hello world;

    close (FILE);

  • 8/13/2019 Perl Training References File Handling

    19/21

    Page 18

    Reading and Writing into a file Contd

    Appending the data into a fileSyntax

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

    Exampleopen FILE,>>C:\\file1.txt" or die "oops: $!";

    print FILE Hello world;close (FILE);

  • 8/13/2019 Perl Training References File Handling

    20/21

    Page 19

    FILE Handling Contd

    Close fileSyntax

    Close ()

    Example

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

    close(FILE);

  • 8/13/2019 Perl Training References File Handling

    21/21

    Page 20

    Imagination Action Joy