If the object moves through the line, pull it back to a position along the normal of the line (x=y y=-x or x=-y y=x If I remember this correctly, which one is negated depends on the direction in which you are moving the object)...
You only have to pull it back along this vector enough for it to touch the line...
After doing so you might also want to change its direction vector by subtracting the normal of the line from direction vector, thus it should now follow the line...
However, if its direction is the opposite of the normal (-normal that is), the object will completely stop...
Something like this was recently discussed on:computer.games.design.programming.algorithms (newsgroup)
Thread is called sliding motion with multiple constraints, but it's slightly more complex than this...