in reply to Indent, anonymous sub, or lexical sub for module private code?
I don't use 5.18 yet, so I'm not sure about this "lexical sub" you speak of, but with older versions, I think there's a "morally equivalent" approach that might have the properties that you're looking for. The following works for me in 5.12 (and presumably works in any Perl 5):
Note how I assign an anonymous sub to a lexically scoped variable. Here's a simple use case:# TryMe.pm package TryMe; use strict; use warnings; sub new { my %obj = ( name => "TryMe", purpose => "None that I know of" ); return bless \%obj; } sub do_something { my ( $class, $with_something ) = @_; my $special_trick = sub { "... this is only a test " }; my $return_value = "Here's how I do_something $with_something:\n"; for ( 1 .. 3 ) { $return_value .= &$special_trick; } $return_value; } 1;
#!/usr/bin/perl use strict; use warnings; use TryMe; my $tester = TryMe->new(); print $tester->do_something( "nice" ), "\n";
|
|---|