Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

global, write-able vars

by zeltus (Sexton)
on Oct 05, 2011 at 13:30 UTC ( #929797=perlquestion: print w/ replies, xml ) Need Help??
zeltus has asked for the wisdom of the Perl Monks concerning the following question:

I really ought to be able to answer this for myself but it's turning out to be a long week and what's left of my brain is half-fried already.

Anyway, all I want to do is declare a global variable in the main part of my programand then manipulate it within subroutines.

This fragment shows what I am trying to do. But it fails when I try to increment the value withing the sub "modification of a read-only value..."

#/usr/bin/perl $tt = 1; mt($tt); sub mt { my $t = shift; print " sub: 1 t = ${$t}\n"; ${$timer}++; print " sub: 2 t = ${$t}\n"; }

Once I have understood (with someone's help, hopefully) what I need to do to make this work, I'd like to ensure it works with "use warnings, strict" and with sub prototyping... and anything else I can think of the keep it under some sort of control.

As I said, I should be able to do this by now. I have worked on PHP code for too long I guess!

Thanks for any help

Bill

Update

Whoopsy! Yes, I did mean $t throughout. This is just the simplest code I could write off the fly to try and demonstrate what I am trying to do. I always use strict and warnings in my "proper" code - this is just some QnD to demo my problemette.

The reason I can't use it as a "real" global and deal with it directly in the subroutine is that I have x number or variables to maintain with values, not just the one in this example. And I do like to use subroutine prototyping

But thanks for the replies... I can now see where I was screwing up and I am happier now I understnad why I was screwing up. Still not entirely sure this is The Right Way To Program tho'! :-)

Bill

Comment on global, write-able vars
Download Code
Re: global, write-able vars
by BrowserUk (Pope) on Oct 05, 2011 at 13:36 UTC

    Quite how you think that  ${$timer}++; is going to work when the local var is called $t and the global var $tt?

    The first thing you need to do is get (back?) into the habit of enabling strict & warnings.

    The (one) way to achieve what you want is to pass a reference to the variable:

    #/usr/bin/perl $tt = 1; mt( \$tt ); sub mt { my $t = shift; print " sub: 1 t = ${$t}\n"; ${ $t }++; print " sub: 2 t = ${$t}\n"; } __END__ C:\test>junk55 sub: 1 t = 1 sub: 2 t = 2

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: global, write-able vars
by keszler (Priest) on Oct 05, 2011 at 13:58 UTC

    I don't recommend this, but if you purposely use a global variable why bother passing it to a subroutine? Global is global.

    use strict; use warnings; my $global = 1; mt(); print "C: global still = $global\n"; sub mt { print "A: global=$global\n"; $global++; print "B: global=$global\n"; } __END__ A: global=1 B: global=2 C: global still = 2
Re: global, write-able vars
by bart (Canon) on Oct 05, 2011 at 15:26 UTC
    @_ contaisn aliases to the parameters you pass, so if you work directly on $_[0], yo ucan change its value. It's the
    my $t = shift;
    that makes a copy.

    There are several ways to make a variable alias to an existing value, but, shame on Perl, none are quite what I'd call "easy". For example:

    *t = \($_[0]);
    which uses a global variable $t. With local you can restrict the damage, but it'll still not pass strict — you still need to declare $t – but not using "my"!

    another way is using for in a non-obious way (for can make a loop variable that is an alias for each item it iterates over, and with a scalar it will loop exactly one):

    for my $t ($_[0]) { # ... the rest of the sub body }

    There is even a few Alias modules on CPAN, the most interesting one always looked to me to be Data::Alias but I must confess I've never used it. (I tried to, long time ago, but it wouldn't compile back then.) Actually, looking at it now, I think Lexical::Alias might be more interesting — if it works.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://929797]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2014-11-26 06:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (163 votes), past polls