newby has asked for the wisdom of the Perl Monks concerning the following question:

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
  • Comment on I try to write script which try to open a text file and read first line from it
  • Download Code

Replies are listed 'Best First'.
Re: I try to write script which try to open a text file and read first line from it
by moritz (Cardinal) on Mar 19, 2012 at 19:08 UTC

    The best approach is to learn enough Perl to do what you want. It's not hard, and perlintro should teach you enough to do it.

    If you are stuck, feel free to ask again here, presenting the code you wrote, and describe how it fails to do what you want.

Re: open file
by choroba (Cardinal) on Mar 21, 2012 at 21:45 UTC
    Is this some kind of a homework?

    Just the problems I notice for the first sight:

    If the behaviour of the program should be different for non-existence, you should test for the existence before trying to open the file. Also, the syntax of (-e $filename) doest not use the second parameter.

    Moreover, you are defining a sub inside the else part. It makes no sense; you should be calling it there instead. If you want your sub to understand the parameter 'pid.dat', you should not hardcode "myfile.txt" in it, but use shift or @_ to get it.

Re: I try to write script which try to open a text file and read first line from it
by JavaFan (Canon) on Mar 19, 2012 at 19:06 UTC
    What have you tried so far? Have you written any code? If so, how far did you get? Where did you get stuck? If you weren't able to write any code what so ever, what manual pages have you read? Which part didn't you understand?
Re: I try to write script which try to open a text file and read first line from it
by kennethk (Abbot) on Mar 19, 2012 at 19:09 UTC

    What have you tried? What didn't work? What are you utilizing as learning resources? Much of the necessary syntax for your task is discussed in Files and I/O (perlintro), perlopentut and I/O Operators (perlop).

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: open file
by Riales (Hermit) on Mar 21, 2012 at 21:49 UTC

    With regard to the different natures of errors that may be generated, here's a hint:

    When you do the following line in sub get_record, why do you think you die with Cannot open file: $!? What do you think $! is? Try searching for it in perldoc perlvar for more info!

Re: I try to write script which try to open a text file and read first line from it
by Anonymous Monk on Mar 19, 2012 at 19:10 UTC
    There's a mistake in the requirement. If the file does not exist, the opening already fails.

    use autodie qw(:all); use Errno qw(ENOENT); use Try::Tiny; try { open my $in_handle, '<', 'textfile'; my $first_line = readline $in_handle; } catch { if (ENOENT == $_->errno) { open my $out_handle, '>', 'statusfile'; print {$out_handle} ('0'); } };