Let $f:E\rightarrow\mathbb{R}$ with $E\in\mathscr{M}$. $f$ is measurable iff for every open subset of $\mathbb{R}$, $f^{-1}(A)$ is measurable I'm learning about Measure Theory (specifically measurable functions) and need help with the following problem:

Let $f:E\rightarrow\mathbb{R}$ with domain $E\in\mathscr{M}$. Show that $f$ is measurable if and only if for every open subset of $\mathbb{R}$ the set $f^{-1}(A)$ is measurable. Hint: every open set of $\mathbb{R}$ is the countable union of open intervals.

In this context $\mathscr{M}$ is the set of measurable subsets of $\mathbb{R}$.

The way I understand the problem is that $f$ is measurable iff $f^{−1}(A)$ is measurable for every open set $A$ in the codomain. Is that correct? I don't understand how the hint is supposed to help me here. Sorry for the lack of effort but I don't have any clue about the answer.

$(\Leftarrow)$ $\mathcal{B}(\mathbb{R})$ is generated by the family of all open sets in $\mathbb{R}$, say $\mathcal{O}$. Now $\{B\subset \mathbb{R}:f^{-1}(B)\in\mathcal{M}\}$ is a $\sigma$-algebra containing $\mathcal{O}$ (by assumption) so that it contains $\mathcal{B}(\mathbb{R})$.
$(\Rightarrow)$ An open set is Borel.
Using the hint, it can be shown (similarly) that $f$ is measurable iff for every open interval $I$ $f^{-1}(I)\in \mathcal{M}$ ($\because \mathcal{B}(\mathbb{R})=\sigma(\mathcal{O})=\sigma(\mathcal{E})$, where $\mathcal{E}$ is a family of all open intervals in $\mathbb{R}$).
