Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting up a variable, explicit package required

Status
Not open for further replies.

HughbertD

Technical User
Apr 30, 2007
42
GB
From the code below I get the following errors:

Global symbol $start requires explicit package name.

I have already initialised this variable here,

Code:
my $start=$newLine[0];

but it refers to my SQL statement:

Code:
my ($queryAll) = " select ev_title, ev_locn, dateTime, dateTimeFin from calendar where dateTime > $start and dateTimeFin < $fin ;";

Am I not setting up this statement correctly?

All code is below:


Code:
#! c:/perl/bin/perl

use Mysql;
use strict;
use Spreadsheet::WriteExcel;

# HTTP HEADER


my ($host) = "localhost";
my ($database) = "joStark";
my ($tablename) = "calendar";
my ($user) = "root";
my ($pw) = "michael";

#dates from the php form

open FILE, "../export/dates.txt" or die "Cannot open file";
 #assign them to variables
while (my ($line)=<FILE>)
{
        my(@newLine)= split(/,/,$line);

       my ($start)=$newLine[0];
       my  ($fin)=$newLine[1];
}

my $workbook = Spreadsheet::WriteExcel->new("perl.xls");

        # Add some worksheets
       my $sheet1 = $workbook->add_worksheet("Audit");
       my $sheet2 = $workbook->add_worksheet("All Meetings");
       my $format = $workbook->add_format();
       my $format2 = $workbook->add_format();

       #Add format
       $format->set_bold();
       $format->set_align('center');


# PERL MYSQL CONNECT
my ($connect) = Mysql->connect($host, $database, $user, $pw);

$connect->selectdb($database);



my ($queryAll) = " select ev_title, ev_locn, dateTime, dateTimeFin from calendar where dateTime > $start and dateTimeFin < $fin ;";

my ($executeAll)=$connect->query($queryAll);
my ($rowNumberAll) = $executeAll->numrows();
my ($numfieldsAll)= $executeAll->numfields();

my ($i)=3;

$sheet2->write(1, 0, "Meeting Title", $format);
$sheet2->write(1, 1, "Meeting Location", $format);
$sheet2->write(1, 2, "Date & Time Start", $format);
$sheet2->write(1, 3, "Date & Time Finish", $format);

my $dateformat = $workbook->add_format(num_format  => 'dd/mm/yy H:mm AM/PM');
$sheet2->add_write_handler(qr/^\d{4}-\d{2}-\d{2}$/, \&write_date);



my ($min)=0;

for ($min=0;$min<=$numfieldsAll;$min++)
{
	$sheet2->set_column($min,$min, 30);
}

while (my (@resultsAll) = $executeAll->fetchrow())
{

	$sheet2->write($i, 0, $resultsAll[0]);
        $sheet2->write($i, 1, $resultsAll[1]);
        $sheet2->write($i, 2, $resultsAll[2], $dateformat);
        $sheet2->write($i, 3, $resultsAll[3], $dateformat);
        $i++;
}



#Query time!
my ($query) = " select ev_locn, sum(mtg_hrs) as total_hrs from ( select time_to_sec(timediff(dateTimeFin, dateTime)) / 3600.0 as mtg_hrs, ev_locn from calendar ) as t where mtg_hrs>=2.0 group by ev_locn;";

my ($execute)=$connect->query($query);

my ($rownumber) = $execute->numrows();
# FETCHROW ARRAY

#foreach (my (@results)=$execute->fetchrow())
while (my (@results) = $execute->fetchrow())
{

      	$sheet1->write(0, $i, $results[0], $format);
     	$sheet1->write(1, $i, $results[1]);
       # print $i;
        $i++;

}
 
$start and $fin will not be visible outside the "while" block that you declare it in. Just move them to the top with your other globally declared variables and you will be OK.

Code:
my ($host) = "localhost";
my ($database) = "joStark";
my ($tablename) = "calendar";
my ($user) = "root";
my ($pw) = "michael";
[b]my ($start,$fin);[/b]

#dates from the php form

open FILE, "../export/dates.txt" or die "Cannot open file";
 #assign them to variables
while (my ($line)=<FILE>)
{
        my(@newLine)= split(/,/,$line);

       [b]$start = $newLine[0];[/b]
       [b]$fin = $newLine[1];[/b]
}

$start and $fin will have the value of the last line (or only line) of the file.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top