0

Hi,
i have the following script:

#!/bin/perl -w
use strict;

$ENV{"PATH"} = ".;" . $ENV{"PATH"};

print "Revesing the history file...\n";
print reverse <> history.txt > history_rev.txt;
print "Reversing the XpressClients file\n";
print reverse <> XpressClients.txt > Xp_clients.txt;
print "Parsing the XpressClients file...\n";
require 'Clients.pl' Xp_clients.txt > clients_file.txt;

the files are located in the same dir as the script.
the problem is - when i run the script i get this error:
Bareword found where operator expected at ./Filesx_Check.pl line 24, near "<> history"
(Missing operator before history?)

i get this error with "history", "XpressClients" and "Xp_clients", i.e, every file i mention.
i tried adding the directoy to PATH but that didn't help. i even changed the files premissions to rwx, but again, to no avail.

i'm seriously frustrated.
ANY suggestion will be appreciated.
Thanks!

3
Contributors
8
Replies
9
Views
8 Years
Discussion Span
Last Post by yair7190
0

When you use print, you should quote strings.

print reverse <>, "history.txt > history_rev.txt\n";

On the other hand, if you are expecting perl to know those are input and output files you must not have been awake in perl class. ;)

0

that's the wired thing - it works fine just the way it is in my computer, but in another computer it prints the error message.

can you point me to what im doing wrong?
do i need to open the files and insert them to arrays?

0

No, i tried that too.
It is possible, im just not sure why it doesnt work...
How else can i tell perl these are input and output files? any idea?
Thnaks

0

I did a little horsing around.

I can get reverse to work with arrays:

print reverse(@array);

But, what you are trying uses standard input (<>). If you want to do that, I believe you have to run perl via command line:

perl -e 'print reverse <>' file1 file2 file3 ....

The above is taken from this page: http://www.unixguide.net/unix/perl_oneliners.shtml

To do what you want, within a perl script, may require opening files and reading them:

open(FILE_HANDLER,"/path/to/file");
my @lines = <FILE_HANDLER>;
close(FILE_HANDLER);

Hope that helps.

0

Hi Stylish,

You did help my and you're wright.
In order to do :
perl -e 'print reverse <>' sourcefile.txt > 'targetfile.txt'
In a perl script i need to open files.
Problem is, that oneliner (perl -e...) is faster and prints the file, as is, backwords. In perl script when i do:
print TARGET reverse <@sourcefile>; (when TARGET is the file handler)
I get the file without spaces. for example, if it was:
daniweb forums
are helpful.
It will be :
daniwebforumsarehelpful.

I'll to play with it for a while, maybe things will start working wright.
Thank you both!
To be continued...

0

open(IN, "/path/to/file");
open(OUT,">",'path/to/outfile');
print OUT reverse <IN>;
close(IN);
close(OUT);

The only problem is that if there is not a newline at the end of the last line of the IN file, the reversed OUT file will have the first and second lines appended together.

0

Reverse ("@sourcefile") - made all the difference.
Works great.
Thank KevinADC & Stylish!

This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.