Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Requires explicit package name

by jaggu_bg (Initiate)
on Nov 28, 2008 at 05:26 UTC ( #726519=perlquestion: print w/ replies, xml ) Need Help??
jaggu_bg has asked for the wisdom of the Perl Monks concerning the following question:

Monks... what to do to fix this error ???? the program & the error is below,
Global symbol "$houtputfile" requires explicit package name at good.pl + line 12. Global symbol "$pid" requires explicit package name at good.pl line 12 +. Global symbol "%hfiles" requires explicit package name at good.pl line + 30. Global symbol "%hfiles" requires explicit package name at good.pl line + 38. Global symbol "%hfiles" requires explicit package name at good.pl line + 39. use strict; use warnings; my $input_file = $ARGV[0]; #input file name my $input_pid = $ARGV[1]; #input pid number my $input_string = $ARGV[2]; #input string to be searched print "Usage:perl file.pl <input file name> <input process id> <input + file string>" if (@ARGV != 3); open (my $hfile, '<', "$input_file") || die("Unable to open the file $ +input_file\n"); open (my $houtputfile, '>', "$houtputfile.$pid") || die("Unable to ope +n the file output.txt\n"); $hfile = *HFILE; my %hFiles = (); while (<$hfile>){ #read the file line by line my $line = $_; if ($line =~ /[DWEM]\s+.*?\s+(?:c|cm|M)\:($input_pid)\s+([^\.]+\.c +)/s) { my $c_file = $2; my $pid = $1; if($input_string eq $c_file){ #check with input string and I +f it matches # print $houtputfile "$line\n"; #write the output in a output + file $houtputfile = $hfiles{$pid}; if($houtputfile) { print $houtputfile "$_\n"; } else { print ("pid = $pid\n"); open( HPIDFILE.$pid,">$houtputfile.$pid") #open( $hfiles{$pid},">$houtputfile.$pid") or die("failed to open $houtputfile.$pid "); $hfiles{$pid} = *HPIDFILE.$pid; $houtputfile = $hfiles{$pid}; print $houtputfile "$_"; } } } }

Comment on Requires explicit package name
Download Code
Re: Requires explicit package name
by brsaravan (Scribe) on Nov 28, 2008 at 06:05 UTC
    The stricture enforces the user to declare the variable before use.
    * Declare $pid first.
    * $houtputfile is a file handle it seems, but you are trying to concatenate with $pid.
    * what *HFILE refers to.
    * $hfiles{$pid} ----where is this "hfiles" variable defined.
    Now debug and rewrite your code.
Re: Requires explicit package name
by kennethk (Monsignor) on Nov 28, 2008 at 06:06 UTC
    The warnings are being raised because you are using variables out of scope. For example, in your open statement, you call $houtputfile and $pid before declaring them with a "my" or, worse, giving them a value. I'd offer a fix, but I'm not sure what their values are supposed to be. Your %hfiles problems are easier to spot - perl is case sensitive, and you've declared %hFiles, not %hfiles.
Re: Requires explicit package name
by Narveson (Chaplain) on Nov 28, 2008 at 06:10 UTC
    "Requires explicit package name" is the error message you get under strict when you have failed to declare a variable. I shared your confusion the first time I saw this message. The remedy is not to supply a package name, but to declare the variable with my.
      The error is indeed misleading. use diagnostics; gives better info, fortunately.
      Supplying a package name *is* a valid way to remedy this problem.
      use strict; $::some_var = "some value"; print $::some_var;
      Works fine.
        So does
        no strict; $some_var = "some value"; print $some_var;

        Would we sincerely recommend either method of silencing the error messages in the original post?

      so maybe the warning should be changed and spelled

       var foobar needs explicit my or possibly (in rare cases) an our (or use vars) declaration or explicit pkg qualification like $pkg::foobar
      cheers --stephan

Log In?
Username:
Password:

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

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

    Is guessing a good strategy for surviving in the IT business?





    Results (44 votes), past polls