Your code works fine for me on Solaris if I open the file for reading
rather than writing (i.e. use "< $file") assuming the file already exists of course. The Solaris man page I have access to seems to indicate you must open the file for reading to use shared locks. You may want to explore opening the file for read and write if you need both kinds of locks.
In other words, try something like "+< $file"
or use sysopen() and it's ilk instead.