in reply to Re: Safe module and undefined subroutines
in thread Safe module and undefined subroutines

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.

Replies are listed 'Best First'.
Re^3: Safe module and undefined subroutines
by thanos1983 (Parson) on Aug 25, 2017 at 13:25 UTC

    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.