Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > quick scope question

Reply
Thread Tools

quick scope question

 
 
1172948251@noid.net
Guest
Posts: n/a
 
      03-03-2007

I want to maintain my $version variable in a single place
within the script and I want it available in a BEGIN block
that is a different package from the rest of the script. Is
this possible? Here's what I mean:

---------------

#!/usr/bin/perl

BEGIN {
package Some:ackage;
my $version = '1';
print "version is $version\n";
}

my $version = $Some:ackage::version;
print ("version is $version\n"); # not initialized ???

 
Reply With Quote
 
 
 
 
Mumia W.
Guest
Posts: n/a
 
      03-03-2007
On 03/03/2007 01:03 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I want to maintain my $version variable in a single place
> within the script and I want it available in a BEGIN block
> that is a different package from the rest of the script. Is
> this possible? Here's what I mean:
>
> ---------------
>
> #!/usr/bin/perl
>
> BEGIN {
> package Some:ackage;
> my $version = '1';


Use "our" instead:

our $version = '1';

> print "version is $version\n";
> }
>
> my $version = $Some:ackage::version;
> print ("version is $version\n"); # not initialized ???
>


When you want a variable to be package scoped, use "our":

perldoc -f our
 
Reply With Quote
 
 
 
 
1172948251@noid.net
Guest
Posts: n/a
 
      03-03-2007
On Mar 3, 12:16 pm, "Mumia W." <paduille.4060.mumia.w
(E-Mail Removed)> wrote:
> On 03/03/2007 01:03 PM, (E-Mail Removed) wrote:
>
> > I want to maintain my $version variable in a single place
> > within the script and I want it available in a BEGIN block
> > that is a different package from the rest of the script. Is
> > this possible? Here's what I mean:

>
> > ---------------

>
> > #!/usr/bin/perl

>
> > BEGIN {
> > package Some:ackage;
> > my $version = '1';

>
> Use "our" instead:
>
> our $version = '1';
>
> > print "version is $version\n";
> > }

>
> > my $version = $Some:ackage::version;
> > print ("version is $version\n"); # not initialized ???

>
> When you want a variable to be package scoped, use "our":
>
> perldoc -f our


Using "our" works perfectly... Thanks!

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      03-04-2007
(E-Mail Removed) <(E-Mail Removed)> wrote:
>
> I want to maintain my $version variable in a single place
> within the script and I want it available in a BEGIN block
> that is a different package from the rest of the script. Is
> this possible? Here's what I mean:
>
> ---------------
>
> #!/usr/bin/perl
>
> BEGIN {
> package Some:ackage;
> my $version = '1';

^^
^^

See:

"Coping with Scoping":

http://perl.plover.com/FAQs/Namespaces.html


> print "version is $version\n";
> }
>
> my $version = $Some:ackage::version;
> print ("version is $version\n"); # not initialized ???
>



--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
Quick question, hopefully quick answer. ~misfit~ NZ Computing 114 01-06-2005 01:36 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM
Quick Question Quick Answer JKop C++ 11 05-24-2004 09:46 PM



Advertisments