3 Replies - 1402 Views - Last Post: 02 November 2011 - 08:01 AM

#1 CapC  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 27-September 11

explicit package error

Posted 01 November 2011 - 11:18 AM

Afternoon, I'm attempting to teach myself perl. Could someone help me understand this? I have encountered the following errors:

Global symbol "%rates" requires explicit package name at convert1.plx line 7.
Global symbol "%rates" requires explicit package name at convert1.plx line 27.
Global symbol "%rates" requires explicit package name at convert1.plx line 31.
syntax error at convert1.plx line 36, near "$rates("
Global symbol "%rates" requires explicit package name at convert1.plx line 36.
Execution of convert1.plx aborted due to compilation errors.

#!/usr/bin/perl
# Convert1.plx
use warnings;
use strict;

my ($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
);

print "Enter starting currency:";
$from = <STDIN>;
print "Enter your target currency";
$to = <STDIN>;
print "Enter your amount: ";
$value = <STDIN>;

chomp($from,$to,$value);

if (not exists $rates{$to})
{
 die "I don't know anything about $to as a currency\n";
}
if (not exists $rates{$from})
{
die "i don't know anything about $from as a currency\n";
}

$rate = $rates($to) / $rates{$from};

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



I am using variations of book examples that I created. This one actually came directly from the book and is still depicting errors. Could someone please help to explain the error? Is this telling me that I have to declare a package notation similar to in java?

This post has been edited by CapC: 01 November 2011 - 11:19 AM


Is This A Good Question/Topic? 0
  • +

Replies To: explicit package error

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: explicit package error

Posted 01 November 2011 - 01:21 PM

The error message "Global symbol requires explicit package name" is telling you that you're using a variable that you did not declare. On line 6 you're declaring the variable $rates, but on all the lines the error message is complaining about you're actually trying to use the variable %rates, not $rates. So you should change the declaration to %rates.

Additionally you're using parentheses instead of curly braces in $rates($to) in line 36.
Was This Post Helpful? 2
  • +
  • -

#3 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: explicit package error

Posted 02 November 2011 - 01:33 AM

sepp2k's nailed it syntactically, but I'd like to add a stylistic point: It's generally considered good form to declare your variables the first time they're actually used rather than having a block of declarations at the top of the code. Doing it that way helps to keep the purpose and function of the variable more clear since you don't have to read all the way back to the top to see whether it might have a pre-existing value.

It would also be cleaner to do your error-checks on the user's input immediately after each item is entered rather than making the user enter all three values, only to be told that the first one was invalid.

So, with those things in mind:

#!/usr/bin/perl
# Convert1.plx
use warnings;
use strict;

my %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
);

print "Enter starting currency: ";
my $from = <STDIN>;
chomp $from;
if (not exists $rates{$from}) {
  die "i don't know anything about $from as a currency\n";
}

print "Enter your target currency: ";
my $to = <STDIN>;
chomp $to;
# The way I would normally write this test:
die "I don't know anything about $to as a currency\n" unless exists $rates{$to};

print "Enter your amount: ";
my $value = <STDIN>;
chomp $value;

my $rate = $rates{$to} / $rates{$from};

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


Was This Post Helpful? 1
  • +
  • -

#4 CapC  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 27-September 11

Re: explicit package error

Posted 02 November 2011 - 08:01 AM

Thanks gents, I just ordered the camel,llama and the perl little black book. The guide I was viewing online obviously had some errors.

I have to say the biggest difficulty thus far has been understanding the change from strictly OO languages to procedural and OO integrated together and how to manage that. But then again I'm only a week into teaching myself perl so we'll see where I'm sitting in a month or two.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1