A Massachusetts man who spent 30 years behind bars for a rape he did not commit was finally freed Wednesday after it was determined the FBI analysis of a single hair found at the crime scene was based on flawed science.

An FBI agent told jurors that the hair, which tests showed had the victim’s blood on it, had to belong to Perrot, while the prosecutor insisted the only way he could be innocent was if police planted the hair at the scene.

Perrot signed a confession that said he broke into the elderly woman’s home, but later retracted the admission, saying he was under the influence of alcohol and valium and did not remember giving it.

Prosecutor Francis Bloom was found to have fabricated a confession from Perrot and forged his signature on it to pressure two other teens to confess that they helped with the break-in and rape.

He was convicted of the rape and beating in 1987 and ordered to serve two concurrent life sentences.

But it was decided earlier this year that Perrot was entitled to a new trial “because the introduction of hair evidence that in numerous and material respects exceeded the foundation of science,” the Innocence Project wrote.

Almost every examiner in that forensic unit of the FBI was found to have given flawed testimony in almost all trials in which they presented evidence, the Bureau has acknowledged.

Perrot plans to live in Massachusetts while he awaits a decision from prosecutors about the future of his case, the Insititute reported.

The Innocence Project, which worked to secure a new trial for Perrot, applauded what they called a landmark decision.

“The decision is vitally important because it will be followed by many other courts around the country which will have to decide how to deal with this erroneous testimony,” said Chris Fabricant, director of strategic litigation for the Innocence Project.

Erroneous microscopic hair analysis contributed to more than 20 percent of the nation’s 337 DNA exonerations, the Innocence Project wrote.