Let $G$ be any group with nontrivial center $Z$. Let $H = \{(a,b)\in G^2\;|\;ab^{-1}\in Z\}$, equivalently, let $H$ be the subgroup of $G^2$ generated by the diagonal copy of $G$ and the subgroup $Z^2$.
Claim 1. $H$ is a subdirect product of copies of $G$. [$H$ contains the diagonal copy of $G$, $D\leq G^2$, which is itself subdirect.]
Claim 2. $H$ is not perfect. [The diagonal subgroup $D\leq H$ is normal in $H$, and $H/D\cong Z$ is abelian. Thus $H'\leq D \lt H$.]
The above shows how to produce nonperfect subdirect products. To complete the answer to the question, we need to apply the construction when $G$ is perfect, so we need a group $G$ that is perfect and has a nontrivial center. $G=\textrm{SL}_n(q)$ has these properties if $n,q>2$ or if $n=2$, $q>3$.