2 Replies - 7993 Views - Last Post: 31 January 2013 - 07:37 AM

#1 Pwn  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 458
  • Joined: 25-November 07

Perl-Use Strict with global variables

Posted 30 January 2013 - 10:07 AM

Going through the tutorial, and I've come across a snag. Tutorial says, always use strict to force variable scope. However, I've declared some global variables, using the 'our' tag, but inside blocks, can't do anything with them. Using same variable name inside scope and I get uninitialized

#!/strawberry/bin/perl -w
use strict;
use warnings;

print "Currency Conversion exchange\n";
our ($value, $from, $to, $rate, %rates);

%rates=
(
	pounds=>1,
	dollars=>1.6,
	marks=>3.0,
	"french francs"=>10.0,
	yen=>174.8,
	"swiss francs"=>2.43,
	drachma=>492.3,
	euro=>1.5,
	sticks=>30,
	stones=>15
);

until(exists $rates{$from})
{
	print "Enter your starting currency: ";
	our $from=<STDIN>;
	chomp $from;
}

until(exists $rates{$to})
{
	print "Enter your target currency: ";
	our $to=<STDIN>;
	chomp $to;
}

until($value!=0)
{
	print "Enter your amount (or 0 to quit): ";
	our $value=<STDIN>;
	chomp $value;
}

print "$value $from is ",$value*$rate," $to.\n";


If I take 'our' away from inside the blocks, I get unitialized variables warnings, and if I put 'our' inside the blocks, then they're treated as separate scoped variables and I get divide by zero errors (or something along that line). I could get this to work by removing the strict pragma, but that's walking backwards.

I've searched several tutorials, and done many various searches to get an idea why, but nothing seems to cover this issue. Any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Perl-Use Strict with global variables

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2087
  • View blog
  • Posts: 3,175
  • Joined: 21-June 11

Re: Perl-Use Strict with global variables

Posted 30 January 2013 - 08:29 PM

The warning about $from being uninitialized is because the first time you try to use $from to index into %rates, $from hasn't been initialized yet. If you change the loop to until($from && exists $rates{$from}), the warning will stop. Same for $to. You should also set $value to 0 before you loop until its != 0.

The other warning you get is about your variables being redeclared. That's because you declare your variables multiple times. Once at the beginning of the file and once when you first assign them. You should not do that. Simply remove the our from the lines where you declare a variable for the second time.

PS: You might want to use my instead of our here. I don't see a reason for your variables to be global here. You don't use subroutines and if you did, it would be better to use variables that are local to those subroutines anyway.
Was This Post Helpful? 0
  • +
  • -

#3 Pwn  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 458
  • Joined: 25-November 07

Re: Perl-Use Strict with global variables

Posted 31 January 2013 - 07:37 AM

Thanks Sepp2k, makes sense, and why didn't I catch that my variables weren't initialized, duh. The 'our' in the blocks, as I stated in OP, was me playing around trying to get it to work. I know they shouldn't be there, was just plucking around to see what problems I could create for myself, you know how it is...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1