For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > November 2006 > wrong dereference for returned value?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author wrong dereference for returned value?
Nikolai Försterling

2006-11-01, 7:58 am

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
John W. Krahn

2006-11-01, 6:57 pm

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/.../fm_prototypes/ 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
Nikolai Försterling

2006-11-01, 6:57 pm

Thanks for your answer.

John W. Krahn wrote:

[...]
>
> You are using prototypes. Read:
> http://library.n0i.net/programming/.../fm_prototypes/ to find
> out why you should *not* use prototypes.


[x]planned this evening :-)

[...]
>
> 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

[...][color=darkred]
>
> 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
Nikolai Försterling

2006-11-02, 7:56 am

John W. Krahn wrote:

>
> You are using prototypes. Read:
> http://library.n0i.net/programming/.../fm_prototypes/ 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.


[...]

changed to:
sub printhash(\[$%]){ ## should accept these variables as parameters
if (!defined %{$_[0]}) {die "keine Uebergabe des Hashes!\n";}
my %h = %{$_[0]};
[color=darkred]
> 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...


changed to:
QueryEventList(); ## from subfunctions.pl
while those which need Parameters now look like:
NewEvent($$); ## when two Params are expected
[color=darkred]
> 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
Mumia W. (reading news)

2006-11-02, 7:56 am

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
Nikolai Försterling

2006-11-02, 7:56 am

Mumia W. (reading news) wrote:

>
> "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?


[color=darkred]
>
> The value returned by QueryEventList is not stored anywhere.


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

>
> 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
Mumia W. (reading news)

2006-11-02, 6:57 pm

On 11/02/2006 08:26 AM, Nikolai Försterling wrote:
> Mumia W. (reading news) wrote:
>
>
> But if this is the return code, shouldn't it be transfered as a parameter to
> printhash?
>
>
>
>
> I thought that if i return $answer, this would become %{$_[0]} somewhat
> automagically.
>
>
> 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
Nikolai Försterling

2006-11-06, 9:56 pm

Mumia W. (reading news) wrote:

>
> 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]}...

>
> 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 :-)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com