For example, consider the following situation:
I have a simple plane pendulum consisting of a mass $m$ attached to a string of length $\ell$. After the pendulum is set into motion, the length of the string is shortened at a constant rate
$$\tag{1}\frac{d\ell}{dt}=-\alpha=constant$$
as shown in the below image
then, if i want to write the Hamiltonian why can't i simply write its definition $${\cal H} = p_\theta \dot{\theta} + p_\ell \dot{\ell} - {\cal L}$$ and work on it and, in the end, apply (1) to my results?
I see in every example of hamiltonian always the "constraints" are applied directly in the start of the process (in the solution of this particular example, after apply (1) the hamiltonian is ${\cal H} = p_\theta \dot{\theta} - {\cal L}$). Why this happen?