Here is a passage from Luhmann’s A Systems Theory of Religion:
In general, there is no correspondence in the environment of the system for its code. Codes instead serve to balance internally the consequences of operative closing. That is because a system (in and of itself), which cannot contact the environment with its own operations (being unable to operate across boundaries), would have to view each environmental condition as equally probable. The coding, however, put [sic] the system in a position to treat surprises as irritations, to digitize them, to understand them as a problem of assigning the code values, or to develop corresponding programs for their repeated use–in short, to learn. In internally produced horizons of expectation, in assumptions about normality or in places of uncertainty, irritations are made visible as distinctions that can become distinctions and thus information. (46)
I’m not sure what “distinctions that can become distinctions” means. It makes more sense to say irritations are made visible as distinctions and thus information. If Luhmann hadn’t died before completing this book, he might have changed that sentence, or the translation might be flawed–note the sic above. And “operative closing” might be better translated as operative closure.
As for how systems learn, Luhmann is saying that they learn through their coding. A system establishes a binary code that does not exist in its environment. Coding allows a system to form expectations, or expectational structures, and expectations allow the system to process some irritations as surprises. The system then modifies its own expectational structures to account for the new information.