Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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:

    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


Comment on Re: Redefining Imported Subs: of scope and no
Select or Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1017926]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (12)
As of 2014-07-22 21:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (129 votes), past polls