explanation of code??

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2006
Posts: 34
Reputation: msaenz is an unknown quantity at this point 
Solved Threads: 0
msaenz msaenz is offline Offline
Light Poster

explanation of code??

 
0
  #1
Dec 10th, 2007
Hi I have this code here and I was wondering if anyone can explain to me how it works... I see the output and all but I get lost from sub2 to sub3

  1. #!usr/bin/perl
  2. my $a = "one";
  3. my $b = "two";
  4. my $c = "three";
  5. my $d = "four";
  6.  
  7. print join(" ", $a, $b, $c, $d), "\n";
  8. print sub1($a, $b, $c, $d), "\n";
  9. print sub2($a, $b, $c, $d), "\n";
  10. print sub3($a, $b, $c, $d), "\n";
  11. print join(" ", $a, $b, $c, $d), "\n";
  12.  
  13. sub sub1 {
  14. my ($var1, $var2, $var3, $var4) = @_;
  15. return sub2($var1, $var2, $var3, $var4);
  16. }
  17.  
  18. sub sub2 {
  19. my $var1 = shift;
  20. my $var2 = shift;
  21. my $var3 = shift;
  22. my $var4 = shift;
  23. return sub3($var2, $var1, $var4, $var3);
  24. }
  25.  
  26. sub sub3 {
  27. $_[0] = "i";
  28. $_[1] = "think";
  29. $_[2] = "therefore";
  30. $_[3] = "i am";
  31.  
  32. }

output:
one two three four
i am
i am
i am
i think therefore i am
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 8
Reputation: iaindb is an unknown quantity at this point 
Solved Threads: 0
iaindb's Avatar
iaindb iaindb is offline Offline
Newbie Poster

Re: explanation of code??

 
0
  #2
Dec 10th, 2007
Have you tried the perl debugger?
  1. perl -d script.pl
Then step through with 's' and look at your variables with 'p $var1' for example. It's quite powerful and it can tell you exactly what's going on. See how you go and post back if your still confused!
--
Perfection is reached, not when there is no longer anything to add, but when there is no longer anything to take away.
-- Antoine de Saint-Exupery
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 34
Reputation: msaenz is an unknown quantity at this point 
Solved Threads: 0
msaenz msaenz is offline Offline
Light Poster

Re: explanation of code??

 
0
  #3
Dec 10th, 2007
i havent i will try and see if i can get it.. or at least explain what i am not getting to you thanks will check back in a few.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 34
Reputation: msaenz is an unknown quantity at this point 
Solved Threads: 0
msaenz msaenz is offline Offline
Light Poster

Re: explanation of code??

 
0
  #4
Dec 10th, 2007
Originally Posted by iaindb View Post
Have you tried the perl debugger?
  1. perl -d script.pl
Then step through with 's' and look at your variables with 'p $var1' for example. It's quite powerful and it can tell you exactly what's going on. See how you go and post back if your still confused!
So what I got from it was this.... the variables a,b,c,d do not change from sub1 and sub2 but by subroutine three it only returns the last thing which is 'i am' for each since one subroutine calls the other... however I dont really quite understand how a,b,c,d became i think therefore I am is it b/c $_[0]=i and since the default @_ (a,b,c,d) it overwrites the previous variables??? I just wanted to make sure my understanding is correct. And thanks for the debugging tip I didnt know how to use it on a script.


  1. DB<16>
  2. main::sub2(test.pl:25): my $var1 = shift;
  3. DB<16>
  4. main::sub2(test.pl:26): my $var2 = shift;
  5. DB<16>
  6. main::sub2(test.pl:27): my $var3 = shift;
  7. DB<16>
  8. main::sub2(test.pl:28): my $var4 = shift;
  9. DB<16> p $var1
  10. one
  11. DB<17>
  12. main::sub2(test.pl:29): return sub3($var2, $var1, $var4, $var3);
  13. DB<17> $var2
  14.  
  15. DB<18> p $var2
  16. two
  17. DB<19>
  18. main::sub3(test.pl:33): $_[0] = "i";
  19. DB<19>
  20. main::sub3(test.pl:34): $_[1] = "think";
  21. DB<19>
  22. main::sub3(test.pl:35): $_[2] = "therefore";
  23. DB<19>
  24. main::sub3(test.pl:36): $_[3] = "i am";
  25. DB<19> p $var2
  26.  
  27. DB<20> p $var3
  28.  
  29. DB<21>
  30. i am
  31. main::(test.pl:14): print sub3($a, $b, $c, $d), "\n";
  32. DB<21>
  33. main::sub3(test.pl:33): $_[0] = "i";
  34. DB<21>
  35. main::sub3(test.pl:34): $_[1] = "think";
  36. DB<21>
  37. main::sub3(test.pl:35): $_[2] = "therefore";
  38. DB<21>
  39. main::sub3(test.pl:36): $_[3] = "i am";
  40. DB<21>
  41. i am
  42. main::(test.pl:17): print join(" ", $a, $b, $c, $d), "\n";
  43. DB<21>
  44. i think therefore i am
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 8
Reputation: iaindb is an unknown quantity at this point 
Solved Threads: 0
iaindb's Avatar
iaindb iaindb is offline Offline
Newbie Poster

Re: explanation of code??

 
1
  #5
Dec 10th, 2007
Originally Posted by msaenz View Post
So what I got from it was this.... the variables a,b,c,d do not change from sub1 and sub2
Yes, but why? read about shift and my in the perl docs.
but by subroutine three it only returns the last thing which is 'i am' for each since one subroutine calls the other...
but why does it return 'i am' the first 3 times? I actually missed this until I looked up return in the docs. Read what happens when sub3 doesn't explicitly say return.
however I dont really quite understand how a,b,c,d became i think therefore I am is it b/c $_[0]=i and since the default @_ (a,b,c,d) it overwrites the previous variables???
Yes, editing @_ and $_ will operate on the passed in parameters. But since sub1 and sub2 take local copies, only calling sub3 directly actually changes the original $a..$d.
I just wanted to make sure my understanding is correct. And thanks for the debugging tip I didnt know how to use it on a script.
no worries. I generally don't like debuggers, I can never figure out how to use them, but sometimes they give you the right info!
--
Perfection is reached, not when there is no longer anything to add, but when there is no longer anything to take away.
-- Antoine de Saint-Exupery
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 34
Reputation: msaenz is an unknown quantity at this point 
Solved Threads: 0
msaenz msaenz is offline Offline
Light Poster

Re: explanation of code??

 
0
  #6
Dec 11th, 2007
okay shift pops the element that has the lowest index which is zero so if I did $number=shift(@numberarrays) $number will be assigned one. Also it returns Iam for sub3 b/c if thereis not return statement it just returns the last thing calculated. And my makes a variable be lexically scoped.. compare to locally or globally. Thanks for your help!!!!!!!!! I appreciate you breaking this down for me.. it help tremendously. Have a good one!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC