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


In reply to global, write-able vars by zeltus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.