perl version for syntax errors

All,

Does it matter what perl verios your running when you get syntax errors?

on version 5.6.1 the code works fine, but on 5.8.0 the code gets errors?

#!/usr/bin/perl
#use strict;
#use warnings;
my $mess = 'messages';
my $mess1 = 'messages.1';
my $mess2 = 'messages.2';
my $mess3 = 'messages.3';
my $mess4 = 'messages.4';
if (-e $mess4)
{print "File $mess4 exists.\n"}

errors:

line 7: my: command not found
line 8: my: command not found
line 9: my: command not found
line 10: my: command not found
line 11: my: command not found
line 15: syntax error near unexpected token `else'
line 15: `else {'

Any help will be greatly appreciated!
Ben

From these errors, it would seem that the script isn't run by Perl, but rather by the shell. How do you usually start it? Have you tried explicitly invoking Perl with the script as an argument?

Pludi,

I need some information: Have you tried explicitly invoking Perl with the script as an argument?

I dont know how to invoke perl?

Ben

On the command line, enter

perl yourscript.pl

And how do you usually start the script?

Pludi,

Thanks. Why would I have to invoke perl, if Ive never had to do that before?

Normally i just enter ./script.pl, to make it run.

Ben

So if invoked explicitly, it does work? (Remember, we aren't psychics, we only know what you tell us)

What's the difference between the working system and the non-working one, other than different Perl versions? What OS in what version, what shell, ...

1 Like

Pludi,

Now I have completely different errors:

String found where operator expected at obj14-1.pl line 95, near "open (MESS1, ""
(Might be a runaway multi-line "" string starting on line 92)
(Missing semicolon on previous line?)
String found where operator expected at obj14-1.pl line 96, near "open (MESS, ""
(Might be a runaway multi-line "" string starting on line 95)
(Missing semicolon on previous line?)
Scalar found where operator expected at obj14-1.pl line 96, near "open (MESS, "$mess"
(Do you need to predeclare open?)

How do I find out versions and everything else you wanted to know?

Ben

---------- Post updated at 03:29 AM ---------- Previous update was at 03:13 AM ----------

I think I found out why....the system it works on is HPUX, the system it doesnt work on is Linux.

HPUX is running 5.6.1 perl version
Linux is running 5.8.0 perl version

Could that be it?

Ben

Those errors have nothing to do with any versions, but are syntax errors from Perl itself. Check your code and make sure that it's correct.

As for the versions:

  • OS and version:
    text uname -sr
  • Current shell:
    text awk -vuser=$USER -F: '$1 ~ user {print $NF}' /etc/passwd
  • Perl version: perl -v
  • Shell version: one of these:
    text echo ${.sh.version} echo $BASH_VERSION echo $version

And again, how do you invoke the script on the two different platforms? Meaning, what do you type to start it?

... and the OP should post the entire Perl code ...

Pludi,

On both platforms all I do (and have done) is ./script.pl, and then they run.

radoulov,

#!/bin/perl
use strict;
use warnings;
#*********************************************************** defining variables
my $mess='messages';
my $mess1='messages.1';
my $mess2='messages.2';
my $mess3='messages.3';
my $mess4='messages.4';
#********************************** checking for existence of defined variables
if (-e $mess4) {
  print "File $mess4 exists.\n" }
else {
  print "File $mess4 does not exist. Creating $mess4 ...\n";
  open (MESS4, ">$mess4") or die $!;
  close(MESS4); }
if (-e $mess3) {
  print "File $mess3 exists.\n" }
else {
  print "File $mess3 does not exist. Creating $mess3 ...\n";
  open (MESS3, ">$mess3") or die $!;
  close(MESS3); }
if (-e $mess2) {
  print "File $mess2 exists.\n" }
else {
  print "File $mess2 does not exist. Creating $mess2 ...\n";
  open (MESS2, ">$mess2") or die $!;
  close(MESS2); }
if (-e $mess1) {
  print "File $mess1 exists.\n" }
else {
  print "File $mess1 does not exist. Creating $mess1 ...\n";
  open (MESS1, ">$mess1") or die $!;
  close(MESS1); }
if (-e $mess) {
 print "File $mess exists.\n"; }
else {
  print "File $mess does not exist. There is nothing to copy!\n";
  exit; }
my $filesize = -s "messages";
if ($filesize == 0) {
  print "File $mess is empty! Aborting ...\n";
  exit;}
else {
  print "Copying ...\n"; }
#*************************************************************** moving process
#copy content from mess3 to mess 4
  open (MESS4, ">>$mess4") or die $!;
  open (MESS3, "$mess3") or die $!;
  while (<MESS3>) {
  chomp($_);
  printf MESS4 "$_\n"; }
  close(MESS4);
  close(MESS3);
#create fresh mess3
  open (MESS3, ">$mess3") or die $!;
  close(MESS3);
#copy content from mess2 to mess3
  open (MESS3, ">>$mess3") or die $!;
  open (MESS2, "$mess2") or die $!;
  while (<MESS2>) {
  chomp($_);
  printf MESS3 "$_\n"; }
  close(MESS3);
  close(MESS2);
#create fresh mess2
  open (MESS2, ">$mess2") or die $!;
  close(MESS2);
#copy content from mess1 to mess2
  open (MESS2, ">>$mess2") or die $!;
 open (MESS1, "$mess1") or die $!;
  while (<MESS1>) {
  chomp($_);
  printf MESS2 "$_\n"; }
  close(MESS2);
  close(MESS1);
#create fresh mess1
  open (MESS1, ">$mess1") or die $!;
  close(MESS1);
#copy content from mess to mess1
  open (MESS1, ">>$mess1") or die $!;
  open (MESS, "$mess") or die $!;
  while (<MESS>) {
  chomp($_);
  printf MESS1 "$_\n"; }
  close(MESS1);
  close(MESS);
#create fresh mess
  open (MESS, ">$mess") or die $!;
  close(MESS);

thanks!
Ben

This code works fine on my Cygwin.

You posted 90 lines of code but the errors above refer to lines 92 and above ...

It works find for me on one system too...I just cant figure out why not the other system.

Here is the code one more time...just to ensure I copied it right,

#!/usr/bin/perl
 
#use strict;
#use warnings;
 
my $mess = 'messages';
my $mess1 = 'messages.1';
my $mess2 = 'messages.2';
my $mess3 = 'messages.3';
my $mess4 = 'messages.4';
 
if (-e $mess4)
{print "File $mess4 exists.\n"}
else {
        print "File $mess4 does not exist. Creating $mess4...\n";
        open (MESS4, ">$mess4") or die $!;
        close (MESS4); }
 
if (-e $mess3)
 {
        print "File $mess3 exists.\n"}
else {
        print "File $mess3 does not exist. Creating $mess3..\n";
        open (MESS3, ">$mess3") or die $!;
        close (MESS3);}
 
if (-e $mess2)
 {
        print "File $mess2 exists.\n"}
else {
        print "File $mess2 does not exist. Creating $mess2..\n";
        open (MESS2, ">$mess2") or die $!;
        close (MESS2);}
 
if (-e $mess1)
 {
        print "File $mess1 exists.\n"}
else {
        print "File $mess1 does not exist.  Creating $mess1..\n";
        open (MESS1, ">$mess1") or die $!;
        close (MESS1);}
 
if (-e $mess)
 {
        print "File $mess exist.\n";}
else {

        print "File $mess does not exist. There is nothing to copy!\n"
exit; }
 
my $filesize = -s "messages";
        if ($filesize == 0) {
        print "File $mess is empty! Aborting ...\n";
exit; }
else {
        print "Copying ...\n";}
 
open (MESS4, ">>$mess4") or die $!;
open (MESS3, "$mess3") or die $!;

while (<MESSS3>) {
        chomp ($_);
        printf MESS4 "$_\n"; }
        close (MESS4);
        close (MESS3);
 
open (MESS3, ">$mess3") or die $!;
        close (MESS3);
 
open (MESS3, ">>$mess3") or die $!;
open (MESS2, "$mess2") or die $!;
 
while (<MESS2>) {
        chomp ($_);
        printf MESS3 "$_\n; }
        close (MESS3);
        close (MESS2);
 
open (MESS2, ">$mess2:) or die $!;
        close (MESS2);
 
open (MESS2 ">>$mess2") or die $!;
open (MESS1 "$mess1") or die $!;
 
while (<MESS1>) {
        chomp ($_);
        printf MESS2 "$_\n; }
        close (MESS2);
        close (MESS1);

open (MESS1, ">$mess1") or die $!;
        close (MESS1);
 
open (MESS1, ">>$mess1") or die $!;
open (MESS, "$mess) or die $!;
 
while (<MESS>) {
        chomp ($_);
        printf MESS1 "$_\n"; }
        close (MESS1);
        close (MESS);
 
open (MESS, ">$mess") or die $!;
close (MESS);

It didnt put the spaces in there last time. prompt>perl ./myscript.pl
Still getting the errors:

String found where operator expected at line 95, near "open (MESS1, ""
(Might be a runaway multi-line "" string starting on line 92)
(Missing semicolon on previous line?)
String found where operator expected at line 96, near "open (MESS, ""
(Might be a runaway multi-line "" string starting on line 95)
(Missing semicolon on previous line?)
Scalar found where operator expected at line 96, near "open (MESS, "$mess"
(Do you need to predeclare open?)
syntax error at line 50, near "exit"
syntax error at line 80, near "$mess2:"
(Might be a runaway multi-line "" string starting on line 76)
Missing comma after first argument to open function at line 83, near "">>$mess2") "
Missing comma after first argument to open function at line 84, near ""$mess1") "
syntax error at line 95, near "open (MESS1, ""
Missing right curly or square bracket at line 105, at end of line
syntax error at line 105, at EOF
Execution of ./myscript.pl aborted due to compilation errors.

Thanks
Ben

Check my comments:

#!/usr/bin/perl
 
#use strict;
#use warnings;
 
my $mess = 'messages';
my $mess1 = 'messages.1';
my $mess2 = 'messages.2';
my $mess3 = 'messages.3';
my $mess4 = 'messages.4';
 
if (-e $mess4)
{print "File $mess4 exists.\n"}
else {
        print "File $mess4 does not exist. Creating $mess4...\n";
        open (MESS4, ">$mess4") or die $!;
        close (MESS4); }
 
if (-e $mess3)
 {
        print "File $mess3 exists.\n"}
else {
        print "File $mess3 does not exist. Creating $mess3..\n";
        open (MESS3, ">$mess3") or die $!;
        close (MESS3);}
 
if (-e $mess2)
 {
        print "File $mess2 exists.\n"}
else {
        print "File $mess2 does not exist. Creating $mess2..\n";
        open (MESS2, ">$mess2") or die $!;
        close (MESS2);}
 
if (-e $mess1)
 {
        print "File $mess1 exists.\n"}
else {
        print "File $mess1 does not exist.  Creating $mess1..\n";
        open (MESS1, ">$mess1") or die $!;
        close (MESS1);}
 
if (-e $mess)
 {
        print "File $mess exist.\n";}
else {

        print "File $mess does not exist. There is nothing to copy!\n" # missing semicolon ...
exit; }
 
my $filesize = -s "messages";
        if ($filesize == 0) {
        print "File $mess is empty! Aborting ...\n";
exit; }
else {
        print "Copying ...\n";}
 
open (MESS4, ">>$mess4") or die $!;
open (MESS3, "$mess3") or die $!;

while (<MESSS3>) {
        chomp ($_);
        printf MESS4 "$_\n"; }
        close (MESS4);
        close (MESS3);
 
open (MESS3, ">$mess3") or die $!;
        close (MESS3);
 
open (MESS3, ">>$mess3") or die $!;
open (MESS2, "$mess2") or die $!;
 
while (<MESS2>) {
        chomp ($_);
        printf MESS3 "$_\n; }  # missing closing quote
        close (MESS3);
        close (MESS2);
 
open (MESS2, ">$mess2:) or die $!; # missing closing quote
        close (MESS2);
 
open (MESS2 ">>$mess2") or die $!;  # missing comma
open (MESS1 "$mess1") or die $!; # missing comma
 
while (<MESS1>) {
        chomp ($_);
        printf MESS2 "$_\n; } # missing closing quote
        close (MESS2);
        close (MESS1);

open (MESS1, ">$mess1") or die $!;
        close (MESS1);
 
open (MESS1, ">>$mess1") or die $!;
open (MESS, "$mess) or die $!; # quote ...
 
while (<MESS>) {
        chomp ($_);
        printf MESS1 "$_\n"; }
        close (MESS1);
        close (MESS);
 
open (MESS, ">$mess") or die $!;
close (MESS);

---------- Post updated at 11:33 AM ---------- Previous update was at 11:27 AM ----------

This should work:

#!/usr/bin/perl
 
#use strict;
#use warnings;
 
my $mess = 'messages';
my $mess1 = 'messages.1';
my $mess2 = 'messages.2';
my $mess3 = 'messages.3';
my $mess4 = 'messages.4';
 
if (-e $mess4)
{print "File $mess4 exists.\n"}
else {
        print "File $mess4 does not exist. Creating $mess4...\n";
        open (MESS4, ">$mess4") or die $!;
        close (MESS4); }
 
if (-e $mess3)
 {
        print "File $mess3 exists.\n"}
else {
        print "File $mess3 does not exist. Creating $mess3..\n";
        open (MESS3, ">$mess3") or die $!;
        close (MESS3);}
 
if (-e $mess2)
 {
        print "File $mess2 exists.\n"}
else {
        print "File $mess2 does not exist. Creating $mess2..\n";
        open (MESS2, ">$mess2") or die $!;
        close (MESS2);}
 
if (-e $mess1)
 {
        print "File $mess1 exists.\n"}
else {
        print "File $mess1 does not exist.  Creating $mess1..\n";
        open (MESS1, ">$mess1") or die $!;
        close (MESS1);}
 
if (-e $mess)
 {
        print "File $mess exist.\n";}
else {

        print "File $mess does not exist. There is nothing to copy!\n";
exit; }
 
my $filesize = -s "messages";
        if ($filesize == 0) {
        print "File $mess is empty! Aborting ...\n";
exit; }
else {
        print "Copying ...\n";}
 
open (MESS4, ">>$mess4") or die $!;
open (MESS3, "$mess3") or die $!;

while (<MESSS3>) {
        chomp ($_);
        printf MESS4 "$_\n"; }
        close (MESS4);
        close (MESS3);
 
open (MESS3, ">$mess3") or die $!;
        close (MESS3);
 
open (MESS3, ">>$mess3") or die $!;
open (MESS2, "$mess2") or die $!;
 
while (<MESS2>) {
        chomp ($_);
        printf MESS3 "$_\n"; }
        close (MESS3);
        close (MESS2);
 
open (MESS2, ">$mess2") or die $!; 
        close (MESS2);
 
open (MESS2, ">>$mess2") or die $!;
open (MESS1, "$mess1") or die $!;
 
while (<MESS1>) {
        chomp ($_);
        printf MESS2 "$_\n"; } 
        close (MESS2);
        close (MESS1);

open (MESS1, ">$mess1") or die $!;
        close (MESS1);
 
open (MESS1, ">>$mess1") or die $!;
open (MESS, "$mess") or die $!; 
 
while (<MESS>) {
        chomp ($_);
        printf MESS1 "$_\n"; }
        close (MESS1);
        close (MESS);
 
open (MESS, ">$mess") or die $!;
close (MESS);
1 Like

Pludi,

The information you wanted:

OS and Version-
Not working: linux 2.4.34.2-skas3
Working: HP-UX B.11.11

Current shell:
Not working: /bin/bash
Working: command didn't work

Perl Version:
Not working:
This is perl, v5.8.0 built for i386-linux-thread-multi
(with 1 registered patch, see perl -V for more detail)
Working:
This is perl, v5.6.1 built for PA-RISC1.1

Shell version:
Not working: 2.05b.0(1)-release
Working: command didn't work

Ben

---------- Post updated at 06:11 AM ---------- Previous update was at 05:35 AM ----------

YAY!!! It works now!! THanks radoulov!! I have been looking at this script for 2 days straight trying to figure it out!!

Thanks!
Ben