Code references cannot be simply serialized. File descriptors, database connections, and anything that has external resources cannot simply be serialized.
When serializing such elements, you must describe them in such a way that they can be recreated. For example, you can serialize a file descriptor as a path and an offset or a code link as the name of the function referenced by the link.
You can find the name of the routine referenced by the code points with Sub::Identify :
#!/usr/bin/perl use strict; use warnings; use Sub::Identify qw/sub_fullname/; sub foo {} my $r = \&foo; print sub_fullname($r), "\n";
Of course, this means that you cannot serialize anonymous links, and serialized data can only be reliably used by programs that implement named functions in the same way.
If you need to do this, you are probably better off using a class instead of just referencing the code.
Chas. Owens
source share