ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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. ;-)
Try to change every string comparaison == with the perl string comparaison operator: eq
so replace if($pastasel == "acpt") with if($pastasel eq "acpt") etc
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:
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.
Try to change every string comparaison == with the perl string comparaison operator: eq
so replace if($pastasel == "acpt") with if($pastasel eq "acpt") etc
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)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.