Unlike the mathematician, the light doesn't know where the other side of the glass is. There's no guarantee that it's 'taking the shorter path' - for example, light hitting the edge of a glass cube that refracts into the cube will be taking a longer path than not refracting in the first place.
Not that light could have a motive anyway, but this strikes me as more of a passing fancy than a rationale.
You may want to read up further on Fermat's principle.
Your response is a little bit ill-formed because Fermat's principle is about the time to travel between two points. You assert that light is not "taking the shorter path", but in doing so you are changing the destination point or else leaving it undefined. Instead, pick a start point, pick an end point, and see how light travels between those two points, with respect to your cube of glass.
From the perspective of their own frame, photons don't travel. They are everywhere along their path at once. From our perspective, a photon goes through A first, then B. From the photon's perspective, that is not how it is.
So the concept of a photon 'picking' a destination point, or not, is mired in an assumption that isn't true (that there would even be anything to pick).
So, the thing is, there are alternative ways of looking at the laws of physics that take a time neutral view and look at the entire system and do take the view that the entire system and use exactly this kind of reasoning. In such an approach the ray of light doesn't start at one point en route to the other, and it's more legitimate to see the system as a whole and see some kind of minimization like this - minimizing the time the light spends in the slower medium.
You might want to read up on the principle of least action. Light takes the shortest path, not because it "knows" or anything silly like that, but because that path's action is a minimum (actually it need only be a stationary point). From a quantum mechanical perspective (see Feynman's lectures about QED), light takes all paths, but the sum tends to the classical path, which is the shortest.
Not that light could have a motive anyway, but this strikes me as more of a passing fancy than a rationale.