Bash error message on a mac using Perl

bashmacintoshperl

I'm trying to run basic functions on Perl (Mac) but I get the same error message all the time. I managed to run programs like the simple 'hello, world' example. They work fine:

$ perl -w hello.pl
Hello World

But when I try different things, such as assigning a value to a variable, it gives me a message such as:

$number = 13*2;
-bash: =: command not found

Commands such as list seem to work, but I get the bash error all the time. I read that it can be related to the path but not sure how to exactly verify this and what's the bash doing here.

Best Answer

You seem to be confusing bash with perl. The default shell on your mac is bash, which cannot set variables in the same way, or using the same syntax, as perl. To set a variable called '$number' to the result of '13 * 2' using perl, on a bash CLI, you would:

my-macbook:~ $ perl -e '$number = 13 * 2; print $number, "\n";'
26

To do the same thing in bash itself, you could:

my-macbook:~ $ number=$(expr 13 \* 2); echo $number
26

You can't put raw perl syntax into your bash shell in Terminal, because the bash shell running in Terminal is expecting bash syntax. Bash is a whole language unto itself, and is not compatible with perl. To tell bash that you want to execute some code using perl instead of bash, you use Perl's -e flag (as in e for execute), then wrap your perl code in single quotes.

Related Question