LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 09-07-2008, 05:19 PM   #1
Doctorzongo
Member
 
Registered: Mar 2008
Distribution: Fedora 11
Posts: 72

Rep: Reputation: 16
New Perl Programmer


Hello there. I need help debugging a perl program for printing out recipes. Keep in mind, I am extremely new to perl programming. I have probably made some blindingly obvious mistake. Thank you.

Code:
#!/usr/bin/perl
#
# This is the code for my recipe selector, version 0.1 BETA
# Written in Perl
# Current recipe(s): Apricot Chicken Pot Stickers, Baked Pasta Casserole
use strict;
use warnings;


# Locate the files
my $acptloc = <recipe/acpt.txt>;
my $bpcloc = <recipe/bpc.txt>;
# Open the recipe files
# - Pasta
open(acptfile, "recipe/acpt.txt") or die("Program unable to open acpt.txt!");
open(bpcfile, "recipe/bpc.txt") or die("Program unable to open bpc.txt!");

# Put the files into variables
# - Pasta
my @acptprint = <acptfile>;
my @bpcprint = <bpcfile>;

# The recipe categories
my @recipecat = ("pasta", "bbq", "bread");

# Print the welcome screen and recipe catagories
print "Welcome to my recipe tracking system!\n\n";
print "This is version 0.1 BETA. Please select a category:\n\n @recipecat\n\n";

# Select the recipe categorie
my $recipecatselect = <STDIN>;

# If the use selects the pasta categories, do this
if ($recipecatselect == "pasta") {

# Prints out the pasta welcome screen
 print "\n\nHello. You selected pasta. Please select a pasta from the list.\n";
 print "Use the abbrievations, thank you.\n\n";
 
# List of pasta recipes
# ACPT - Apricot Chicekn Pot Stickers
# BPC - Baked Pasta Casserole
# In an array:
my @pastalist = ("acpt", "bpc");


 # Print the pasta list
 print "acpt - apricot chicken pot stickers\n bpc - baked pasta casserole\n\n";
 
 # Select the pasta
my $pastasel = <STDIN>; 

# If the user selects ACPT, do this
         if ($pastasel == "acpt") {
         
            
            # Print the recipe
            print "\n\n";
            print @acptprint;
            print "\n";
            
            # Close the recipe
            close ("acptfile");
            

         }
         

# If the user selects BPC, do this
         elsif ($pastasel == "bpc") {
            
            # Print the recipe
            print "\n\n";
            print @bpcprint;
            print "\n";
            
            # Close the file
            close ("bpcfile");
            
         }
         
         else {
            print "That feature is not currently implemented. Sorry";
         }
         
         }

else {
    print "That feature is not currently implemented. Sorry.";
}

print "Press ENTER to exit.";
my $exit = <STDIN>;
I know the code is probably horribly lumped together, but this is practice. And no, this is not a school project, I am doing it on my own. ;-)
 
Old 09-07-2008, 05:36 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Try to change every string comparaison == with the perl string comparaison operator: eq
so replace if($pastasel == "acpt") with if($pastasel eq "acpt") etc
 
Old 09-08-2008, 01:11 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
These 2 vars are unused:

my $acptloc = <recipe/acpt.txt>;
my $bpcloc = <recipe/bpc.txt>;

you should get a warning from Perl about that. Personally, I'd find your var names easier to read if you insert underscores between words eg $recipe_cat_select instead of $recipecatselect, but that's just me

You can ask Perl to check your code without executing the prog like this:

perl -wc myprog.pl

Actually, I think instead of

my $acptloc = <recipe/acpt.txt>;

you mean

my $acptloc = "recipe/acpt.txt";

Last edited by chrism01; 09-08-2008 at 01:13 AM.
 
Old 09-09-2008, 03:34 PM   #4
Doctorzongo
Member
 
Registered: Mar 2008
Distribution: Fedora 11
Posts: 72

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by chrism01 View Post
These 2 vars are unused:

my $acptloc = <recipe/acpt.txt>;
my $bpcloc = <recipe/bpc.txt>;

you should get a warning from Perl about that. Personally, I'd find your var names easier to read if you insert underscores between words eg $recipe_cat_select instead of $recipecatselect, but that's just me

You can ask Perl to check your code without executing the prog like this:

perl -wc myprog.pl

Actually, I think instead of

my $acptloc = <recipe/acpt.txt>;

you mean

my $acptloc = "recipe/acpt.txt";
DUH! $acptloc was supposed in placed of putting <recipe/acpt.txt> in the open line.
 
Old 09-09-2008, 03:35 PM   #5
Doctorzongo
Member
 
Registered: Mar 2008
Distribution: Fedora 11
Posts: 72

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by keefaz View Post
Try to change every string comparaison == with the perl string comparaison operator: eq
so replace if($pastasel == "acpt") with if($pastasel eq "acpt") etc
Alright, thanks.
 
Old 09-09-2008, 03:41 PM   #6
Doctorzongo
Member
 
Registered: Mar 2008
Distribution: Fedora 11
Posts: 72

Original Poster
Rep: Reputation: 16
Nope, still not working.
 
Old 09-09-2008, 04:49 PM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
try add: chomp $recipecatselect;
just under the my $recipecatselect = <STDIN>; line
(because $recipecatselect contains a \n character at the end, so could never be strictly equal to "pasta" for example, chomp will remove the \n)

Last edited by keefaz; 09-09-2008 at 04:50 PM.
 
Old 09-09-2008, 08:10 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
... and don't just say 'its not working', tell us what error you get etc.
 
  


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
Programmer to Programmer ( Long Story Of A GUI ) mdoubledragon Programming 1 10-13-2005 05:41 PM
Should I become a programmer? coolblue Programming 10 09-18-2005 09:36 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM

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

All times are GMT -5. The time now is 04:29 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
Open Source Consulting | Domain Registration