in reply to make a hash globally accessible

i am using use vars qw(%seg000) - where %seg000 is the hash name
here is a snippet of code :<br> use vars qw(%seg000); %seg000 = (); #ONE OF THE MANY SUBROUTINES WHERE THE HASH IS ASSIGNED VAUES sub ProcessSegment000 { my $dataline = shift; $seg000{fileid} = substr($dataline,0,3); print "INSIDE the segment : " . $seg000{fileid} . "\n"; -#THIS PRINT + STATEMENT SHOWS THAT THE HASH NOW CONTAINS DATA $seg000{filetype} = substr($dataline,3,3); $seg000{fileversionid} = substr($dataline,6,5); $seg000{impactionind} = substr($dataline,11,1); $seg000{expactionind} = substr($dataline,12,1); $seg000{mornetcaseid} = substr($dataline,13,30); } sub readfile -- THIS SUB FIRES THE ABOVE SUBROUTINE { my $myfile = shift; while (!eof $myfile){ chomp($input = readline($myfile)); my $segmentno = substr($input,0,3); eval "\%seg000 = &ProcessSegment$segmentno(\$input,\\\%seg000) +"; print "OUTISDE THE SUBROUTINE " . $seg000{fileid}; #THIS PRINT STATEMENT SHOWS NO DATA IN THE HASH. } } $file="c\:\\0008022.fnm"; -the data file to be read open FILEHANDLE, "<$file"; readfile(\*FILEHANDLE);

Replies are listed 'Best First'.
RE: Re: make a hash globally accessible
by chromatic (Archbishop) on Oct 08, 2000 at 08:31 UTC
    I do not like that eval. I would rather use a hash of subroutine references or a wrapper subroutine taking the segment value or a closure scheme.

    That said, you're very close. Assigning the results of the ProcessSegmentxxx call to %seg000 would work -- if you actually returned the hash from the subroutine. Likewise, passing a reference to the hash would work -- if you actually used the reference in the subroutine to hold data.

    The code to do that would be similar to:

    sub ProcessSegment000 { my $dataline = shift; my $seg000 = shift; $seg000->{fileid} = substr($dataline,0,3); # et cetera
    Note the dereferencing arrow.

    Seriously though, I shudder to think at the kind of work you mihgt have to do to update all of those similar subs. That'll be the next thing to tackle, right?