in reply to Safe module and undefined subroutines

Hello bovlb,

Welcome to the Monastery. Just a minor note to make here. You have not answered the question of Anonymous Monk What problem are you trying to solve with it?.

Why you want to call one subroutine as a parameter within another one? If you want to call a local subroutine withing another subroutine why you do not call it like this?

#!/usr/bin/perl use strict; use warnings; use feature 'say'; sub a { say 'sub a'; local *_b = sub { say 'sub b'; }; _b(); } a(); __END__ $ perl test.pl sub a sub b

Update: You can make it even more complicated if you want.

#!/usr/bin/perl use strict; use warnings; use feature 'say'; sub a { local *_b = sub { say @_; return 'sub b'; }; return _b('sub a'); } say a(); __END__ $ perl test.pl sub a sub b

Update2: You can make it even more complicated if you want, which is like calling a subroutine withing another subroutine a(_b()).

#!/usr/bin/perl use strict; use warnings; use feature 'say'; sub a { local *_b = sub { return @_; }; return _b(@_); } say a('sub a'); __END__ $ perl test.pl sub a

You can read more about this from this question Defining a sub within a sub: OK?. Tell us what you are trying to achieve? Many people here (not me) are very experienced so they can help you in many areas / ideas.

Looking forward to your update, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: Safe module and undefined subroutines
by 1nickt (Canon) on Aug 25, 2017 at 12:20 UTC

    Why you want to call one subroutine as a parameter within another one?

    It is rather common to do that. What's wrong with it, in your view?

    use strict; use warnings; use feature 'say'; print_timestamp('nigh'); print_timestamp( get_formatted_time() ); sub print_timestamp { say 'The time was ' . shift() } sub get_formatted_time { return uc scalar localtime } __END__


    The way forward always starts with a minimal test.

      Hello 1nickt,

      Maybe you missed understand what I was writing / asking. I am asking what was his target in total. It looks like XY Problem

      I never used it they way that you are using it, I would use it like this:

      #!/usr/bin/perl use strict; use warnings; use feature 'say'; sub print_timestamp { local *_get_formatted_time = sub { return $_[0] . uc scalar localtime; }; return _get_formatted_time(@_); } say print_timestamp('The time was: '); __END__ $ perl test.pl The time was: FRI AUG 25 15:18:08 2017

      Instead of:

      #!/usr/bin/perl use strict; use warnings; use feature 'say'; print_timestamp('nigh'); print_timestamp( get_formatted_time() ); sub print_timestamp { say 'The time was ' . shift() } sub get_formatted_time { return uc scalar localtime } __END__ $ perl test.pl The time was nigh The time was FRI AUG 25 15:20:10 2017

      Update: When you are calling a subroutine withing another subroutine like this:

      sub a { sub b{ } }

      It is basically the same as:

      sub a { } sub b{ }

      End of Update: So since he is try to test if a subroutine is safe (foo(bar()); # no errors reported - not expected behavior and foo(); # Undefined subroutine reported as expected). Why not simply calling it one by one, since it is the same thing if they are not defined locally?

      But I suppose it depends the case. Thanks for sharing other ideas, BR.

      Seeking for Perl wisdom...on the process of learning...not there...yet!

        You seem to be mixing up "calling another subroutine in a subroutine" and "defining another subroutine in a subroutine".


        The way forward always starts with a minimal test.