Click to See Complete Forum and Search --> : Need help to resolve error -Bareword found where operator expected at joke.pl line 15


janmejay
02-25-2008, 01:44 PM
I am a newbie to Perl programming. Couple questions, need help!

Copied below script from a book and tried to run it, however got following error

+++++++++++
sunp5:/export/home/janmejay-> /bin/perl joke.pl
Bareword found where operator expected at joke.pl line 15, near "print "What"
(Might be a runaway multi-line "" string starting on line 11)
(Do you need to predeclare print?)
syntax error at joke.pl line 15, near "print "What disappears "
Search pattern not terminated at joke.pl line 15.

+++++++++++

2nd question - why do I have to write /bin/perl filename everytime I want to run a script. Why not just with filename? If I give only filename, it gives
ksh: joke.pl: cannot execute
++++++++++++
Here is my code -

========================
sunp5:/export/home/janmejay-> cat -n joke.pl
1 #!/usr/bin/perl -w
2 #
3 # JokeMachine.pl
4 clear_the_screen();
5 clear_the_screen();
6 $reply = "";
7 while ($reply ne 'yes'){
8 print 'Would you like to hear a joke? (yes/no):';
9 chomp($reply = <STDIN>);
10 if ($reply ne 'yes') {
11 print "Hum...Perhaps you misunderstood.\n\";
12 }
13 }
14 clear_the_screen();
15 print "What disappears the moment you say its name?";
16 chomp($reply =<STDIN>;
17 if ($reply ne 'silence') {
18 print "Sorry, Wrong answer. Think about it and try again later.\n\n";
19 } else {
20 print "Yes, that is right. Well done!\n\n";
21 }
22 sub clear_the_screen {
23 for ($i=0; $i < 25; ++$i){
24 print "\n";
25 }
26 }
========================

CyCo
02-25-2008, 04:13 PM
Two (2) errors...

Line #11 should read like this:
print "Hum...Perhaps you misunderstood.\n";

And line #16 should read like this:chomp($reply = <STDIN>);

janmejay
02-25-2008, 04:33 PM
Thanks, Cyco!

it did work!

Everytime, I run script from ksh, I have to write like this /bin/perl filename,
how can I make it run just by writing filename ?

Thanks again.

Sixtease
02-26-2008, 12:38 AM
Firstly you'll need to set up the permissions to execute the file.chmod +x filenameIf you do this, you can run the file from the directory where it resides by saying./filenameIf you really want to be able to call it just by filename (which I discourage you from), then you can either make a symlink to the file from a directory that is in $PATH (like /usr/local/bin), which would be done by this commandln -s /the/whole/path/to/filename /usr/local/bin/filenameor add the current directory to the PATH, which would enable you to run any executable file by its name from the directory where it resides:export PATH=".:$PATH"I only have experience with bash, so in ksh, exporting a variable might have a different syntax.

Note: PATH is the environment variable determining where commands reside. See what it contains by saying echo $PATH.

janmejay
02-26-2008, 09:35 AM
Thank you Sixtease for your help on this.

I understood and it works. Only thing I want to ask you now is - you have said - "If you really want to be able to call it just by filename (which I discourage you from), "

What's the reason that you discourage to do this? Any concern that one might forget the links being used or any other technical reason? I was thinking, that's easier way to run commands just by using filename. I may not be able to foresee any problem this might create hence asking.

Thanks again.
Janmejay

Sixtease
02-26-2008, 11:14 AM
The problem with creating symlinks is that you have to undertake the fuss of creating one in PATH (which usually takes root access) for every silly thing you want to execute - and if you learn a programming language, you'll want to execute a lot of different scripts. Then your /usr/local/bin directory gets bloated with a lot of symlinks and the scripts you made persist as bareword-callable commands for you, which pollutes your environment (for example tab-completion).

With adding "." to your PATH, there's the problem that you become vulnerable to alien scripts. Say somebody sends you a bunch of files and you want to remove them. You cd to the directory where the files are and type rm *. But he included an executable file called "rm", which is a program that does something nasty. And since "." is in your PATH, you'll execute his chicanerous program unknowing.

Of course you can avoid this by having "." in the last position in PATH, so standard programs have a priority over the ones in your current directory. However, then you have to call scripts that coincide with command names with a "./" prepended... and thus do I believe it is safer and more consistent to run all custom scripts with "./" prepended - it's not that much extra to type, it's what other people use and it keeps you aware that you're not executing a standard command.

janmejay
02-26-2008, 11:30 AM
I ran this script - wanted to see if I could run the script by giving just its filename

$ cat -n hello.pl
1 #!/usr/bin/perl -w
2 export PATH=".:$/export/home/janmejay/perl/hello.pl"
3 print "Hello World!\n";

If I give
$ hello.pl # then I get this error
ksh: hello.pl: not found

Or if I give,

$ /bin/perl hello.pl # then I get this error
syntax error at hello.pl line 3, near "print"
Execution of hello.pl aborted due to compilation errors.

janmejay
02-26-2008, 11:33 AM
Thanks Sixtease, I agree with you "./" is a safer and cleaner way.

I tried by adding export path as mentioned in above post and got error in the above post.

Thanks again for your help on this one.

Sixtease
02-26-2008, 05:04 PM
The export thing is a shell command, not a perl command:$ export PATH=".:$PATH"
$ hello.pl