Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2003
    Location
    san jose, CA
    Posts
    68

    Unanswered: subroutine - variable scope?

    hello everyone:
    I want to pass two numbers to a subroutine. within the subroutine add two numbers and return the sum to calling function.

    I have two files my_main.pl and my_sub.pl. the parameter's value didn't pass to the subroutine. sum's value is always
    zero.

    I think i may not understood the value scope well in perl. Please check my code and tell me how can I make this work.
    Thanks for your help!

    my_main.pl
    PHP Code:
      #!/usr/bin/perl
     
      
      
    use strict;
      use 
    warnings;
      
      require 
    "my_sub.pl";
      
       
    my $x 5;
       
    my $y 7;
      
      
    my $result addition($x$y);
      print 
    $result

    my_sub.pl
    PHP Code:
     #!/usr/bin/perl
     #a sub-routine used in my_main.pl
     
     #Illegal character in prototype for main::addition $a, $b at my_sub.pl line 11
     #
     
     
    use strict;
     use 
    warnings;
     
     
    my $num1 0;
     
    my $num2 0;
     
     
    sub addition($num1,$num2)
     {
       
    my $answer $num1 $num2;
       print 
    "\$answer is: $answer.\n";
       return 
    $answer;
       
     }
     
    1

  2. #2
    Join Date
    Oct 2002
    Location
    Baghdad, Iraq
    Posts
    697
    What you're trying to do here:

    sub addition($num1,$num2)

    is like saying Sub Addition(num1, num2) in VB or some such language. Those are known as formal parameters or dummy variables.

    In perl, parameters are passed through a special array called @_. (There's another variable called $_, but it's a different variable. In perl, the @ and $ are part of the variable name.)

    Essentially, when you call addition(2, 3), the list (2, 3) is assigned to @_.

    So to get what you're looking for, the typical way to do it is:

    Code:
    sub addition {
        my($num1, $num2) = @_;
        my $answer = $num1 + $num2;
        print "$answer\n";
        return $answer;
    }
    The "my" statement places a variable in the surrounding lexical scope, which usually means the surrounding curly braces.

    But you could also do this:

    Code:
    sub addition {
        return $_[0] + $_[1];
    }
    Since @_ is a regular array, you can look up its values. You can even use shift, pop and splice on it.

    Perl can simulate named parameters, too:

    Code:
    print fraction(numerator=>7, denominator=>3);
    
    sub denominator {
        my(%params) = @_;
        return $params{numerator} / $params{denominator};
    }
    If you want to require that sub addition be called with two scalar parameters, you can specify that like so:

    Code:
    sub addition($$) {
        # same as before
        ....
    }
    That's known as a prototype. They don't do quite what people with assumptions from other languages expect, but they are useful. They also don't guarantee that the sub is called that way, you can bypass a prototype with an ampersand before the sub name.

    And exercise caution if you add them to existing code.

    Further reading.
    Last edited by sco08y; 03-20-08 at 00:46.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •