CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Scope of 'our'

 



perlwatcher
User

Jun 26, 2009, 1:00 AM

Post #1 of 8 (1111 views)
Scope of 'our' Can't Post

Hi I have following ... two perl modules and one perl script which uses one of the two modules ... all three put in different files.

package arun;
require Exporter;
our @ISA = qw (Exporter);
our @EXPORT;
#our $value = 10;
push @EXPORT, qw(add);

our sub add{

my $first = shift;
my $second = shift;
return ( $first+$second);
}

1;
+++++++++++++++++++++++++++++++++++++++++++
End of module 1
+++++++++++++++++++++++++++++++++++++++++++

package arun1;
use arun;

sub AUTOLOAD{

print "Can't find the method you looked for hence came here\n";

}
1;
++++++++++++++++++++++++++++++++++++++++++
End of module 2

++++++++++++++++++++++++++++++++++++++++++


use arun1;
print add(5,6);
++++++++++++++++++++++++++++++++++++

End of script

+++++++++++++++++++++++++++++++++++


My question whether the add function in the script will be identified or not?



Any help would really be great
Thanks in advance.


1arryb
User

Jun 26, 2009, 8:11 AM

Post #2 of 8 (1095 views)
Re: [arunbhargav] Scope of 'our' [In reply to] Can't Post

Hi arunbhargav,

"our" variables are global, but only within the scope of the current package. To make your example work, you need to import and re-export add from the arun package in arun1:

Code
package arun1; 
use arun;
use Exporter;
use base qw(Exporter);
our @EXPORT = @arun::EXPORT;

sub AUTOLOAD{

print "Can't find the method you looked for hence came here\n";

}
1;


However, since it appears you are groping for inheritence, why not go the OO way and turn arun and arun1 into classes?


Code
$ cat arun.pm 
#!/usr/bin/perl
package arun;

use strict;
use warnings;

sub new {
my $class = shift;

return bless {}, ref($class) || $class;
}

sub add {
my ($self, $first, $second) = @_;
return ($first + $second);
}

1;

$ cat arun1.pm
#!/usr/bin/perl
package arun1;

use strict;
use warnings;

use arun;
use base qw(arun);

sub new {
my $class = shift;

return bless {}, ref($class) || $class;
}

sub AUTOLOAD{

print "Can't find the method you looked for hence came here\n";

}
1;

$ cat arun.pl
#!/usr/bin/perl

use strict;
use warnings;

use arun1;

my $sum = arun1->add(5,6);
print "sum=$sum\n";

# Note: you could also instantiate an arun1 object but this
# isn't necessary since your arun and arun1 classes don't
# maintain any state.
my $a = arun1->new();
$a->add(2,2);
print "sum=$sum\n";


Cheers,

Larry


(This post was edited by 1arryb on Jun 26, 2009, 8:17 AM)


perlwatcher
User

Jun 26, 2009, 10:51 AM

Post #3 of 8 (1081 views)
Re: [1arryb] Scope of 'our' [In reply to] Can't Post

Thanks larry for your patience in answering my query,

I have one more query to extend the same question.

In a package say for example what is the difference between

package xyz;

my $var;

&

package xyz;

our $var;

in either of the cases isnt $var global across the package ?
Please forgive if the question too basic


1arryb
User

Jun 26, 2009, 11:15 AM

Post #4 of 8 (1075 views)
Re: [arunbhargav] Scope of 'our' [In reply to] Can't Post

Hi arunbhargav,

In one sense, there is no difference: both our and my variables are lexically scoped within the file/block they are declared. That is, the declaration has to be "above" and "outside" the place in the code you use them or you get a compiler error. The difference is that our variables are added to the symbol table of the package wherein they are defined, which means you can get at them from outside the package:

Code
$ cat varscope1.pm 
#!/usr/bin/perl

package varscope1;

use strict;
use warnings;

my $localvar = "whatever";
our $pkgvar = "whichever";

# both $localvar and $pkgvar are defined for the rest of foo.pm, but only $pkgvar is visible from other packages.

1;

$ cat varscope1.pl
#!/usr/bin/perl

use strict;
use warnings;

use varscope1;

print "$varscope1::pkgvar\n"; #ok.
print "$varscope1::localvar\n"; # not ok.


If you run varscope1.pl you'll get:

Code
Name "varscope1::localvar" used only once: possible typo at varscope1.pl line 9. 
whichever
Use of uninitialized value $varscope1::localvar in concatenation (.) or string at varscope1.pl line 9.


Cheers,

Larry


perlwatcher
User

Jun 26, 2009, 12:02 PM

Post #5 of 8 (1071 views)
Re: [1arryb] Scope of 'our' [In reply to] Can't Post

now that clarifies my doubt
I have also tried the following

Code
 
my $var;

sub func{

local $var = 25;

print "value in func is : $var \n";

func1($var);
}

sub func1{

my $value = shift;

print " Value in Func1 is :$value \n";

}

&func;


which throws an error that it " Cant localize $var in func()"

Can you help me know why is it ?


1arryb
User

Jun 26, 2009, 12:36 PM

Post #6 of 8 (1066 views)
Re: [arunbhargav] Scope of 'our' [In reply to] Can't Post

Hi arunbhargav,

Gee, I was hoping you wouldn't bring up 'local' Smile.

The local keyword "localizes" a global variable by temporarily giving it an entry in the local scope. When the localized variable falls out of scope (of the block where local was called) then the variable reverts to its global value.

In your example, $var is declared 'my', which means it isn't part of any package symbol table, so it can't be localized. To fix it, declare $var as 'our'.

However, you really shouldn't use local unless you need to override one of the builtin Perl globals. In fact, if you feel the need to localize one of your own variables, it is an indication that your code should be refactored.

Cheers,

Larry


(This post was edited by 1arryb on Jun 26, 2009, 12:40 PM)


perlwatcher
User

Jun 26, 2009, 12:38 PM

Post #7 of 8 (1064 views)
Re: [1arryb] Scope of 'our' [In reply to] Can't Post

Thanks larry , thats lot of information at one go.Smile


KevinR
Veteran


Jun 26, 2009, 12:42 PM

Post #8 of 8 (1060 views)
Re: [arunbhargav] Scope of 'our' [In reply to] Can't Post

Edit.... Larry beat me to it .

local() is for use with perls system variables, like $_ and @_, and for other predefined variables like $\, but its not for private variables you package with "my". What you want to do is just use "my" again to declare a private variable local to a block :


Code
my $var = 25;  

sub func{

my $var = shift;

print "value in func is : $var \n";

func1($var);
}

sub func1{

my $value = shift;

print " Value in Func1 is :$value \n";

}

func($var);


Of course $var will have the same value in each block since you are not changing it, just printing it.
-------------------------------------------------


(This post was edited by KevinR on Jun 26, 2009, 12:44 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives