I have the following code:
#! /usr/bin/perl -T
{
package MSG;
use strict;
use warnings;
sub info
{
print STDERR join("\n", @_), "\n";
}
sub import
{
no strict 'refs';
*{caller().'::info'} = \&info;
}
}
{
package main;
use strict;
use warnings;
MSG->import;
# sub info;
info "a", "b";
}
Without the sub info;
line in the main
package, I get the following error:
String found where operator expected
I think the reason is explained here. When I add the line, the code works as expected. But I do not want it in the main
package.
How to move whatever sub info;
does into the import
function of the MSG
package?