zentara has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I've been testing a little game program, and it would not run at first, giving an error:
Can't use string ("STDOUT") as a symbol ref while "strict refs" in use at /usr/lib/perl5/5.8.0/Term/Cap.pm line 576.
So Ok, easy enough to make a copy of Term::Cap and throw a 'no strict refs' into it.

But I was wondering if there is a way to tell Term::Cap to not to use strict refs from the script. Something like:

#!/usr/bin/perl use warnings; use Term::Cap 'no strict refs';
Another problem is that Term::Cap is not actually called by the script, but by Perl itself. Is there some trick?

Replies are listed 'Best First'.
Re: setting 'no strict refs' in a module from the script
by ysth (Canon) on Jan 25, 2004 at 20:32 UTC
    Try using *STDOUT where you have STDOUT. What do you mean "by Perl itself"??
      I mean that the game script dosn't have a use Term::Cap statement in it, so Perl itself is bringing it in.
        I don't think perl will do that. Perhaps you are using some other module that is using Term::Cap? Here are some things that will:
        use re 'debugcolor'; #or use Pod::Text::Termcap; #or (shell command) $ pod2text -t #or use Term::Readline; # unless ornaments are disabled
        If it is coming from Term::Readline, you may be able to disable ornaments by setting an environment variable PERL_RL=" ornaments=0"
Re: setting 'no strict refs' in a module from the script
by antirice (Priest) on Jan 25, 2004 at 22:00 UTC

    You can always replace &strict::import. Something like this untested piece of code may do:

    package StrictDisallow; no warnings; BEGIN { require strict; *o_import = \&strict::import; *strict::import += \&n_import } our %disallow; our @stricts = qw(refs subs vars); sub import { shift; my $namespace = shift; return unless $namespace && @_; %disallow = (%disallow,$namespace,[@_]); } sub n_import { my $caller = caller; if (exists $disallow{$caller}) { my $class = shift; my %temp; if (@_ == 1) { @temp{@stricts} = (); } else { @temp{@_} = (); } delete @temp{@{$disallow{$caller}}}; return unless %temp; @_ = ($class,keys %temp_hash); } &o_import; } 1;

    Then you'd use it like:

    #!/usr/bin/perl -w use strict; use StrictDisallow Term::Cap => "refs"; ...

    Just be certain to use StrictDisallow before loading the modules you wish to restrict.

    I hope this helps.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      Wow, that is nice. It would seem that Perl would have this sort of thing built into it. Or maybe there is a reason why it dosn't?

        It's because strict and warnings are meant to help in development and testing. Since you are using these other modules, they should compile properly. The easiest thing is to create a patch, apply it locally and submit it to the module's author.

        On a side note, someone already suggested that I put StrictDisallow.pm on CPAN. I have no interest in it and anyone who wishes to do so is free to do so if they please. If anyone does, though, a shout out would be nice =P

        antirice    
        The first rule of Perl club is - use Perl
        The
        ith rule of Perl club is - follow rule i - 1 for i > 1

Re: setting 'no strict refs' in a module from the script
by ysth (Canon) on Jan 27, 2004 at 00:53 UTC
    What's at line 576? What version is Term::Cap? There isn't anything to do with STDOUT at that line in version 1.07 (included with 5.8.0) or version 1.08 (included starting with 5.8.1).
Re: setting 'no strict refs' in a module from the script
by zentara (Cardinal) on Jan 27, 2004 at 13:15 UTC
    In case anyone is interested, the script I'm talking about is textmaze by robobunny. textmaze