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



p3aul
Novice

Jul 27, 2012, 11:53 PM


Views: 1881
Help with simple command line program

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


Views: 1871
Re: [p3aul] Help with simple command line program

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


Views: 1852
Re: [p3aul] Help with simple command line program

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


Views: 1851
Re: [p3aul] Help with simple command line program

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


Views: 1841
Re: [FishMonger] Help with simple command line program

Perfect! Thank you fishmonger!Laugh