Something a bit more simple than what I usually do, but I thought it was a cool little mechanic that some people might not know about.
The version that I made is easily tile-able. An optional daytime/night time sensor can also be added so that the fence automatically closes during the night.