in reply to I try to write script which try to open a text file and read first line from it

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.