in reply to required explicit package error.

You're getting these errors because you're using strict. But don't turn it off, because these errors are good! They're telling you that you should explicitly declare your variables as either package globals or lexicals (my).

In your case, just use lexicals. Add this, before you start executing any code:

my($currentLine, $name, $infilename);
I've got to sort of question your program flow, though; you're looking for lines that begin with $name, right? But $name isn't defined--it has no value. So what are you trying to look for, then? Or are you trying to look for the string literal '$name', not the value of the variable name? If so, you should backslash the '$':
if ($currentLine =~ /^ \$name:/) {
Also, the first line of your script is a bit odd:
!/usr/local/bin/perl
Is this what you're really using? I don't think this will work. You should have:
#!/usr/local/bin/perl
and while you're at it, you should turn warnings on:
#!/usr/local/bin/perl -w

Replies are listed 'Best First'.
RE: Re: required explicit package error.
by Paav (Novice) on Jun 14, 2000 at 23:58 UTC
    Btrott, Thanks a lot for your help. I still have a little problem I modified the program a little bit. It will be recieving information from a file. The program is supposed to parse the line with total in it. there is file rows there. i need to use the las two, the one with the total.
    ..... ..... .... .... .... ..... ..... .... .... .... total 1234 1234 5678 7894
    I need to use the the numbers "5678" and "7894", and graph them in excel everyday. The graph would be "day" vs "total". I hope this makes what I am trying to do a little easier to understand. Now my code reads...
    #!/usr/local/bin/perl -w my($currentLine, $name, $infilename); chomp ($infilename = "temp1.pl"); if ($ currentLine =~ /^ \$name:/) { print $currentLine; }
    Thanks alot, Paav
      It sounds like you just want a simple script to open a file, find the line that says "total", and report the last two rows of numbers. Here is some code to do this :
      #!/usr/local/bin/perl print "Input file name: "; $_ = <>; chop; open(INFILE, $_); while (<INFILE>) { if (m/total/) { s/^total\s[0-9]+\s[0-9]+\s([0-9]+)\s([0-9]+)$/$1 $2/; print; } } close INFILE;
      There are probably 3 things in this script that are non-intuitive to the budding Perl programmer :

      1) The use of $_
      2) The use of <>
      3) The use of regular expressions

      Here they are, demystified :

      1) $_ is the "default variable" when you don't supply any other variable name to a sub (such as chop and print in our example).

      2) <> abbreviates <STDIN>

      3) There are two regular expressions in this example, the first one is a matching regular expression (m/total/) which returns true if and only if the text "total" exists as a substring of $_

      The second regular expression is a search and replace regex.
      s/^total\s[0-9]+\s[0-9]+\s([0-9]+)\s([0-9]+)$/$1 $2/
      all it does is replace "^total\sA\sB\sC\sD$" with "C D" where ^ (caret) matches start of line, \s matches space, and A B C and D are numbers (1 or more digits). The use of the parenthesis following the first / provides Perl's regular expression engine the ability to do what is called "backreferencing" (using $1 and $2 after the second /).

      Let me know if that code works for you, and if there's anything else you'd like to do.

      -verbal </CODE>
        Verbal, I tried your method but I still get an error. It takes the file in however, it does nothing with it. how come??
        #!/usr/local/bin/perl print "Input file name: "; $_ = <>; chop; open(INFILE, $_); while (<INFILE>) { if (m/total/) { s/^total\s[0-9]+\s[0-9]+\s([0-9 +]+)\s([0-9]+)$/$1 $2/; print; } } close INFILE;
        Thanks a lot for yout help.. Paav
        Verbal, I tried the code out the way you had asked.It takes the input in however, it doesn't do anything with it. By the way, I am the one, who is trying to read the total from a file, and spit it out to another file. My code reads following:
        print "Input file name: "; $_ = <>; chop; open(INFILE, $_); while (<INFILE>) { if(m/total/) { s/^total\s[0-9]+\s[0-9]+\s([0-9]+)\s([0-9]+)$/$1 $2/; print; } } close INFILE
        I think the program is getting the input, however it is just not doing anything with it. Once I enter the name of the file, it just sits there, instead of parsing. How come?? THanks Paav
        Verbal, I tried the code out the way you had asked.It takes the input in however, it doesn't do anything with it. By the way, I am the one, who is trying to read the total from a file, and spit it out to another file. My code reads following:
        print "Input file name: "; $_ = <>; chop; open(INFILE, $_); while (<INFILE>) { if (m/total/) { s/^total\s[0-9]+\s[0-9]+\s([0-9]+)\s([0-9]+)$/$1 $2/; print; } } close INFILE;
        I think the codes seems OK. THe name of the file that i will be reading the data from is called, "elements". My question is, I am not sure if the file is even being read, or parsed. How would I print out the parsed information?? Thanks Paav
      I don't know what you're doing there. It seems you've solved your initial problem, but that code isn't going to work correctly. Here's some code that may do what you want:
      #!/usr/local/bin/perl -w use strict; while (<>) { next if !/^total/; my @fields = split /\s+/; my($day, $total) = @fields[3,4]; ## Now graph the values or do whatever you ## want with them. ... ## This will exit the loop after it's seen ## the first "total" line. If you don't want ## to do this (if, for example, you'll have ## several "total" lines, each of which you ## want to graph), take out this line. last; }
      To be invoked as such:
      % foo.pl <filename>