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

#!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?

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!

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.

Have you tried the perl debugger?

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.

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

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!

commented: good explanation +2

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.