Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > Perl and the DBI > subroutine - variable scope?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-18-08, 20:20
sjgrad03 sjgrad03 is offline
Registered User
 
Join Date: Aug 2003
Location: san jose, CA
Posts: 68
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
Reply With Quote
  #2 (permalink)  
Old 03-20-08, 00:41
sco08y sco08y is offline
Registered User
 
Join Date: Oct 2002
Location: Fort Polk, LA
Posts: 500
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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On