Cosmological perfect fluids in higher-order gravity