Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

"Global Symbol Requires Explicit Package Name" error

by Portree (Novice)
on Aug 09, 2005 at 17:52 UTC ( [id://482330]=perlquestion: print w/replies, xml ) Need Help??

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

Hello All,
I have a question to pose to the wisdom of the Monks. I am just starting to work on Tab Delimited files and I have already run into a problem. I continue to get an error "Global Symbol Requires Explicit Package Name". Can anyone find what it is that I am doing wrong? I have read the files on declaring variables and Lexicon variables and they didn't seem to give me the specific information that I need to fix this. Any help is greatly appreciated.
Portree
use strict; use warnings; use win32::ole; my ($f_mfg_desk, $f_mfg_desk_output, $record,@wip_query, $wip_query, $ +opr_cd, @header, $header, %header, $x, $row, $row_array, @row_array, @rule_array, $ +BREAK, $year, $month, $day, $hour, $min, $sec, $run_time, $dtme, $local_outfile_timestamp, $remote_outfile_timestamp, $local_outfil +e_latest, $remote_outfile_web, $Oper_Cd, $Pick_Ref_Num, $Part_Id, @header_da +ta, $newrow, @newrow, $head); # This gets the document from the server $f_mfg_desk = '//142.22.50.34/planning/logistics/programs/wip_query.tx +t'; @header = ("Oper_Cd","Pick_Ref_Num","Part_Id","Lot Qty","Order Id", "Order Lineitem Id","Order Lineline Id","Sublot Num", "Comm Invoice Num","Oper In Dt","Cust Early Shp Dt", "Cust Late Shp Dt","Current Date","Business Class Cd", "Action Expedite Flg","Required Ship Saleable", "Required Ship Inventory Transfer","Total Required Ships"); open (INFILE, '//163.10.50.33/planning/logistics/programs/chicago_wip_ +query.txt'); while (<INFILE>) { $record = $_; chomp($record); @newrow = split(/\t/,$record); ###This next line is where I get the error $header_data{$newrow[2]} = [$newrow[3], $newrow[4], $newrow[5], $n +ewrow[6], $newrow[7], $newrow[8], $newrow[9], $newrow[10], $newrow[11 +], $newrow[12], $newrow[13], $newrow[14], $newrow[15], $newrow[16], $ +newrow[17], $newrow[18]]; } ### error ends here close(INFILE);

Edited by Arunbear: Changed title from 'Variables', as per Monastery guidelines

Replies are listed 'Best First'.
Re: "Global Symbol Requires Explicit Package Name" error
by holli (Abbot) on Aug 09, 2005 at 18:27 UTC
    I'll elaborate a bit on his one more bug. Let's walk through your program ;)

    use strict; use warnings; use win32::ole;
    You should get used to write module names in the right case (here: Win32::OLE), or your programs will throw "module foo::bar not found in @INC..." errors if they are ever run on a case-sensitive file system.
    my ($f_mfg_desk, $f_mfg_desk_output, $record,@wip_query, $wip_query, $ +opr_cd, @header, $header, %header, $x, $row, $row_array, @row_array, @rule_array, $ +BREAK, $year, $month, $day, $hour, $min, $sec, $run_time, $dtme, $local_outfile_timestamp, $remote_outfile_timestamp, $local_outfil +e_latest, $remote_outfile_web, $Oper_Cd, $Pick_Ref_Num, $Part_Id, @header_da +ta, $newrow, @newrow, $head);
    ugh.
    There's nothing wrong with globals if there are just a few and their use is justified. Blindly declaring all variables as globals begs for trouble. You should read about scoping.
    # This gets the document from the server $f_mfg_desk = '//142.22.50.34/planning/logistics/programs/wip_query.tx +t';
    This assigns a scalar and does not "get" anything. (cut & paste reduce problem fragment?)
    ... open (INFILE, '//163.10.50.33/planning/logistics/programs/chicago_wip_ +query.txt'); while (<INFILE>) { $record = $_; chomp($record); @newrow = split(/\t/,$record);
    A good example of bad scoping. $record here is just used within the loop, so you'd better write that as
    while (my $record = <INFILE>) {
    Also, you are just using $record to chomp and split it, so after all your code would be smarter written
    while (<INFILE>) { chomp; @newrow = split /\t/;
    which is a bit shorter and more concise.
    $header_data{$newrow[2]} = [$newrow[3], $newrow[4], $newrow[5], $n +ewrow[6], $newrow[7], $newrow[8], $newrow[9], $newrow[10], $newrow[11 +], $newrow[12], $newrow[13], $newrow[14], $newrow[15], $newrow[16], $ +newrow[17], $newrow[18]]; }
    This also can be written with a bit less effort.
    $header_data{$newrow[2]} = [@newrow[3..18]];


    holli, /regexed monk/
      Hello All
      I have read over all of your comments, and I understand to an extent what you are saying. I am a lowly hacker trying to automate a report. Below is the updated code I have, but I am now getting a new error. Here is what I ultimately want to do, and if someone can point me to a straight forward source then I am happy to try as much on my own as I can.

      I have a tabe delimited file. I want to eliminate rows where the date in column "Cust Late Shp Dt" is greater than today. Then I want to sort the data by the same field. Then save the data and put it back in the same location as I got it from. Below is the code that I have so far, but if I need to start completely over I will, just so I can get this done. Once I have the template and understand it, I think I can then apply it to several other reports. Thank you in advance for all of your help.
      use strict; use warnings; use Win32::OLE; use Date::Calc; my ($f_mfg_desk, $f_mfg_desk_output, @wip_query, $opr_cd, @header, %header_data, @row_array, $BREAK, $Oper_Cd, @newrow, $start_time); # This gets the document from the server $f_mfg_desk = '//163.10.50.33/planning/logistics/programs/chicago_wip_ +query.txt'; @header = ("Oper_Cd","Pick_Ref_Num","Part_Id","Lot Qty","Order Id", "Order Lineitem Id","Order Lineline Id","Sublot Num", "Comm Invoice Num","Oper In Dt","Cust Early Shp Dt", "Cust Late Shp Dt","Current Date","Business Class Cd", "Action Expedite Flg","Required Ship Saleable", "Required Ship Inventory Transfer","Total Required Ships"); $start_time = time; open (INFILE, '//163.10.50.33/planning/logistics/programs/chicago_wip_ +query.txt'); while (<INFILE>) { chomp; @newrow = spli t /\t/; $header_data{$newrow[2]} = [$newrow[3..18]]; } close(INFILE)


      Once I have this done I then have figured out the email functionality and several other items that I need. The actual manipulation of the document is what eludes me right now. I know I am just at the start, defining the variables and getting the header information read into the program, so I understand I have a long ways to go.

      Oh, and I am using Activestate Komodo to write this program in, which is better than a free-ware I was using. Is this the best program for this type of activity, or is there one that is maybe a little more helpful in pointing out where an error is and how to fix it?

        I'd recommend you check out woolfy's Where and how to start learning Perl as there are some strange assumptions here, but a possible rewrite:

        use strict;
        use warnings; #good!
        use Win32::OLE;
        use Date::Calc; why load when you don't use it, or Win32::OLE?

        #having a few variable with a large lexical scope isn't #nessarily bad but in general use the smallest scope # This acutally assigns the file location to a scalar # doesn't get anything (as [holli] stated) my %header_data; my $f_mfg_desk = '//163.../chicago_wip_query.txt'; # Since you don't actually use @headers: I dropped it # If you needed it just say why and I'll go from there # "time" gives seconds after epoch which is proably not what you want, + see localtime* my ($sec, $min, $hour, $dayofmonth, $month, $year, $weekday, $day) = l +ocaltime(time); $month++; $year += 1900; open (INFILE, $f_mfg_desk); #if you do my $record = <INFILE> you'd need chomp($record);... or while (<INFILE>) { chomp; my @newrow = split /\t/; #missing the ]; here $header_data{$newrow[2]} = [ $newrow[3..18] ]; } close(INFILE); #to sort and reprint (proably want to copy a backup too) open (OUTFILE, ">$f_mfg_desk"); # see perldsc* and sort* for my $key (sort keys %header_data) { #see perlref* for @{} for my $a (@{$header_data{$key}}) { print OUTFILE "$a\t"; } print OUTFILE "\n"; }

        *:localtime, perldsc, sort, perlref

        "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

Re: "Global Symbol Requires Explicit Package Name" error
by jdhedden (Deacon) on Aug 09, 2005 at 17:56 UTC
    You declared 'header_data' as an array, but are using it as a hash (i.e., you need to change @header_data to %header_data).

    Remember: There's always one more bug.
      jdhedden,
      Thank you very much, yes that is it. OK, so now I have a question. I did all of the reading on variables and I understand the use of my and "$" signs, but when do you use "@" or "%". Can you point me to an article for that? Thanks again.
Re: "Global Symbol Requires Explicit Package Name" error
by Joost (Canon) on Aug 09, 2005 at 18:19 UTC
    This:
    my ($f_mfg_desk, $f_mfg_desk_output, $record,@wip_query, $wip_query, $ +opr_cd, @header, $header, %header, $x, $row, $row_array, @row_array, @rule_array, $ +BREAK, $year, $month, $day, $hour, $min, $sec, $run_time, $dtme, $local_outfile_timestamp, $remote_outfile_timestamp, $local_outfil +e_latest, $remote_outfile_web, $Oper_Cd, $Pick_Ref_Num, $Part_Id, @header_da +ta, $newrow, @newrow, $head);

    is where you're going wrong.

    Really. You should use lexical variables in the smallest possible scope. That usually means: declare them where you're first using them, or a scope or two "up" if you're doing conditional assignments.

    What you're doing is trying to declare all variables you need up front, which besides being obviously hard to maintain, will also open up a lot of potential pitfalls, the least of which is that you're basically treating all variables as global.

    This specific error stems from failing to declare a variable.

Re: "Global Symbol Requires Explicit Package Name" error
by Mr. Muskrat (Canon) on Aug 10, 2005 at 03:11 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: "Global Symbol Requires Explicit Package Name" error
by PodMaster (Abbot) on Aug 10, 2005 at 04:49 UTC
    For a better explanations of error messages, you can look them up in perldiag ('perldoc perldiag'), or simply use diagnostics; to get the explanation automatically
    Global symbol ``%s'' requires explicit package name
    
        (F) You've said ``use strict vars'', which indicates that
        all variables must either be lexically scoped (using ``my''),
        declared beforehand using ``our'', or explicitly qualified to
        say which package the global variable is in (using ``::'').
    

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Hello All,
      Thank you very much for taking the time to reply. I linked to those documents and I will read them over, I have even printed some of them. So, am I to understand that this is some kind of text string error? Thank you all again

      Portree
        Hello All
        OK a few questions to help me understand. In my code below (as is familiar already) I have sections of the code that I have used, but I am really not quite sure what they do. I have played around with it and if someone could help me to understand better I would appreciate it.

        I have buried my questions in the code below, removing all comments that were there already, that way my questions are clear. Thanks again for the help

        Portree
        use strict; use warnings; #good! use Win32::OLE; use Date::Calc; use diagnostics; my %header_data; my $f_mfg_desk = '//163.10.50.33/planning/logistics/programs/chicago_w +ip_query2.txt'; my $f_mfg_desk2 = '//163.10.50.33/planning/logistics/programs/chicago_ +wip_query3.txt'; my ($sec, $min, $hour, $dayofmonth, $month, $year, $weekday, $day) = l +ocaltime(time); $month++; $year += 1900; open (INFILE, $f_mfg_desk); ($record);... or while (<INFILE>) { chomp; my @newrow = split /\t/; # What does this below statement acutally mean? $header_data{$newrow[2]} = [ $newrow[3..18] ]; } close(INFILE); open (OUTFILE, ">$f_mfg_desk2"); # If I am sorting by this data, why does it delelte most of # the data +? # Which data field is actually being used to sort? for my $key (sort keys %header_data) { for my $a (@{$header_data{$key}}) { print OUTFILE "$a\t"; } print OUTFILE "\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://482330]
Approved by holli
Front-paged by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-04-16 11:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found