Thank you Martin and everyone else.

On Oct 18, 2011, at 5:36 PM, Martin A. Brown wrote:

> 
> You are asking perl to open a file called:
> 
>  /tmp/home/ubuntu/bin/test.count
> 
> When, in fact, I guess that you would like to create the file:
> 
>  /tmp/test.count
> 

Of course, you are correct. I didn't see it, but it made perfect sense when you 
hit me with the clue sledge. 

> A few notes and recommendations:
> 
> 0. I would suggest renaming the script from 'test' to
>    'test-count-something'; there is a shell builtin called 'test' 
>    and often a binary /usr/bin/test, as well.

That's not the real name of the script. But noted.

> 1. To solve the fully qualified path issue from above, use 'basename'
>    from the perl module File::Basename:
> 
>    use File::Basename;
>    sub writetemp {
>        my $fname = "/tmp/". basename($0) . ".count";
>        open (NEWFILE, ">$fname") or die $!;
>        print NEWFILE "$product{name}.value $product{value} $date";
>        close (NEWFILE);
>    }

Done, and it works as expected.

> 2. If you are going to write a file with one program and read the 
>    file with another program, get in the habit of creating the file
>    atomically.  Imagine what happens when your data file creation 
>    program is still writing a the data file when along comes the 
>    data consuming program and starts to read it!  Hilarity.
> 
>    How do you do prevent this problem?  Take advantage of 
>    filesystem atomicity.  In perl, you can use the perl function 
>    rename:
> 
>    use File::Basename;
>    sub writetemp {
>        my $fname = "/tmp/". basename($0) . ".count";
>        open (NEWFILE, ">${fname}.tmp") or die $!;
>        print NEWFILE "$product{name}.value $product{value} $date";
>        close (NEWFILE);
>        rename($fname . ".tmp", $fname) or die $!;
>    }

Excellent suggestion. I will implement this and the other suggestions. I'm a 
novice when it comes to perl. I can usually cobble something together, but I 
learn by example all the time.

> Wow, you are still reading.

Yes. I did read your whole message. :)

Thank you and everyone else for the help

Russell Johnson
[email protected]



_______________________________________________
PLUG mailing list
[email protected]
http://lists.pdxlinux.org/mailman/listinfo/plug

Reply via email to