vipinh has asked for the wisdom of the Perl Monks concerning the following question:

i am using use vars qw(%seg000) - where %seg000 is the hash name . this will declare it as a global hash here is a snippet of code :
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'.
(tye)RE: global hash going out of scope
by tye (Sage) on Oct 06, 2000 at 21:17 UTC

    You are doing %seg000= &ProcessSegment000(...) which tries to a assign single value to an array. This will fail. You'd know that if you checked for failure of your eval and printed $@. Though I don't think this explains your problem since that failure should come after the subroutine was called and before the hash is cleared by the assignment. If you in fact have a subroutine that returns an even number of values, then dropping "%seg000=" from your eval might well fix your problem.

    In the long run, you might be better off to take the time to figure out how to elminate the eval entirely.

            - tye (but my friends call me "Tye")
code tags (was: Re: RE: global hash going out of scope)
by merlyn (Sage) on Oct 06, 2000 at 20:44 UTC
    Hmm. Why isn't putting <code> tags more obvious for the beginners? Why do we get so many of these malformed posts?

    Maybe we need slashdot style box at the bottom: "plain text" vs "HTML"?

    Perhaps until you've deselected some option, every empty new box for you to post should include

    <code> Put your message here. If you want HTML instead of typewriter, go outside the <code> tags! < /code> (without the space, durn it {grin})

    -- Randal L. Schwartz, Perl hacker

      I fixed it but will entertatin any ideas for making posting easier for newbies.

      vroom | Tim Vroom | vroom@cs.hope.edu