View Full Version : error with explicit package name given
From one sub, I am returning an array to pass to another sub.
sub readdir {
.... stuff here....
return @rooms;
&processBooking;
}
sub processBooking {
my @rooms = &readdir(@rooms);
I don't understand why this gives the error because I have defined my @rooms.
error message.
Global symbol "@rooms" requires explicit package name at .......
bazz
hyperbole
03-08-2006, 07:41 PM
You are defining @rooms in another function. readdir can't see that version of @rooms because it is only available to processBooking. Try declaring @rooms inside readdir as well.
.
It is defined in both subs. :confused:
Should I have to make it global? If I do, it works but, I don't know why it has to be global.
sub readdir {
my @rooms;
my $Filepath = "/domains/574/2317/html/cgi-bin/$clientsReservationsDir/Rooms_Booked/$yearRequested/"; # to read in files for all dates.
opendir(my $dir,$Filepath);
while(my $roomsBooked=readdir($dir)){
next unless -f $Filepath.$roomsBooked && $roomsBooked!~/^\.+$/;
push(@rooms,$roomsBooked);
}
closedir($dir); # All booked dates are now read in.
return @rooms;
&processBooking;
}
sub processBooking {
my @rooms = &readdir(@rooms);
#print "at = @rooms";
}
FishMonger
03-08-2006, 09:31 PM
my @rooms = &readdir(@rooms);
Hmm...that seams to be a catch-22 (circular reference) and I'd expect it to issue a warning.
return @rooms;
&processBooking;
Why are you trying to execute the &processBooking sub after you've already exited the calling sub?
OK, made those changes that I thought you were suggesting. Still it throws up the error unless I make the array global.
I'm trying another approach, since I remembered the term 'last;' :)
I hope it goes better this time :)
bazz
OK this is where I am now and the question should be easier to pose.
In a sub, which does dir and file searcing and manipulation, it outputs a form. It also builds an array called @roomsToBook and this comprises a list of file names.
when the form is submitted, it causes a re-run of this script but with a different sub.
I want that array (@roomsToBook), to be passed to this other sub. so that it knows which files to open, save to and close.
So how can you pass an array from sub to sub, ideally, without it showing in the html of the form.
oops, here's the end of the first sub.
for (@vacantRooms) { $vacant{$_}++; }
#print Dumper %vacant;
for $room (keys %vacant) {
if ($numberOfNightsRequested == $vacant{$room}) {
#print "room = $room<br />"; #outputs the list of available rooms that date
push (@availableRooms, $room); # builds array of available rooms
}
}
#print "roomFileHandle = @roomFileHandle\n"; # array of room files like this Twin_012_YYYY-MM-DD
foreach my $roomFile (@roomFileHandle) {
my ($roomTypeChosen, $possibleRoomNumber, $YYYYmmDD) = split /\_/, $roomFile, 4;
#print "first available room = $availableRooms[0]\n"; #outputs 012
if ( $availableRooms[0] == $possibleRoomNumber ) { #aim to book in first available room.
#print "arn =$availableRooms[0]<br />\n"; # ouputs first available room
#print "prn =$possibleRoomNumber<br />\n"; # outputs possibleRoomNumber
print "roomFile = $roomFile\n<br />"; # outputs roomfiles where the first available room matched the possibilites.
push (@roomsToBook, $roomFile);
}
}
bazz
FishMonger
03-09-2006, 04:46 PM
when the form is submitted, it causes a re-run of this script but with a different sub.Well, from that description, the array that needs to be passed to "this other sub" is comming in from param('arrayname') not the first sub. If that's not the case, you need to give a beter description and more comprehensive example of your code.
FishMonger
03-09-2006, 04:59 PM
So how can you pass an array from sub to sub
my @array = build_array();
process_array(@array);
sub build_array {
# does some processing and builds the array
return \@privatearray;
}
sub process_array {
my @privatearray = @_;
# process the array
}
Or, instead of the return statement in the first sub, you call the second sub from with the first sub.
build_array();
sub build_array {
# does some processing and builds the array
process_array(@privatearray);
}
sub process_array {
my @privatearray = @_;
# process the array
}
Thank you very much FishMonger.:thumbsup: :thumbsup:
I shall think that through for a bit.
Bazz
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.