Hello PERLanians. I am a very new in perl and try to learn it.
So I need advise in the following task:
I try to write script which try to open a text file and read first line from it. If reading has fails because the file was not present, the script have to create a file and write value of "0" in that file. If error generated has different nature (e.g. not enough access privileges) script shall not try to create a file (as it already exists; we simply do not have permission).
I can not understand the last part of the task (If error generated has different nature (e.g. not enough access privileges) script shall not try to create a file (as it already exists; we simply do not have permission).
So far I have this code:
#!/usr/bin/perl -w
#use strict;
open (FF, "myfile.txt") ;
if(!-e FF, "myfile.txt")
{
open FF, "> myfile.txt";
print FF "0\n";
close FF;
}
else
{
sub get_record
{
open (FF, "myfile.txt") or die "Cannot open file: $! ";
chomp(my $record = <FF>);
close FF;
return $record;
}
}
$text = &get_record('pid.dat');
print "text = $text\n";
Any suggestions are welcome,
thank you
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.