Monks I am seeking your help once again. I apologize in advance if I am just over looking something very simple, but I just can't figure out what I am doing wrong here. I am working on a script that first reads some data into a hash. Then it reads some data from a flat txt benchmark file into another hash. From there I am printing a report that essentially just compares the hashes to print the data. However I am getting an error that one of my variables needs to an explicit package name and I can;t figure out why. Here is the code. I have tried to strip it down to just the pertinent stuff.
#!/usr/local/bin/perl -w use strict; use vars qw($opt_m); use Getopt::Std; my ($today) = &finder(); if ($script_mode eq 'normal') { my ($yesterday) = &read_benchmark(); &write_benchmark(); &print_report(); } else { &write_benchmark(); } sub finder { my %today; open (IN, "< todays data") or die "$!"; while (<IN>) { chomp; $today{$_}++; } close (IN); return \%today; } sub read_benchmark { my %yesterday; open (BENCH, "< benchmark.txt") or die "$!"; while (<BENCH>) { chomp; $yesterday{$_}++; } close (BENCH); return \%yesterday; } sub write_benchmark { open (BENCH, "> benchmark.txt") or die "$!"; foreach (sort keys %$today) { print BENCH "$_\n"; } close (BENCH); chmod(0640, "benchmark.txt") or die "$!"; return; } sub print_report { foreach (sort keys %$today) { print LOG "$_\n" unless exists $yesterday->{$_}; } foreach (sort keys %$yesterday) { print LOG "$_\n" unless exists $today->{$_}; } print LOG "End of Report.\n"; return; }
The Error message I get is. Global symbol "$yesterday" requires explicit package name at ./daily10 line 115. Line 115 is where I try to use $yesterday->{$_}. Now I am guessing that %yesterday isn't accessible because it is returned to that if statement, but the other subs that access are within that if statement too. I guess by declaring the variable in use vars that would probably work, but I don't understand why it isn;t working here. Any help is appreciated. Thanks.

In reply to Problem Declaring Variable by PrimeLord

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.