LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   New Perl Programmer (https://www.linuxquestions.org/questions/programming-9/new-perl-programmer-668200/)

Doctorzongo 09-07-2008 05:19 PM

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. ;-)

keefaz 09-07-2008 05:36 PM

Try to change every string comparaison == with the perl string comparaison operator: eq
so replace if($pastasel == "acpt") with if($pastasel eq "acpt") etc

chrism01 09-08-2008 01:11 AM

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";

Doctorzongo 09-09-2008 03:34 PM

Quote:

Originally Posted by chrism01 (Post 3272923)
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.

Doctorzongo 09-09-2008 03:35 PM

Quote:

Originally Posted by keefaz (Post 3272665)
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.

Doctorzongo 09-09-2008 03:41 PM

Nope, still not working.

keefaz 09-09-2008 04:49 PM

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)

chrism01 09-09-2008 08:10 PM

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


All times are GMT -5. The time now is 08:44 PM.