LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2004, 03:40 PM   #1
whohasit
Member
 
Registered: Nov 2003
Location: Seattle
Distribution: Fedora Core 1
Posts: 34

Rep: Reputation: 15
simple Perl Question


This issue is driving me crazy since I declared 2 old variables as 'my'.

Everything worked under dynamic assignment (and using 'our').

Here is a more basic example.... can you tell me why test2 (using 'my') does not print "hello"?


#!/usr/local/bin/perl

&test1();
&test2();

sub test1 {
our($x,$y)=(); # using 'our'
$x='hello';
$y='x';
print "$$y\n"; # prints hello
}

sub test2 {
my($a,$b)=(); # using 'my'
$a='hello';
$b='a';
print "$$b\n"; # prints nothing!!
}


Thanks in advance for your sanity.


-w
 
Old 11-03-2004, 04:29 PM   #2
secesh
Senior Member
 
Registered: Sep 2004
Location: Savannah, GA
Distribution: Ubuntu, Gentoo, Mythbuntu, ClarkConnect
Posts: 1,154

Rep: Reputation: 47
i like perl... i've played with references before, but never quite like this...

here's what i came up with; the best i can think is it's a scope issue, but i can't explain it...

Code:
#!/usr/bin/perl

#use strict;

&test1;
&test2;
&test3;

sub test1 {
    our($x,$y)=('hello',\$x);
    print "$$y\n";
    ##WORKS
}
sub test2 {
    my($a,$b)=('hello',\$a);
    print "$$b\n";
    ##NOTHING
}
sub test3 {
    my $a = 'hello';
    my $b = \$a;
    print "$$b\n";
    ##WORKS
}
 
Old 11-05-2004, 08:44 AM   #3
whohasit
Member
 
Registered: Nov 2003
Location: Seattle
Distribution: Fedora Core 1
Posts: 34

Original Poster
Rep: Reputation: 15
Resolved.

Thanks for your comments and effort.

Explained:

This *is* a scoping issue.

The symbolic references (variable representing a variable) is usually a bad idea and will be detected/errored using strict refs pragma. Symbolic references look to global (package) scope but our declarations and work above has happend with 'my', or lexical scope. Hence, these worked fine when 'my' was not used. --- In summary, hard references ($a_ref = \$a) better "mean what they say" while symbolic references (as I've heard) can better do damage from a far.

Anyway, Hashes should meet/replace this particular requirement.
 
Old 11-05-2004, 10:52 AM   #4
secesh
Senior Member
 
Registered: Sep 2004
Location: Savannah, GA
Distribution: Ubuntu, Gentoo, Mythbuntu, ClarkConnect
Posts: 1,154

Rep: Reputation: 47
thanx for all that, i'm sure i'll review it to learn that myself later.

glad you got it fixed.
 
  


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
simple Perl question true_atlantis Programming 5 11-18-2003 08:37 PM
Simple Perl Question jacksmash Programming 1 11-13-2003 09:17 PM
Simple Perl question! Please help! jacksmash Programming 4 11-07-2003 05:56 AM
Simple Perl Question Danny.au Programming 6 07-18-2002 09:46 AM
A simple perl programming question J_Szucs Programming 12 07-14-2002 10:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:57 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