Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-23 13:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found