Monday, March 24, 2014

Perl: Reading from file, read, eof

Perl: Reading from file, read, eof


  • read
  • eof
In Perl we usually care about lines of input so the above is enough. Still some like to read files with chunks of arbitrary length. read puts the read string to the variable passed to the function and returns the number of characters actually read READ_LENGTH = read FILEHANDLE,SCALAR,LENGTH

examples/files/read_file.pl
 
#!/usr/bin/perl
use strict;
use warnings;

# reading in 30 characters:

open my $in, "<", $0 or die $!;
my $expected = 30;
my $buf;
my $actual = read $in, $buf, $expected;
if ($actual < $expected) {
    print "reached end of file\n";
}

# returns TRUE if we are stand at or after the end of file.
eof($in)
 
--source: Edu Maven 

Perl: tell, seek

Perl: tell, seek


  • tell
  • seek
For our purposes a file is a line of characters. After a bunch of read and/or write operations we need to tell where are we on that line ?
 LOCATION = tell FILEHANDLE
 
We might also want to move within that file
 seek FILEHANDLE, OFFSET, WHENCE
 
 WHENCE:
     0 from beginning of file
     1 from current location
     2 from end of file
 OFFSET: 
     +/- number of bytes to move
 
the important values are:
seek $fh, 0,0;    # go to the beginning of the file
seek $fh, 0,2;    # go to the end of the file
 
 
--source: Edu Maven

Perl: truncate

Perl: truncate


# Sometimes you need to 
truncate FILEHANDLE, LENGTH;
 
examples/files/truncate.pl
 
#!/usr/bin/perl
use strict;
use warnings;

my $new = $ARGV[0];

my $filename = "file.txt";
open my $fh, "+<", $filename or die "Could not open $!\n";
my $old = <$fh>;

seek $fh, 0, 0;              # move to the beginning of the file 
print $fh $new;
truncate $fh, length $new;   # cut the file to the new size 
 
 
--Thanks: Edu Maven 

Wednesday, March 12, 2014

Compare values - examples

Perl: Compare values - examples


Expression Value
"12.0" == 12 TRUE
"12.0" eq 12 FALSE
2 < 3 TRUE
2 lt 3 TRUE
12 > 3 TRUE
12 gt 3 FALSE !
"foo" == "" TRUE ! (Warning)
"foo" eq "" FALSE
"foo" == "bar" TRUE ! (Warning)
"foo" eq "bar" FALSE

When reading from STDIN you can always expect a string

examples/scalars/is_empty_string.pl
 
#!/usr/bin/perl
use strict;
use warnings;

my $input = <STDIN>;
chomp $input;
 
if ($input == "") {     # wrong! use eq
    # empty string 
 
 
--Thanks: Edu Maven 

undef, the initial value and defined

Perl: undef, the initial value and defined


$q = $x + 1;        # is 1 + warning as the default number is 0
$w = $y . "abc";    # is 'abc' + warning as the default string is ""
$z++;               # is 1 - no warning
 
You can check if the variable already has a value or if it still undef:

if (not defined $x) {   # to avoid warning
     $x = 0;
}
 
 $x = $x + 1;
 
 
--Thanks: Edu Maven 

Boolean values: TRUE and FALSE

Perl: Boolean values: TRUE and FALSE


if ($z) {
    # $z is true
}
 
The FALSE values:

undef
""
0  0.0  00000  0e+10
"0"

All other values, such as the following are TRUE:

1
"00"
"0\n"
In many cases the separation must be between "real" values and undefined values. For that you can use the defined function:

if (defined $x) {
    # $x is defined (not undef)
}
 
 
---Thanks: EDU Maven