Current location: Hot Scripts Forums » Programming Languages » Perl » Simple Perl Question


Simple Perl Question

Reply
  #1 (permalink)  
Old 11-03-04, 05:07 PM
whillers whillers is offline
New Member
 
Join Date: Nov 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Simple Perl Question

This is driving me crazy since declaring 2 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"?



&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
Reply With Quote
  #2 (permalink)  
Old 11-04-04, 10:54 AM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
If you had been using strict you would have noticed that even using our is giving you errors. What you are tyring to do is dereference a scalar reference. This is how you should be doing that:

Code:
use strict;
use warnings;

# ...
sub ... {
  our $x = 'hello';
  our $y = \$x;
  print "$$y\n";
}
That will work (wihtout any warnings) with our and my.

~Charlie
Reply With Quote
  #3 (permalink)  
Old 11-10-04, 03:44 PM
darkmonkey's Avatar
darkmonkey darkmonkey is offline
New Member
 
Join Date: Nov 2004
Location: London, United Kingdom
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Code:
my($a,$b)=(); # using 'my'
Should/Could (and will work as):

Code:
my($a,$b); # using 'my'
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Configuring perl newbie question? mickengland Perl 1 09-17-04 11:02 AM
Simple question Maniax JavaScript 4 08-31-04 01:08 PM
Simple PHP question about JPEG? snooky5 PHP 4 08-14-04 03:36 PM
Simple question kashi PHP 5 08-13-04 10:23 PM
question about simple login system magsec4 PHP 5 02-23-04 03:35 AM


All times are GMT -5. The time now is 03:59 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.