LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-01-2010, 02:32 PM   #1
PoleStar
Member
 
Registered: Jul 2010
Posts: 170

Rep: Reputation: 2
perl "my" problem


Good day,


If I declare some thing in a loop like

Code:
use strict;

if($val == 1){

my $temp = $val;

}

.... 

some more loops


print "$temp";

It tells me

"Global symbol "$temp" requires explicit package name at..."



Any ideas ?
 
Old 10-01-2010, 02:44 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 613Reputation: 613Reputation: 613Reputation: 613Reputation: 613Reputation: 613
Since you declared it within a block, it does not exist outside of that block. Declare/initialize it before the if statement.

http://en.wikipedia.org/wiki/Scope_(programming)
 
Old 10-01-2010, 02:59 PM   #3
PoleStar
Member
 
Registered: Jul 2010
Posts: 170

Original Poster
Rep: Reputation: 2
So can I make macro othe top of program, #define style?


Problem is this loop is going to run through many entries.
And I want to keep certain variables, even when the loop ends.
 
Old 10-01-2010, 03:01 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,262

Rep: Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440Reputation: 4440
Quote:
Originally Posted by PoleStar View Post
So can I make macro othe top of program, #define style?

Problem is this loop is going to run through many entries.
And I want to keep certain variables, even when the loop ends.
Then don't use a "my" on it.

Using "my" means that the particular chunk doesn't exist outside that loop. Leaving it off will define it globally, so other loops can use it.
 
Old 10-01-2010, 03:04 PM   #5
PoleStar
Member
 
Registered: Jul 2010
Posts: 170

Original Poster
Rep: Reputation: 2
So you suggest I don't use "strict" ?


Because when I take of my from the loop/block
It bring back same error on that line.
 
Old 10-01-2010, 03:23 PM   #6
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 613Reputation: 613Reputation: 613Reputation: 613Reputation: 613Reputation: 613
No, I suggest you do this.

Code:
use strict;

my $temp = 0;

if($val == 1){

$temp = $val;

}

.... 

some more loops


print "$temp";
That way $temp is still in scope when you print it.
 
1 members found this post helpful.
Old 10-01-2010, 03:38 PM   #7
PoleStar
Member
 
Registered: Jul 2010
Posts: 170

Original Poster
Rep: Reputation: 2
oh

Its so much like C.

Thanks alot.
 
Old 10-01-2010, 10:04 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by PoleStar View Post
Its so much like C.
Fun reading
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl: how to save an e-mail attachment on disk keeping the "&" character (no "%26"!!) d1s4st3r Programming 5 09-29-2010 10:30 PM
Perl - Can't use string ("html") as an ARRAY ref while "strict refs" OldGaf Programming 9 08-11-2009 12:14 PM
"Failed Dependency error" while installing RPM for "DateTime" perl modules giridhargopal.cj Linux - Newbie 7 11-19-2008 01:05 AM
problem "make"ing gtk+ "/usr/bin/env: perl -w" caid Linux - Newbie 8 07-29-2005 05:51 AM
LFS 4.1: Stalled at Perl, "missing seperator" error from "make" SparceMatrix Linux From Scratch 1 06-07-2003 04:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration