| | |
explanation of code??
Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2006
Posts: 34
Reputation:
Solved Threads: 0
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
output:
one two three four
i am
i am
i am
i think therefore i am
Perl Syntax (Toggle Plain Text)
#!usr/bin/perl my $a = "one"; my $b = "two"; my $c = "three"; my $d = "four"; print join(" ", $a, $b, $c, $d), "\n"; print sub1($a, $b, $c, $d), "\n"; print sub2($a, $b, $c, $d), "\n"; print sub3($a, $b, $c, $d), "\n"; print join(" ", $a, $b, $c, $d), "\n"; sub sub1 { my ($var1, $var2, $var3, $var4) = @_; return sub2($var1, $var2, $var3, $var4); } sub sub2 { my $var1 = shift; my $var2 = shift; my $var3 = shift; my $var4 = shift; return sub3($var2, $var1, $var4, $var3); } sub sub3 { $_[0] = "i"; $_[1] = "think"; $_[2] = "therefore"; $_[3] = "i am"; }
output:
one two three four
i am
i am
i am
i think therefore i am
Have you tried the perl debugger?
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!
Perl Syntax (Toggle Plain Text)
perl -d script.pl
--
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
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
•
•
Join Date: Jul 2006
Posts: 34
Reputation:
Solved Threads: 0
•
•
•
•
Have you tried the perl debugger?
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!Perl Syntax (Toggle Plain Text)
perl -d script.pl
Perl Syntax (Toggle Plain Text)
DB<16> main::sub2(test.pl:25): my $var1 = shift; DB<16> main::sub2(test.pl:26): my $var2 = shift; DB<16> main::sub2(test.pl:27): my $var3 = shift; DB<16> main::sub2(test.pl:28): my $var4 = shift; DB<16> p $var1 one DB<17> main::sub2(test.pl:29): return sub3($var2, $var1, $var4, $var3); DB<17> $var2 DB<18> p $var2 two DB<19> main::sub3(test.pl:33): $_[0] = "i"; DB<19> main::sub3(test.pl:34): $_[1] = "think"; DB<19> main::sub3(test.pl:35): $_[2] = "therefore"; DB<19> main::sub3(test.pl:36): $_[3] = "i am"; DB<19> p $var2 DB<20> p $var3 DB<21> i am main::(test.pl:14): print sub3($a, $b, $c, $d), "\n"; DB<21> main::sub3(test.pl:33): $_[0] = "i"; DB<21> main::sub3(test.pl:34): $_[1] = "think"; DB<21> main::sub3(test.pl:35): $_[2] = "therefore"; DB<21> main::sub3(test.pl:36): $_[3] = "i am"; DB<21> i am main::(test.pl:17): print join(" ", $a, $b, $c, $d), "\n"; DB<21> i think therefore i am
•
•
•
•
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.
--
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
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
•
•
Join Date: Jul 2006
Posts: 34
Reputation:
Solved Threads: 0
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!
![]() |
Similar Threads
- How to set database location via code in Crystal Report XI....? (Visual Basic 4 / 5 / 6)
- Narue's Heap sort a further explanation? (C)
- Classes (C++)
- I am trying to get this code to work. Please help. (C++)
- Character counting/usage (was: Need Help with my CODE!! PLEASE HELP....) (C++)
- array declaration explanation needed (C)
- Need help in os programming (C)
- code explanation required (C)
- reading a file into code (Java)
Other Threads in the Perl Forum
- Previous Thread: How to print to file
- Next Thread: word count on a file (looked online but didn't help much)
| Thread Tools | Search this Thread |





