We consider a shape optimization problem written in the optimal control form: the governing operator is the $p$-Laplacian in the Euclidean space $R^d$, the cost is of an integral type, and the control variable is the domain of the state equation. Conditions that guarantee the existence of an optimal domain will be discussed in various situations. It is proved that the optimal domains have a finite perimeter and, under some suitable assumptions, that they are open sets. A crucial difference is between the case $p>d$, where the existence occurs under very mild conditions, and the case $ple d$, where additional assumptions have to be made on the data.