Saying "it doesn't work" is not very helpful. Post any error messages or warnings the script outputs or describe what the script does do.
In your case I see one problem right away:
CODE
my $file = <STDIN>;
$file will end with an input terminator, probably a newline, so it will not be able to open a file named "file.txt\n" which your die statment should have caught and echoed back an error. You need to use chomp() on your input:
CODE
chomp(my $file = <STDIN>);
from now on, start all your perl script with these two pragmas:
CODE
use strict;
use warnings;
and you can add this one for good measure:
CODE
use diagnostics;
The first two are very important, especially for new perl coders. The third one dplays very verbose messages and is sometimes more useful than the brief messages that perl outputs by default.
If your perl resource material did not mention the need to chomp() input, I would stop using it.
Besides all that, your script looks like it should work once the chomp() is added.
Kevin