I am learning how to use Perl on chrome. When I attempt to enter a variable in terminal it gives me this output. I know how to run code with files, just trying to see what I can do directly in the terminal using perl -e and not going to vim. How can I enter and then print scalar variables in the terminal?
#code starts below
stepdeff@penguin:~$ perl -e $var = 10;
syntax error at -e line 1, near "="
Execution of -e aborted due to compilation errors.
stepdeff@penguin:~$ perl -e x = 10;
stepdeff@penguin:~$ perl -e 'print "x";'
xstepdeff@penguin:~$ perl -e x = $10;
stepdeff@penguin:~$ perl -e print "x";
stepdeff@penguin:~$ perl -e $x = "time";
syntax error at -e line 1, near "="
Execution of -e aborted due to compilation errors.
stepdeff@penguin:~$ perl -e $x =10;
stepdeff@penguin:~$ perl -e $name = "friend";
syntax error at -e line 1, near "="
Execution of -e aborted due to compilation errors.
stepdeff@penguin:~$ perl -E $name = "friend";
syntax error at -e line 1, near "="
Execution of -e aborted due to compilation errors.
stepdeff@penguin:~$ perl -e $x = 10;
syntax error at -e line 1, near "="
Execution of -e aborted due to compilation errors.
stepdeff@penguin:~$ perl -E $x = 10;
syntax error at -e line 1, near "="
Execution of -e aborted due to compilation errors.
stepdeff@penguin:~$
CodePudding user response:
You have to have quotes around the code
stepdeff@penguin:~$ perl -e'$var = 10;'
CodePudding user response:
You need to pass the program as a single argument.
If your program is
$var = 10;
say $var;
Then you need to pass that entire thing as an argument.
For the shell in question, this can be achieved by wraping the entire string in single quotes, replacing any single quotes within with '\''
. This means you could use
perl -E'$var = 10;
say $var;'
Altering the whitespace in the program means we can also use
perl -E'$var = 10; say $var;'
or
perl -E'
$var = 10;
say $var;
'