Perl with Sublime Text 2: not working

2020-07-27 07:04发布

问题:

I'm new to Perl and am trying to write a simple program that prompts the user to enter a number. I then want that number to be printed. I think I have the correct code, but when I run the program I'm not able to enter anything. I'm using Sublime Text 2. Am I missing a plugin or something? How do I get this to work? I've only done simple if/else statements prior to this and everything worked. I just can't seem to prompt the user for input.

Here is the code:

print ("Please enter a number: \n");
$seq = <STDIN>;
print("Sequence = $seq \n");

And here is the output:

Use of uninitialized value $seq in concatenation (.) or string at C:\blah\blah\practice.pl line 3.
Please enter a number:
Sequence =
[Finished in 0.4s]

回答1:

According to this - very similar - Python question: " Sublime Text 2 console input " - Sublime Text doesn't support STDIN input. Confirmed here as well.

You can solve this one of 2 ways:

  1. Run your program outside of Sublime, in shell. On Windows, simply save your Perl script as c:\your_directory\your_subdir\your_perl_script.pl ; open the command interpreter ("Start"=>"Run"=>"cmd.exe") and on c:\ prompt in the interpreter, run:

    c:\your_directory\your_subdir\your_perl_script.pl
    

    or if you didn't associate .pl extension with Perl when you installed Perl,

    perl c:\your_directory\your_subdir\your_perl_script.pl
    
  2. Follow the linked SO question's answer and use SublimeREPL

  3. Use the Terminal plugin



回答2:

Remember to add an "<>;" at the last line of your perl code.

And then: tools->build new system->

{
    "cmd": ["perl","$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.perl",
    "variants":
    [
        {
            "name": "RunInCommand",
            "shell": true,
            "cmd": ["start", "perl","${file_base_name}.pl"]
        }
    ]
}

Now, if you want to run your perl code using a shortcut key, such as "F5", you can add one: preferences->key buildings-user->

[
    {"keys": ["f5"], "command": "build", "args": {"variant": "RunInCommand"}}
]

Enjoy programming.