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

Hi, I am prototyping a simple soap server in perl, and have run up against a problem accessing global variables. I would normally avoid them as much as I can, and pass variables, but in this circumstance I really don’t think I can. However, I can’t seem to access the global variable either. How would I do it? The code is below, in it the $count variable is the on I am playing with, and obviously, the code doesn’t actually do very much, but I just want to be able to access it. I have tried using my and the $Metaspace::count notation, but it just won’t work. Thanks in advance!
#!/usr/bin/perl -w use SOAP::Transport::HTTP; use XML::Parser; SOAP::Transport::HTTP::CGI ->dispatch_to('Metaspace') ->handle; package Metaspace; $count = 0; # snipped some irrelevant code sub ls { my ($class, $metas) = @_; my $datadir = "data"; $return = 'listing files matching '.$metas.'<br />'; opendir(DIR, $datadir) or return "can't open $datadir: $!"; while (defined($file = readdir(DIR))){ if (check_metas("$datadir/$file", $metas)){ $return .= '<a href="'.$datadir.'/'.$file.'">'.$file.'</a><br /> +'; } } closedir(DIR); return $return.$count; } sub check_metas { my ($file, $metas) = @_; return 0 unless -f $file; my $parser = new XML::Parser(ErrorContext =>2); $parser->setHandlers(Start => \&XML_start_handler, Char => \&XML_char_handler); $parser->parsefile($file); return 1; } sub XML_start_handler { $count++; } sub XML_char_handler { $count++; }

Replies are listed 'Best First'.
Re: Soap package problem
by stajich (Chaplain) on Jun 20, 2002 at 12:22 UTC
    What about
    package Bar; use vars qw($COUNT); BEGIN { $COUNT = 0;} 1; --- script test.pl -- use Bar; print "count is ", $Bar::COUNT, "\n"; $Bar::COUNT++; print "count is ", $Bar::COUNT, "\n";
    I get (perl 5.6.1)
    count is 0 count is 1
    Did something similar NOT work for you?
Re: Soap package problem
by RMGir (Prior) on Jun 20, 2002 at 11:53 UTC
    Did you try our?
    our $count;
    Edit: Of course, our is only available in relatively new perl versions...
    --
    Mike
      sorry, that didn't work either, although I hadn't checked it, no.
        Very odd... Looking at your code, it all seems sane to me.

        I'd "use strict;" and "our $count;", then see if any interesting errors pop up.

        It's possible the snipped code isn't irrelevant?

        Although I can't see how, unless you have another "package" statement somewhere in there...
        --
        Mike