Unified treatment of cosmological perturbations from superhorizon to small scales