
#allocate a new variable, $fd, as a file descriptor and then attenpt to
# open the file.  
if (open(my $fd,">","aTestFile.dat")) { #open in write/create mode (>)
    #if the open succeeds execute with the locally scoped $fd
    print $fd "this is the first line\n"; #there is $fd, print there
    #Note the file descriptor, $fd, without a comma before the list 
    # of what to print.  This instructs Perl to print to that file, 
    # not the console
    close($fd);
}
else {
    die "Failed to create the file, $!";
    #the special variable $! contains the error message from the last 
    # OS call. In this case, since we got here, we know that the open 
    # failed.  So, $!, will have the message from the OS explaining
    # why the open failed
}

if (open(my $fd,">>","aTestFile")) { #re-open in append mode (>>)
    print $fd "and a line appended\n";
    close($fd);
}
else {
    die "Failed to open the file, for append, $!";
}

if (open(my $fd,"<","aTestFile")) { #open in read mode (<)
    while (my $lin = <$fd>) { #get all lines, returns false at end
        print "From file: $lin";
    }
}
else {
    die "Failed to open the file, for read, $!";
}
