School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,450 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,590 people online right now. Registration is fast and FREE... Join Now!




share variable

 

share variable

Sun751

19 Jun, 2009 - 03:47 AM
Post #1

D.I.C Head
**

Joined: 11 Dec, 2008
Posts: 57



Thanked: 1 times
My Contributions
CODE

my $var;

ini($var);
prn($var);
sub ini
{
    my $var1 = shift;
    $var1 = 1;
}

sub prn
{
    my $var2 = shift;
    if($var2)
    {
        print "Variable is defined";
    }
}


In above code I am trying to get the value of variable "$var" which is initialize in sub ini, but its not working
could any one tell me how to make it work, problem is i don't want to have global variable and want seperate
subroutine to initialize;

Any suggestion welcome!!!

Cheers!!!


User is offlineProfile CardPM
+Quote Post


KevinADC

RE: Share Variable

19 Jun, 2009 - 01:02 PM
Post #2

D.I.C Regular
Group Icon

Joined: 23 Jan, 2007
Posts: 401



Thanked: 25 times
Dream Kudos: 50
My Contributions
Not advocating you do this but I'm trying to stick with the code you have:

CODE

my $var = ini();
prn($var);
sub ini
{
    my $var1 = shift || 1;
    return $var1;
    
}

sub prn
{
    my $var2 = shift;
    if($var2)
    {
        print "Variable is defined";
    }
}

User is offlineProfile CardPM
+Quote Post

dsherohman

RE: Share Variable

20 Jun, 2009 - 06:30 AM
Post #3

D.I.C Head
**

Joined: 29 Mar, 2009
Posts: 184



Thanked: 35 times
My Contributions
QUOTE(Sun751 @ 19 Jun, 2009 - 11:47 AM) *

In above code I am trying to get the value of variable "$var" which is initialize in sub ini, but its not working
could any one tell me how to make it work, problem is i don't want to have global variable and want seperate
subroutine to initialize;

You've almost got it, actually. You just need to pass $var into ini() by reference instead of by value:

CODE

my $var;

ini(\$var);
prn($var);
sub ini
{
    my $var1 = shift;
    $$var1 = 1;  # Note $$ instead of $
}

sub prn
{
    my $var2 = shift;
    if(defined $var2)
    {
        print "Variable is defined";
    }
}

The reason for the $$ instead of $ on the marked line is because $var1 is now a reference to $var and you want to change the value of the referenced variable ($var), not the value of the reference itself ($var1). If you're familiar with C, you can think of references as being more-or-less like pointers.

Also note that I changed "if($var2)" to "if(defined $var2)" in prn() so that you'll still get the "Variable is defined" output when $var2 is 0 or an empty string (both of which are defined, but false).

A simpler way to do this, if you're not comfortable with references, would be to just return the value from ini and assign it to $var from the calling scope:
CODE

my $var;

$var = ini();  # Need parens because ini() isn't defined yet
prn($var);
sub ini
{
    return 1;
}

sub prn
{
    my $var2 = shift;
    if(defined $var2)
    {
        print "Variable is defined";
    }
}


User is offlineProfile CardPM
+Quote Post

KevinADC

RE: Share Variable

20 Jun, 2009 - 01:18 PM
Post #4

D.I.C Regular
Group Icon

Joined: 23 Jan, 2007
Posts: 401



Thanked: 25 times
Dream Kudos: 50
My Contributions
I think the general rule is to stay away from soft references. I would post an example using a hard reference instead but really I don't think this person is ready for references yet at all. It looks like they are trying to get their head around variable scoping still.
User is offlineProfile CardPM
+Quote Post

Ed_Bighead

RE: Share Variable

20 Jun, 2009 - 02:39 PM
Post #5

D.I.C Head
Group Icon

Joined: 26 Apr, 2009
Posts: 166



Thanked: 14 times
Dream Kudos: 50
My Contributions
Could you post it somewhere then? I'd like to see it.
User is offlineProfile CardPM
+Quote Post

KevinADC

RE: Share Variable

20 Jun, 2009 - 06:18 PM
Post #6

D.I.C Regular
Group Icon

Joined: 23 Jan, 2007
Posts: 401



Thanked: 25 times
Dream Kudos: 50
My Contributions
QUOTE(Ed_Bighead @ 20 Jun, 2009 - 02:39 PM) *

Could you post it somewhere then? I'd like to see it.


Maybe I shot my mouth off too soon. $$var evidently is a hard reference to a scalar. In some small defense of myself, it is very unusual to create a reference to a scalar, so I jumped the gun when I looked at the code. Apology to dsherohman.

This post has been edited by KevinADC: 20 Jun, 2009 - 06:19 PM
User is offlineProfile CardPM
+Quote Post

chorny_cpan

RE: Share Variable

25 Jun, 2009 - 04:11 AM
Post #7

New D.I.C Head
Group Icon

Joined: 13 May, 2009
Posts: 35


Dream Kudos: 25
My Contributions
CODE
use strict;use warnings;use diagnostics;
use Scalar::Alias;

my $var;

ini($var);
prn($var);
sub ini
{
    my alias $var1 = shift;
    $var1 = 1;
}

sub prn
{
    my $var2 = shift;
    if($var2)
    {
        print "Variable is defined";
    }
}


This post has been edited by chorny_cpan: 25 Jun, 2009 - 04:12 AM
User is offlineProfile CardPM
+Quote Post

dsherohman

RE: Share Variable

26 Jun, 2009 - 04:27 AM
Post #8

D.I.C Head
**

Joined: 29 Mar, 2009
Posts: 184



Thanked: 35 times
My Contributions
QUOTE(KevinADC @ 21 Jun, 2009 - 02:18 AM) *

Maybe I shot my mouth off too soon. $$var evidently is a hard reference to a scalar. In some small defense of myself, it is very unusual to create a reference to a scalar, so I jumped the gun when I looked at the code. Apology to dsherohman.

I was kind of wondering where you saw a soft reference, yeah... smile.gif

I do agree that references may be a little advanced for some, but the question here was about how to pass a variable into a sub and have the sub change the value held by that variable in the calling scope. Accomplishing this pretty much demands the use of references. (Or Scalar::Alias, which I'd not heard of until chorny_cpan's post. Have to take a look at that...)
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 01:51AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month