LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 01:32 PM   #1
PoleStar
Member
 
Registered: Jul 2010
Posts: 231

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, 01:44 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
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, 01:59 PM   #3
PoleStar
Member
 
Registered: Jul 2010
Posts: 231

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, 02:01 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,753

Rep: Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983
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, 02:04 PM   #5
PoleStar
Member
 
Registered: Jul 2010
Posts: 231

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, 02:23 PM   #6
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
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, 02:38 PM   #7
PoleStar
Member
 
Registered: Jul 2010
Posts: 231

Original Poster
Rep: Reputation: 2
oh

Its so much like C.

Thanks alot.
 
Old 10-01-2010, 09: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



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 09:30 PM
Perl - Can't use string ("html") as an ARRAY ref while "strict refs" OldGaf Programming 9 08-11-2009 11:14 AM
"Failed Dependency error" while installing RPM for "DateTime" perl modules giridhargopal.cj Linux - Newbie 7 11-19-2008 12:05 AM
problem "make"ing gtk+ "/usr/bin/env: perl -w" caid Linux - Newbie 8 07-29-2005 04:51 AM
LFS 4.1: Stalled at Perl, "missing seperator" error from "make" SparceMatrix Linux From Scratch 1 06-07-2003 03:31 PM

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

All times are GMT -5. The time now is 07:20 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
Open Source Consulting | Domain Registration