LinuxQuestions.org
Review your favorite Linux distribution.
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 01-23-2006, 04:10 AM   #1
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
Unable to escape scope in Perl


I'm having some difficulty grasping the concept of scoping in Perl and am hoping someone can explain it to me. The specific source code I'm having trouble with is here:
Code:
#!/usr/bin/perl

# PerlSim.pl - Perl OGame Combat Simulator

# Copyright (c)2006 John Dorn III <john.dorn@gmail.com>
# Licensed under the GNU GPL.
# This program is provided "as is" without any warranty or guarantee. Use at your own risk.

use Class::Struct;
use strict;
use warnings;

struct ( Ship => { # creates ships as objects
  quantity => '$', # number of ships
  hull => '$', # declares hull as a scalar (structural integrity)
  shield => '$', # shielding
  weapon => '$', # weapon power
  cargo => '$', # cargo capactity
  speed => '$', # base speed
  fuel => '$', # fuel consumption
} );

my %fleet_att = ( # attackers fleet
  "sc" => "0",
  "lc" => "0",
  "lf" => "0",
  "hf" => "0",
  "cr" => "0",
  "bs" => "0",
  "cs" => "0",
  "rc" => "0",
  "ep" => "0",
  "bm" => "0",
  "de" => "0",
  "rip" => "0",
);
my %fleet_def = ( # defenders fleet
  "sc" => "0",
  "lc" => "0",
  "lf" => "0",
  "hf" => "0",
  "cr" => "0",
  "bs" => "0",
  "cs" => "0",
  "rc" => "0",
  "ep" => "0",
  "bm" => "0",
  "ss" => "0",
  "de" => "0",
  "rip" => "0",
  "ml" => "0",
  "sl" => "0",
  "hl" => "0",
  "gc" => "0",
  "ic" => "0",
  "pc" => "0",
  "ssd" => "0",
  "lsd" => "0",
);
my %tech_att = ( # attackers techs
  "weapon" => "0",
  "shielding" => "0",
  "armor" => "0",
);
my %tech_def = ( # defenders techs
  "weapon" => "0",
  "shielding" => "0",
  "armor" => "0",
);
my %res_def = ( # defenders resources
  "metal" => "0",
  "crystal" => "0",
  "deut" => "0",
);

print "PerlSim v0.01\n";
print "Licensed under the GNU GPL.\n";
print "This program is provided \"as is\" without any warranty or guarantee.\n";
print "Use at your own risk.\n\n";

# Attacker information:
print "Attacker Technologies:\n";
print "Weapon Technology: ";
chomp ( $tech_att{'weapon'} = <STDIN> );
print "Shielding Technology: ";
chomp ( $tech_att{'shielding'} = <STDIN> );
print "Armor Technology: ";
chomp ( $tech_att{'armor'} = <STDIN> );
print "\n";
print "Attacker Fleet:\n";
print "Small Cargo: ";
chomp ( $fleet_att{'sc'} = <STDIN> );
print "Large Cargo: ";
chomp ( $fleet_att{'lc'} = <STDIN> );
print "Light Fighter: ";
chomp ( $fleet_att{'lf'} = <STDIN> );
print "Heavy Fighter: ";
chomp ( $fleet_att{'hf'} = <STDIN> );
print "Cruiser: ";
chomp ( $fleet_att{'cr'} = <STDIN> );
print "Battleship: ";
chomp ( $fleet_att{'bs'} = <STDIN> );
print "Colony Ship: ";
chomp ( $fleet_att{'cs'} = <STDIN> );
print "Recycler: ";
chomp ( $fleet_att{'rc'} = <STDIN> );
print "Espionage Probe: ";
chomp ( $fleet_att{'ep'} = <STDIN> );
print "Bomber: ";
chomp ( $fleet_att{'bm'} = <STDIN> );
print "Destroyer: ";
chomp ( $fleet_att{'de'} = <STDIN> );
print "Death Star: ";
chomp ( $fleet_att{'rip'} = <STDIN> );
print "\n";

#Defender information:
print "Defender Technologies:\n";
print "Weapon Technology: ";
chomp ( $tech_def{'weapon'} = <STDIN> );
print "Shielding Technology: ";
chomp ( $tech_def{'shielding'} = <STDIN> );
print "Armor Technology: ";
chomp ( $tech_def{'armor'} = <STDIN> );
print "\n";
print "Defender Fleet:\n";
print "Small Cargo: ";
chomp ( $fleet_def{'sc'} = <STDIN> );
print "Large Cargo: ";
chomp ( $fleet_def{'lc'} = <STDIN> );
print "Light Fighter: ";
chomp ( $fleet_def{'lf'} = <STDIN> );
print "Heavy Fighter: ";
chomp ( $fleet_def{'hf'} = <STDIN> );
print "Cruiser: ";
chomp ( $fleet_def{'cr'} = <STDIN> );
print "Battleship: ";
chomp ( $fleet_def{'bs'} = <STDIN> );
print "Colony Ship: ";
chomp ( $fleet_def{'cs'} = <STDIN> );
print "Recycler: ";
chomp ( $fleet_def{'rc'} = <STDIN> );
print "Espionage Probe: ";
chomp ( $fleet_def{'ep'} = <STDIN> );
print "Bomber: ";
chomp ( $fleet_def{'bm'} = <STDIN> );
print "Solar Satellite: ";
chomp ( $fleet_def{'ss'} = <STDIN> );
print "Destroyer: ";
chomp ( $fleet_def{'de'} = <STDIN> );
print "Death Star: ";
chomp ( $fleet_def{'rip'} = <STDIN> );
print "Missile Launcher: ";
chomp ( $fleet_def{'ml'} = <STDIN> );
print "Small Laser: ";
chomp ( $fleet_def{'sl'} = <STDIN> );
print "Heavy Laser: ";
chomp ( $fleet_def{'hl'} = <STDIN> );
print "Gauss Cannon: ";
chomp ( $fleet_def{'gc'} = <STDIN> );
print "Ion Cannon: ";
chomp ( $fleet_def{'ic'} = <STDIN> );
print "Plasma Cannon: ";
chomp ( $fleet_def{'pc'} = <STDIN> );
print "Small Shield Dome: ";
chomp ( $fleet_def{'ssd'} = <STDIN> );
print "Large Shield Dome: ";
chomp ( $fleet_def{'lsd'} = <STDIN> );
print "\n";
print "Defender Resources:\n";
print "Metal: ";
chomp ( $res_def{'metal'} = <STDIN> );
print "Crystal: ";
chomp ( $res_def{'crystal'} = <STDIN> );
print "Deuterium: ";
chomp ( $res_def{'deut'} = <STDIN> );
print "\n";

# build the ships
foreach ( keys %fleet_att ) {
  if ( ( defined $fleet_att{$_} ) && ( $fleet_att{$_} ne "" ) && ( $fleet_att{$_} > 0 ) ) {
    $main::att_ship = Ship -> new(); # need to use explicit package name main:: here to escape lexical scoping
    $main::att_ship -> quantity('$fleet_att{$_}');
    print "Quantity: $main::att_ship -> quantity \n";
    my $desc = $_;
    foreach (<ships.ini>) {
      m/^${fleet_att{$desc}}{(.+),(.+),(.+),(.+),(.+),(.+)}/; # pattern match on ships.ini file
      $main::att_ship -> hull($1);
      $main::att_ship -> shield($2);
      $main::att_ship -> weapon($3);
      $main::att_ship -> cargo($4);
      $main::att_ship -> speed($5);
      $main::att_ship -> fuel($6);
    }
    print "$main::att_ship->hull \n";
    print "$main::att_ship->shield \n";
    print "$main::att_ship->weapon \n";
    print "$main::att_ship->cargo \n";
    print "$main::att_ship->speed \n";
    print "$main::att_ship->fuel \n";
  } 
}
Just in case it's needed, these are what the lines in the ships.ini file look like:
Code:
cr{27000,50,400,800,15000,300}
Please excuse the geekiness of the current code... Anyone who's played OGame will understand. My questions are thus:

1) Why, on line 182 ( $main::att_ship = Ship -> new(); ) do I need to use the main:: to escape lexical scoping if this is the line in which the variable is being declared?
2) Why aren't the memory parentheses being assigned? Is there a problem with the regular expression?
3) Is there something special you need to do to print members of a struct like in line 195 ( print "$main::att_ship->hull \n"; )?
For reference, this is what I'm getting after all the data entry:
Code:
Quantity: Ship=HASH(0x813fdfc) -> quantity
Ship=HASH(0x813fdfc)->hull
Ship=HASH(0x813fdfc)->shield
Ship=HASH(0x813fdfc)->weapon
Ship=HASH(0x813fdfc)->cargo
Ship=HASH(0x813fdfc)->speed
Ship=HASH(0x813fdfc)->fuel
Thanks in advance for all help

I'm going to get this someday

Oh, as for the Copyright notice, I do intend (eventually) to release this software... under the GPL of course
 
Old 01-23-2006, 04:10 PM   #2
FlowState
LQ Newbie
 
Registered: Jul 2005
Distribution: Gentoo, LFS
Posts: 13

Rep: Reputation: 0
1) If you want to predeclare $att_ship as a global variable (which you'll have to, since I see you use strict), try adding:

Code:
use vars qw($att_ship);
Global variables declared in this way are allowable under use strict

2) It looks like you are not opening the file before looping through it

Code:
open FH, "ships.ini";
foreach (<FH>){
...
The way you have it now, foreach is looping through the literal string 'ships.ini', instead of the filehandle to it.

3) I think if you fix 2, this will start displaying properly.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
scope of an object and global scope lucky6969b Programming 7 12-09-2005 10:09 PM
unable to run perl script using browser madhuri Debian 3 12-02-2005 05:46 PM
unable to run perl script using browser madhuri Linux - Distributions 1 11-22-2005 03:19 PM
Unable to install Perl module via CPAN Sparky.Watson Linux - Software 1 08-24-2005 09:23 PM
scope suchi_s Programming 1 10-30-2004 08:07 AM

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

All times are GMT -5. The time now is 03:33 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration