Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Redefining Imported Subs: of scope and no

by 7stud (Deacon)
on Feb 09, 2013 at 04:26 UTC ( #1017926=note: print w/replies, xml ) Need Help??

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:


    …but it doesn't work for me:

    use if $/ = "\n", 5.012 => qw( say ); --output:-- Can't locate 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/ line 13. BEGIN failed--compilation aborted at 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

Replies are listed 'Best First'.
Re^2: Redefining Imported Subs: of scope and no
by Anonymous Monk on Feb 09, 2013 at 09:41 UTC

    but it doesn't work for me:

    Not surprising, $/="\n" assignment is always true (and assignment ), 5.12 is not a module (maybe you wants features), and 5.12 doesn't export say (features you want, maybe)

    Apparently, 'use if' can only see a global variable that exists at compile time.

    nothing specific to if, that is the nature of compile-time and our/my, initialization doesn't happen until later

    use constant DEBUG => !!( 0 || $ENV{PERL_DEBUG_MYAPPNAME} );
    use if DEBUG , qw' Carp::Always ';

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1017926]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2018-06-19 16:40 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (114 votes). Check out past polls.