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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: global, write-able vars
by BrowserUk (Patriarch) on Oct 05, 2011 at 13:36 UTC | |
|
Re: global, write-able vars
by keszler (Priest) on Oct 05, 2011 at 13:58 UTC | |
|
Re: global, write-able vars
by bart (Canon) on Oct 05, 2011 at 15:26 UTC |