The virial theorem and the factorized wavefunction approach to perturbation theory. Application to the spherical Stark and spherical quadratic Zeeman problems in hydrogen