wrong dereference for returned value?

wrong dereference for returned value?

am 01.11.2006 14:28:56 von chat.foster

Hello NG,

i'm new to this group, but already did some research on this topic here in
perl.beginners and of course via google, but haven't found a solution so
far, so i try to get some help here.

I started to write some scripts and in one of them a return value of a
function is being returned as a reference ($answer) to the hash (by
Frontier::Client).
This response has to be processed by the function printhash in order to get
the result printed.

#######################################################
### subfunctions.pl ###
[...]
sub QueryEventList(){
my %queryhash = %inithash; ## predefined basic values ##
$queryhash{'COMMAND'} = "QueryEventList"; ## more additions to the hash in
other subs
my $answer = $client->call("function.call",\%queryhash );
return $answer;
}
[...]
sub printhash(){
my %h = %{$_[0]}; ## this line contains the fault ##
my $o = $_[1] ? " => $_[1]" : "";

foreach my $k (keys %h) {
if ($h{$k} =~ /HASH/) {
&printhash($h{$k}, "$o$k");
} elsif ($h{$k} =~ /ARRAY/) {
for(@{$h{$k}}) {
print "$k => $_\n";
}
} else {
print "$k => $h{$k}\n";
}
}
}
[...]
#######################################################
### main ###
[...]
{
if ($arguments >= 1){&usage() && &dmax();}
print "EreignisListe abfragen: \n";
&QueryEventList(); ## from subfunctions.pl
}
[...]
&printhash($answer);
#######################################################

But as i call &printhash($answer) via the main program, i get the error "Use
of uninitialized value in hash dereference at subfunctions.pl
line 'faultline'".
I don't know what's wrong there.
The returned value contains the server response in the reference to the
response hash ($answer), so i dereference the value returned by the
subfunction as an argument to the printhash-function (by %{$_[0]}) in order
to get the hash, but this code seems to be buggy...

Has anyone an idea what went wrong?

thanks in advance.
Niko

Re: wrong dereference for returned value?

am 01.11.2006 16:19:41 von someone

Nikolai Försterling wrote:
>
> i'm new to this group, but already did some research on this topic here in
> perl.beginners and of course via google, but haven't found a solution so
> far, so i try to get some help here.
>
> I started to write some scripts and in one of them a return value of a
> function is being returned as a reference ($answer) to the hash (by
> Frontier::Client).
> This response has to be processed by the function printhash in order to get
> the result printed.
>
> #######################################################
> ### subfunctions.pl ###
> [...]
> sub QueryEventList(){

You are using prototypes. Read:
http://library.n0i.net/programming/perl/articles/fm_prototyp es/ to find out
why you should *not* use prototypes.


> my %queryhash = %inithash; ## predefined basic values ##
> $queryhash{'COMMAND'} = "QueryEventList"; ## more additions to the hash in
> other subs
> my $answer = $client->call("function.call",\%queryhash );
> return $answer;
> }
> [...]
> sub printhash(){

Your prototype says that this subroutine accepts *NO* arguments. But you are
probably not getting a warning message about this because you apparently
always prepend '&' to all sub calls which bypasses the prototype. So why are
you using prototypes if you are just bypassing them anyways?

perldoc perlsub


> my %h = %{$_[0]}; ## this line contains the fault ##
> my $o = $_[1] ? " => $_[1]" : "";
>
> foreach my $k (keys %h) {
> if ($h{$k} =~ /HASH/) {
> &printhash($h{$k}, "$o$k");
> } elsif ($h{$k} =~ /ARRAY/) {
> for(@{$h{$k}}) {
> print "$k => $_\n";
> }
> } else {
> print "$k => $h{$k}\n";
> }
> }
> }
> [...]
> #######################################################
> ### main ###
> [...]
> {
> if ($arguments >= 1){&usage() && &dmax();}
> print "EreignisListe abfragen: \n";
> &QueryEventList(); ## from subfunctions.pl
> }
> [...]
> &printhash($answer);
> #######################################################
>
> But as i call &printhash($answer) via the main program, i get the error "Use
> of uninitialized value in hash dereference at subfunctions.pl
> line 'faultline'".

That *warning* (not error) message means that the value of the first argument
passed to printhash() is undefined. Use the defined() function to test
whether a value is defined or not.

perldoc -f defined



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall

Re: wrong dereference for returned value?

am 01.11.2006 17:04:15 von chat.foster

Thanks for your answer.

John W. Krahn wrote:

[...]
>> sub QueryEventList(){
>
> You are using prototypes. Read:
> http://library.n0i.net/programming/perl/articles/fm_prototyp es/ to find
> out why you should *not* use prototypes.

[x]planned this evening :-)

[...]
>> return $answer;
>> }
>> [...]
>> sub printhash(){
>
> Your prototype says that this subroutine accepts *NO* arguments. But you
> are probably not getting a warning message about this because you
> apparently
> always prepend '&' to all sub calls which bypasses the prototype. So why
> are you using prototypes if you are just bypassing them anyways?

I'm not that experienced in Perl since i started to write my first scripts
about two months ago and don't understand every detail so far.

> perldoc perlsub

[x]noticed and planned to study, too

>> my %h = %{$_[0]}; ## this line contains the fault ##
>> my $o = $_[1] ? " => $_[1]" : "";
[...]
>> But as i call &printhash($answer) via the main program, i get the error
>> "Use of uninitialized value in hash dereference at subfunctions.pl
>> line 'faultline'".
>
> That *warning* (not error) message means that the value of the first
> argument
> passed to printhash() is undefined. Use the defined() function to test
> whether a value is defined or not.

That value should be defined in every case as every sub gets a response
i already tried the functions without (), especially printhash.

> perldoc -f defined

If i got some more questions, i'll be back.
But a lot to read til then ;-)

> John

Thanks

Re: wrong dereference for returned value?

am 02.11.2006 10:55:19 von chat.foster

John W. Krahn wrote:

>> [...]
>> sub QueryEventList(){
>
> You are using prototypes. Read:
> http://library.n0i.net/programming/perl/articles/fm_prototyp es/ to find
> out why you should *not* use prototypes.

First, I tried to just change the sub entries to the correct(?) form, e.g.
sub test($$$) {...}, while sub QueryEventList() is correct as this function
doesn't need parameters.


[...]
>> sub printhash(){

changed to:
sub printhash(\[$%]){ ## should accept these variables as parameters
if (!defined %{$_[0]}) {die "keine Uebergabe des Hashes!\n";}
my %h = %{$_[0]};

> Your prototype says that this subroutine accepts *NO* arguments. But you
> are probably not getting a warning message about this because you
> apparently
> always prepend '&' to all sub calls which bypasses the prototype. So why
> are you using prototypes if you are just bypassing them anyways?

Additionally, i removed all "&" from the function calls in the main script,
but %{$_[0]} still seems to be dead...

>> [...]
>> #######################################################
>> ### main ###
>> [...]
>> {
>> if ($arguments >= 1){&usage() && &dmax();}
>> print "EreignisListe abfragen: \n";
>> &QueryEventList(); ## from subfunctions.pl

changed to:
QueryEventList(); ## from subfunctions.pl
while those which need Parameters now look like:
NewEvent($$); ## when two Params are expected

> That *warning* (not error) message means that the value of the first
> argument
> passed to printhash() is undefined. Use the defined() function to test
> whether a value is defined or not.

As mentioned above, printhash now always dies as there is no %{$_[0]}, but
the subfunctions should return it in $answer, which is the reference to the
response hash.
But e.g.
sub QueryEventList{
my %queryhash = %inithash;
$queryhash{'COMMAND'} = "QueryEventList";
my $answer = $client->call("function.call",\%queryhash );
return $answer;
}
should return the expected value when i call
&printhash($answer);

lg Niko

Re: wrong dereference for returned value?

am 02.11.2006 13:33:10 von paduille.4060.mumia.w

On 11/01/2006 07:28 AM, Nikolai Försterling wrote:
> Hello NG,
>
> i'm new to this group, but already did some research on this topic here in
> perl.beginners and of course via google, but haven't found a solution so
> far, so i try to get some help here.
>
> I started to write some scripts and in one of them a return value of a
> function is being returned as a reference ($answer) to the hash (by
> Frontier::Client).
> This response has to be processed by the function printhash in order to get
> the result printed.
>
> #######################################################
> ### subfunctions.pl ###
> [...]
> sub QueryEventList(){
> my %queryhash = %inithash; ## predefined basic values ##
> $queryhash{'COMMAND'} = "QueryEventList"; ## more additions to the hash in
> other subs
> my $answer = $client->call("function.call",\%queryhash );

"My $answer" makes $answer a lexical variable that is restricted to
QueryEventList. By default, $answer has no association with any other
$answer variable anywhere else in the program.

> return $answer;
> }
> [...]
> sub printhash(){
> my %h = %{$_[0]}; ## this line contains the fault ##
> my $o = $_[1] ? " => $_[1]" : "";
>
> foreach my $k (keys %h) {
> if ($h{$k} =~ /HASH/) {
> &printhash($h{$k}, "$o$k");
> } elsif ($h{$k} =~ /ARRAY/) {
> for(@{$h{$k}}) {
> print "$k => $_\n";
> }
> } else {
> print "$k => $h{$k}\n";
> }
> }
> }
> [...]
> #######################################################
> ### main ###
> [...]
> {
> if ($arguments >= 1){&usage() && &dmax();}
> print "EreignisListe abfragen: \n";
> &QueryEventList(); ## from subfunctions.pl

The value returned by QueryEventList is not stored anywhere.


> }
> [...]
> &printhash($answer);

Understandably, $answer is undefined because it hasn't been initialized
with the return value of QueryEventList.

> #######################################################
>
> But as i call &printhash($answer) via the main program, i get the error "Use
> of uninitialized value in hash dereference at subfunctions.pl
> line 'faultline'".
> I don't know what's wrong there.
> The returned value contains the server response in the reference to the
> response hash ($answer), so i dereference the value returned by the
> subfunction as an argument to the printhash-function (by %{$_[0]}) in order
> to get the hash, but this code seems to be buggy...
>
> Has anyone an idea what went wrong?
>
> thanks in advance.
> Niko

You're welcome.

--
paduille.4060.mumia.w@earthlink.net

Re: wrong dereference for returned value?

am 02.11.2006 15:26:17 von chat.foster

Mumia W. (reading news) wrote:

>> ### subfunctions.pl ###
>> [...]
>> sub QueryEventList(){
>> my %queryhash = %inithash; ## predefined basic values ##
>> $queryhash{'COMMAND'} = "QueryEventList"; ## more additions to the hash
>> in
>> other subs
>> my $answer = $client->call("function.call",\%queryhash );
>
> "My $answer" makes $answer a lexical variable that is restricted to
> QueryEventList. By default, $answer has no association with any other
> $answer variable anywhere else in the program.

But if this is the return code, shouldn't it be transfered as a parameter to
printhash?

>> return $answer;
>> }
>> [...]
>> sub printhash(){
>> my %h = %{$_[0]}; ## this line contains the fault ##
>> my $o = $_[1] ? " => $_[1]" : "";


>> [...]
>> if ($arguments >= 1){&usage() && &dmax();}
>> print "EreignisListe abfragen: \n";
>> &QueryEventList(); ## from subfunctions.pl
>
> The value returned by QueryEventList is not stored anywhere.

I thought that if i return $answer, this would become %{$_[0]} somewhat
automagically.

>> [...]
>> &printhash($answer);
>
> Understandably, $answer is undefined because it hasn't been initialized
> with the return value of QueryEventList.

Shouldn't this have happened with the declaration of "our $answer"?
Sorry, i haven't posted the whole code, so you cannot know that that script
includes "our (%inithash,%queryhash,$answer,$client);"

greetings Niko

Re: wrong dereference for returned value?

am 02.11.2006 20:01:07 von paduille.4060.mumia.w

On 11/02/2006 08:26 AM, Nikolai Försterling wrote:
> Mumia W. (reading news) wrote:
>
>>> ### subfunctions.pl ###
>>> [...]
>>> sub QueryEventList(){
>>> my %queryhash = %inithash; ## predefined basic values ##
>>> $queryhash{'COMMAND'} = "QueryEventList"; ## more additions to the hash
>>> in
>>> other subs
>>> my $answer = $client->call("function.call",\%queryhash );
>> "My $answer" makes $answer a lexical variable that is restricted to
>> QueryEventList. By default, $answer has no association with any other
>> $answer variable anywhere else in the program.
>
> But if this is the return code, shouldn't it be transfered as a parameter to
> printhash?
>
>>> return $answer;
>>> }
>>> [...]
>>> sub printhash(){
>>> my %h = %{$_[0]}; ## this line contains the fault ##
>>> my $o = $_[1] ? " => $_[1]" : "";
>
>
>>> [...]
>>> if ($arguments >= 1){&usage() && &dmax();}
>>> print "EreignisListe abfragen: \n";
>>> &QueryEventList(); ## from subfunctions.pl
>> The value returned by QueryEventList is not stored anywhere.
>
> I thought that if i return $answer, this would become %{$_[0]} somewhat
> automagically.
>
>>> [...]
>>> &printhash($answer);
>> Understandably, $answer is undefined because it hasn't been initialized
>> with the return value of QueryEventList.
>
> Shouldn't this have happened with the declaration of "our $answer"?

No, as I said above, the $answer variable in QueryEventList is trapped
within QueryEventList and has no association with any other
variable--even ones named $answer in other parts of the script.


> Sorry, i haven't posted the whole code, so you cannot know that that script
> includes "our (%inithash,%queryhash,$answer,$client);"
>
> greetings Niko

Since $answer was already declared "our," you shouldn't have declared it
with "my" in QueryEventList.

Use debugging code (print statements) to help you find out what is
really in $answer before you call printhash, e.g.

print "The value of answer is $answer\n";
printhash($answer);

In fact, you should put a print statement in QueryEventList too. Put in
the debugging statements and test, then remove the "my" before $answer
in QueryEventList and test.


--
paduille.4060.mumia.w@earthlink.net

Re: wrong dereference for returned value?

am 06.11.2006 17:51:56 von chat.foster

Mumia W. (reading news) wrote:

>> Shouldn't this have happened with the declaration of "our $answer"?
>
> No, as I said above, the $answer variable in QueryEventList is trapped
> within QueryEventList and has no association with any other
> variable--even ones named $answer in other parts of the script.

I thought that the return of $answer automatically gets read in printhash's
input variable %{$_[0]}...

>> Sorry, i haven't posted the whole code, so you cannot know that that
>> script includes "our (%inithash,%queryhash,$answer,$client);"
>
> Since $answer was already declared "our," you shouldn't have declared it
> with "my" in QueryEventList.

Huh, I had to d o something like this, cause the program is splitted into
four files and though I used "use 'vars'" and "require 'other_file'", the
interpreter couldn't find the values declared in the other files, so I
Thought I had to declare them in all other files, too.

> Use debugging code (print statements) to help you find out what is
> really in $answer before you call printhash, e.g.
>
> print "The value of answer is $answer\n";
> printhash($answer);
>
> In fact, you should put a print statement in QueryEventList too. Put in
> the debugging statements and test, then remove the "my" before $answer
> in QueryEventList and test.

I'm afraid you're right and the previous errors mentioned above have been
caused by other mistakes in the code and that the our-declaration of
$answer hasn't been necessary.

Though not really a solution, i bypassed the problem by doing the following
(in the subs file):
[...]
sub QueryEventList{
my %queryhash = %inithash;
$queryhash{'COMMAND'} = "QueryEventList";
my $answer = $client->call("api.call",\%queryhash );
if (defined $answer) {&printhash($answer);} ## <--
else {&noanswer;}
[...]
The return code ($answer) is no longer needed and now it works as intended.
Next thing to is a frontend for it :-/
Some sleepless nights again...

Thanks for your help and patience :-)