"Lying per se is not illegal. For it to be so, the person lied to has to suffer a detriment that the lier [sic] ought to prevent"
That will not be correct. For example, I go to the lost and found and lie to them that I lost my iPhone, giving a precise description of the iPhone that someone else I know lost. The person lied to (the employee at the lost and found) will not suffer any detriment; the rightful owner will. Or any time to go to a shop and lie to an employee to defraud the store, the employee lied to is unlikely to suffer a detriment.
Now in court, if I lie and give a false alibi to someone who is absolutely innocent but cannot prove it, that's perjury. Even though nobody suffered (including the state who would have convicted an innocent person without my lie), it's still perjury.