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

Thanks again everyone. Your lifesavers
I had a couple of problems
First problem was-- a typo
The second problem was-- I used -require- to extract my variables, but did not use -our -
use strict; require "vars.pm"; our $var1; our $var2;
Hello,
I am having a problem and can not figure it out. I have this var set
my $num = 44; print NEWFILE "<font>$num</font>\n";
I get this error --requires explicit package name--
and do not understand why.

Replies are listed 'Best First'.
Re: requires explicit package name
by ikegami (Patriarch) on Nov 12, 2010 at 18:58 UTC
    Not from that code you don't.
    $ perl -c -e' use strict; my $num = 44; print NEWFILE "<font>$num</font>\n"; ' -e syntax OK
      Sorry,
      I will try to come up with more information
Re: requires explicit package name
by kcott (Archbishop) on Nov 12, 2010 at 19:30 UTC

    The error is from use strict - which is good. Also check you have use warnings.

    This type of problem is (almost) always due to bad code which isn't a syntax error. For instance, a stray double-quote on a line above could be wrapping my $num in a string. Mismatched parentheses, brackets or braces around this code are likely candidates. A missing semicolon to terminate a statement is another possibility. There could be other reasons.

    The line where the error is reported is important - you haven't shown that here.

    If you can't find the problem through simple visual inspection, try commenting out a line near the error and see if you get different (more useful) messages.

    I also find it good practice to keep a mental catalogue of silly mistakes I make often and check for those first. For example, I often don't release the Shift key quickly enough, resulting in a colon, rather than a semicolon, at the end of the line, e.g.

    $var1 = $hash{$key}: $var2 = some_sub(@args):

    Use of an editor with syntax highlighting can also sometimes help in this situation. I understand that is a personal choice and some people prefer not to use syntax highlighting.

    -- Ken

      Hi Ken
      I will check for for those things.
      My brain was on a different journey for the answer to my problem. I think it might be on the right path now
      Thanks for the information.
Re: requires explicit package name
by marto (Cardinal) on Nov 12, 2010 at 19:02 UTC

    Can you please post an example script which reproduces the error you are experiencing? We'd rather not have to waste time guessing exactly what you're doing. See How do I post a question effectively?.

      I have to narrow the problem down .
      I thought it was something simple I missed .
      The script in question is called from another script using CGI.pm .
      sorry for the vagueness.