Parameter Passing : Recursion Problem!!! 
Author Message
 Parameter Passing : Recursion Problem!!!

We have a function like the follows

sub parse_msg
{
        local($data_cut, $actual_msg ) ;
        print $_[0], $_[1] ;

#       Some Code to parse the $_[1] argument according to info in $_[0].

        .
        .
        .
#       The $data_cut and $actual_msg get some values due to this code

        &parse_msg( $data_cut, $actual_msg ) ;

Quote:
}

The parse_msg sub is called recursively. Now the problem is that the $_[0] & $_[1]
show no values for the parse msg that is called inside.

PLEASE HELP...
SOS...
TREAT ASSURED



Fri, 02 May 1997 18:45:52 GMT  
 Parameter Passing : Recursion Problem!!!

Quote:

>We have a function like the follows

>sub parse_msg
>{
>    local($data_cut, $actual_msg ) ;
>    print $_[0], $_[1] ;

>#   Some Code to parse the $_[1] argument according to info in $_[0].

>    .
>    .
>    .
>#   The $data_cut and $actual_msg get some values due to this code

>    &parse_msg( $data_cut, $actual_msg ) ;
>}

>The parse_msg sub is called recursively. Now the problem is that the $_[0] & $_[1]
>show no values for the parse msg that is called inside.

>PLEASE HELP...
>SOS...
>TREAT ASSURED

Should work.  Are you sure $data_cut and $actual_msg are set in the
code you omitted?  Assigning to $_[0] and $_[1] within that code could
lead to similar effects.  Perl5 won't let you do that but perl4 will.

Anno



Fri, 02 May 1997 20:27:59 GMT  
 Parameter Passing : Recursion Problem!!!

Quote:

>sub parse_msg
>{
>    local($data_cut, $actual_msg ) ;
>    print $_[0], $_[1] ;
[munch]
>    &parse_msg( $data_cut, $actual_msg ) ;
>}

>The parse_msg sub is called recursively. Now the problem is that the $_[0] & $_[1]
>show no values for the parse msg that is called inside.

Yes, that makes sense. What we have here is a classical case of variable
suicide, you know, the:

&somesub($x);

sub somesub {
  local($x);

  print "$_[0]\n";

Quote:
}

stuff. The local($x) makes $x disappear, and since $_[0] is only a
reference to $x, you can't see the global $x anymore.


the VERY FIRST local() statement in the subroutine. ALWAYS. Just pretend
it's a coding style.

This should work:

sub parse_msg
{

   local($data_cut, $actual_msg);

   # munched bit goes here...

   &parse_msg( $data_cut, $actual_msg ) ;

Quote:
}
>PLEASE HELP...
don't panic...
>SOS...

the firebrigade is on it's way...
Quote:
>TREAT ASSURED

Great, a treat for this simple trick. Can you do oversea delivery of treats? :)

Cheers,
--
Jan-Pieter Cornet



Fri, 02 May 1997 21:05:33 GMT  
 Parameter Passing : Recursion Problem!!!

   We have a function like the follows

   sub parse_msg
   {
        local($data_cut, $actual_msg ) ;
        print $_[0], $_[1] ;

   #    Some Code to parse the $_[1] argument according to info in $_[0].

        .
        .
        .
   #    The $data_cut and $actual_msg get some values due to this code

        &parse_msg( $data_cut, $actual_msg ) ;
   }

   The parse_msg sub is called recursively. Now the problem is that the $_[0] & $_[1]
   show no values for the parse msg that is called inside.

this is variable suicide.  see the faq on rtfm.mit.edu
or this news group.

or use perl 5, and s/local/my/ there.

.mrg.



Sun, 04 May 1997 22:45:55 GMT  
 Parameter Passing : Recursion Problem!!!

|>
|> We have a function like the follows
|>
|>
|> sub parse_msg
|> {
|>   local($data_cut, $actual_msg ) ;
|>   print $_[0], $_[1] ;
|>
|> # Some Code to parse the $_[1] argument according to info in $_[0].
|>
|>   .
|>   .
|>   .
|> # The $data_cut and $actual_msg get some values due to this code
|>
|>   &parse_msg( $data_cut, $actual_msg ) ;
|> }
|>
|>
|> The parse_msg sub is called recursively. Now the problem is that the $_[0] & $_[1]
|> show no values for the parse msg that is called inside.

I've tried some test programs like yours.  I believe your problem is the use
of the $data_cnt and $actual_msg variables as locals.  Try using some other
variables or removing the local statement.

--
Jim Lyons                     |  Operating Systems Specialist
Computation Center            |  512-471-6331
University of Texas at Austin |
GO:d?:p+:c:l-:u:e++:m+/m++:s:n---:h----:f:!g:w:t:r-:y?



Mon, 05 May 1997 05:33:08 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Examples of permutation, illustrating recursion, parameter-passing, ...

2. Creating a Win32::Process for a batch file and passing parameters to it problem

3. Problem passing parameters through MSDOS/Windows batch file

4. passing parameter problem

5. problem passing parameter

6. parameter passing problem

7. continuation-passing and tail-recursion

8. Passing references via recursion

9. passing by value, using recursion

10. newbie's question about passing parameters to subroutine

11. (Possible bug?) Changing passed parameters

12. Pass parameters

 

 
Powered by phpBB® Forum Software