-4

I am trying to write a perl script having a menu in which user selects one in list (1-3) and then asks the user to enter a string (depending on menu selected) which should be read as input and printed in console. However when executing script, I am finding this error. What could be wrong here?

~] # perl sample.pl

Global symbol "$user" requires explicit package name at ./sample.pl line 26.
Global symbol "$user" requires explicit package name at ./sample.pl line 27.
Global symbol "$user" requires explicit package name at ./sample.pl line 28.
Global symbol "$process" requires explicit package name at ./sample.pl line 37.
Global symbol "$process" requires explicit package name at ./sample.pl line 38.
Global symbol "$process" requires explicit package name at ./sample.pl line 39.
Missing right curly or square bracket at ./sample.pl line 52, at end of line
syntax error at ./sample.pl line 52, at EOF
Execution of ./sample.pl aborted due to compilation errors.

#!/usr/bin/perl

use strict;
use warnings;
use Switch;

my $input = '';

while ($input ne '3')
{
clear_screen();

print "1. user\n".
      "2. process\n".
      "3. exit\n";

print "Enter choice: ";
$input = <STDIN>;
chomp($input);

{
    case '1'
 {

print "Enter user";
$user =  <STDIN>;
chomp ($user);
print "User is  $user\n";

  }

    case '2'
 {
 print "Enter process:";
 $process =  <STDIN>;
 chomp ($process);
 print "Process is $process\n";
    }
  }
Taryn
  • 242,637
  • 56
  • 362
  • 405
user3331975
  • 2,647
  • 7
  • 28
  • 30
  • 1
    http://perlmaven.com/global-symbol-requires-explicit-package-name – mpapec Oct 31 '14 at 09:35
  • 1
    Don't use switch http://stackoverflow.com/questions/2630547/why-is-the-switch-module-deprecated-in-perl – Dr.Avalanche Oct 31 '14 at 09:37
  • 1
    Your [edit](http://stackoverflow.com/posts/26671362/revisions) totally changes the question, your indentation is horrible. Lean basic programming http://learn.perl.org – Dr.Avalanche Oct 31 '14 at 10:17
  • 1
    It means exactly what it says on the tin - you are missing a bracket. It's more easy to see where this is happening if your format your code nicely. – Sobrique Oct 31 '14 at 10:20
  • 1
    @user3331975 Please don't keep altering your question when new errors crop up. Mark this question closed by accepting an answer, and then do some research into basic Perl debugging and programming practices. – i alarmed alien Oct 31 '14 at 11:17

1 Answers1

1

If you use perltidy to format your code, you can easily see that there's an unmatched bracket:

#!/usr/bin/perl
use warnings;

my $input='';

while ( $input ne '3' ) {
    clear_screen();

    print "1. user\n" . "2. process\n" . "3. exit\n";
    print "Enter choice: ";
    $input=<STDIN>;
    chomp($input);
    {   ## <-- this open brace is probably the culprit of the error

        if ( $input eq '1' ) {
            print "Enter user";
            $user=<STDIN>;
            chomp($user);
            print "User is  $user\n";
        }
        else {
            print "Enter process:";
            $process=<STDIN>;
            chomp($process);
            print "Process is $process\n";
        }
    }

Code formatting isn't just about making code look nice; it also makes it easier to track down errors and for others to understand. You should also use strict; on all your scripts to ensure that potential programming errors get caught before they cause problems in your code.

i alarmed alien
  • 9,412
  • 3
  • 27
  • 40