Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make all variables "local"

Status
Not open for further replies.

CJason

Programmer
Oct 13, 2004
223
US
Is there a way to make all variables "local"...or, do you have to declare each one as local?
 
All variables are package variables by default, unless stated otherwise. You'll have to declare them.

You should know that 'local' in Perl means something different to what people often expect. You're more than likely looking for a lexical variable, which are declared using "my".
 
Thanks for the response! FYI: Yes, I am looking for "local" as opposed to "my".
 
No problem. It would be unusual to declare all variables using 'local'. Any particular reason for it?
 
ishnid...yes, I'm trying to make my perl scripts function the exact way that DCL scripts act on VMS!?!?!? And "local" is basically how it acts.

I bet you're glad you asked [neutral]
 
Perl is not DCL so IMO there's no valid reason to want them to act the same. Do you feel the same way with bash scripts?

Declaring all of your vars with local means that you're not running under strictures, which is bad, and you may not even have warnings enabled. The combination of the strict and warnings pragmas will point out lots of mistakes/bugs.

Do yourself (and us) a favor and use the pragmas and declare your vars with my instead of local.

Just my $0.02

Ron
aka FishMonger
 
Hi Ron, Welcome to Tek-Tips. [wavey2]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I have to agree with Ron here. It's usually a big mistake to try to compile one language in another or to "migrate" code from one language to another line by line.
Your best bet is to understand what the original code was trying to do and to replicate that functionality but using the tools and strengths of the new language you are trying to migrate to.
There are plenty of really clever people here that can help you to do that.
"local" in perl is used for very specific things (e.g. managing perl "special" variables). For pure scope limiting, "my" is generally the tool of choice.



Trojan.
 
Ok ok...I understand how to write Perl the "right" way. I am writing a DCL to Perl converter...so, I don't have the luxury of doing MOST things the right way...I'm stuck with what I can do with a converter. And, hopefully, this is just going to be a stop-gap solution simply to get on a different platform.

Trojan & Ron, although I totally agree with you...there is WAY too much code to do a re-write. If I had an unlimited time and resources, OF COURSE I would to it the way that you suggested!

And...AGAIN...it is "local" I want, not "my".
 
ok CJason,
I think we get where you're coming from now.
We all have to make compromises in life and obviously you're stuck in a hole with this one.
Writing a converter like this is going to be a big task and very difficult to get right.

I wish you luck! :)


Trojan.
 
Thanks for the encouragement, Trojan!! Believe me, I need it!!!
 
Well feel free to drop back again with more questions.
The guys (and girls?) here are trying to give you the best advice they can but obviously that is limited by our understanding of your situation.
Now that we know you're trying to write a converter I'm sure you'll find subsequent advice more tailored to your situation.



Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top