CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Help with simple command line program

 



p3aul
Novice

Jul 27, 2012, 11:53 PM

Post #1 of 5 (1143 views)
Help with simple command line program Can't Post

I'm writing this to be used as a simple program to be run on the gnome terminal in Ubuntu. I have perl 5.10.1 and am using the given/when statement. Here is the code:

Code
#!/user/bin/perl 
print("Enter 1 for C to F or 2 for F to C");
$choice=<>;
given($choice);{
when(1){ print( "Enter Temp in C ");
$c=<>;
$g = (9/5) * $c + 32;
print ("Temp in Farenhite is: $g");
}
when(2){ print ("Enter Temp in F: ");
$f=<>;
$h = (5/9)* ($f - 32);
print ("Temp in Celsius Is: $h");
}
default { print ("ERROR! input must be a 1 or 2");
}


Here is the terminal output:

Code
owner@owner-desktop:~/PERL-LAB$ perl temp.pl 
syntax error at temp.pl line 5, near "){"
syntax error at temp.pl line 10, near "){"
syntax error at temp.pl line 14, near "}"
Execution of temp.pl aborted due to compilation errors.
owner@owner-desktop:~/PERL-LAB$


I guess it is something simple, but I'm a bit rusty with perl right now and I can't see it. This is my first time programming with given/when also.
Any help would be greatly appreciated and then I can move on and program a MS Word Killer!
Paul


wickedxter
User

Jul 28, 2012, 3:55 AM

Post #2 of 5 (1133 views)
Re: [p3aul] Help with simple command line program [In reply to] Can't Post

you have to include it by useing the module

Code
use feature qw () #this will allow u to use the switch



p3aul
Novice

Jul 28, 2012, 1:56 PM

Post #3 of 5 (1114 views)
Re: [p3aul] Help with simple command line program [In reply to] Can't Post

Ok I put this line rigt under the shebang line:
use feature qw ();
Also corrected the syntax of the print statement. all to no avail

Code
#!/user/bin/perl 
use feature qw ();
print "Enter 1 for C to F or 2 for F to C ";
$choice=<>;
given($choice){
when(1){ print "Enter Temp in C ";
$c=<>;
$g = (9/5) * $c + 32;
print "Temp in Farenhite is: $g";
}
when(2){ print "Enter Temp in F: ";
$f=<>;
$h = (5/9)* ($f - 32);
print "Temp in Celsius Is: $h";
}
default { print "ERROR! input must be a 1 or 2";
}
}

Here are the errors:


Code
owner@owner-desktop:~/PERL-LAB$ perl temp.pl 
syntax error at temp.pl line 5, near "){"
syntax error at temp.pl line 10, near "}"
syntax error at temp.pl line 15, near "}"
Execution of temp.pl aborted due to compilation errors.
owner@owner-desktop:~/PERL-LAB$



FishMonger
Veteran / Moderator

Jul 28, 2012, 2:17 PM

Post #4 of 5 (1113 views)
Re: [p3aul] Help with simple command line program [In reply to] Can't Post

That use statement is instructing perl to not import any of the functions in the pragma.

You need to tell it that you want to import the switch function.

Code
use feature qw(switch);


See: http://search.cpan.org/~rjbs/perl-5.16.0/regen/feature.pl


(This post was edited by FishMonger on Jul 28, 2012, 2:19 PM)


p3aul
Novice

Jul 28, 2012, 6:51 PM

Post #5 of 5 (1103 views)
Re: [FishMonger] Help with simple command line program [In reply to] Can't Post

Perfect! Thank you fishmonger!Laugh

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives