http://www.perlmonks.org?node_id=1017926


in reply to Redefining Imported Subs: of scope and no

I don't understand why you're using feature qw(say) if you wanna redefine 'say.

I had trouble understanding that too. But as I understand it, the op turned on say() with his use statement, and now the op has say() statements littered throughout the code. Now the op wants to figure out a way to make his say() statements print nothing if a switch is set. Note that his custom say() sub doesn't print anything if $verbose is false.

As for 'use if', I was looking at that too, and I found two problems with it:

  1. 'use if' doesn't work as advertised for me. Here is the syntax shown in the 'if' pragma:

    use if CONDITION, MODULE => ARGUMENTS

    …but it doesn't work for me:

    use if $/ = "\n", 5.012 => qw( say ); --output:-- Can't locate 5.012.pm in @INC (@INC contains: /Users/7stud/perl5/perlb +rew/perls/perl-5.16.0/lib/site_perl/5.16.0/darwin-2level /Users/7stud +/perl5/perlbrew/perls/perl-5.16.0/lib/site_perl/5.16.0 /Users/7stud/p +erl5/perlbrew/perls/perl-5.16.0/lib/5.16.0/darwin-2level /Users/7stud +/perl5/perlbrew/perls/perl-5.16.0/lib/5.16.0 .) at /Users/7stud/perl5 +/perlbrew/perls/perl-5.16.0/lib/5.16.0/if.pm line 13. BEGIN failed--compilation aborted at 2.pl line 1.

    Yet all my perl programs use 5.012 without error. In addition, the 'use if' can't see a my variable in the code, e.g.

    my $verbose = 1;

    Apparently, 'use if' can only see a global variable that exists at compile time. Using our to declare $verbose doesn't work either:

    our $verbose = 1; use if $verbose, 'strict'; $v = 'hello';

    No error. I see that you got around that problem with use constant.

  2. Turning off say() will cause all the say() statements in the op's code to produce errors.

It looks like 'use subs' can be made to work (I think you originally posted something about that??):

use strict; use warnings; #use 5.012; #enables say() use subs qw( say ); my $verbose = 1; sub say { if ($verbose) { print shift, " world\n"; } } say 'hello'; --output:-- hello world