Home > Archive > PERL Beginners > August 2007 > Barewood found where operator expected?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Barewood found where operator expected?
|
|
| Mojorising 2007-08-28, 7:01 pm |
| Hello! I am trying to write a simple Perl script to edit a file using
two other source files named infra_east.txt and infra_west.txt. Please
excuse my ignorance on this. I am sure there are much better ways to
do what I am doing but this is the best I can come up with on my
currently limited knowledge. It's my first time dabbling with Perl.
When I put the following line in my command console I get this error:
Barewood found where operator expected at -e line 1, near "/s/emile"
(Missing operator before emile?)
syntax error at -e line 1, near "/s/emile"
Excecution of -e aborted due to compilation errors.
Here is the script:
perl -i.old -p -e /s/$(cat infra_east.txt)/$(cat infra_west.txt)/ig
configfile.cfg
I basically copied an example from this page. It worked when I tested
it before. Not sure what's different. It appears exactly the same as
what's on that page and my test script.
http://open.itworld.com/nl/unix_sys_adm/09252002/
Can anyone give me a shove in the right direction? I have searched a
grat deal and haven't found anything explaining this. Oddly, a Google
search for this "barewood" error turns up no results. Weird.
Thanks very much for any help!!!
Mike
PS: Please don't use the e-mail in my posting. It hasn't been in use
for years.
| |
| Jeff Pang 2007-08-28, 10:00 pm |
| 2007/8/28, mojorising <moj0rising@hotmail.com>:
>
> Here is the script:
> perl -i.old -p -e /s/$(cat infra_east.txt)/$(cat infra_west.txt)/ig
> configfile.cfg
>
Not tested it but it seems to be:
perl -pi.old -e 's/$(`cat infra_east.txt`)/$(`cat infra_west.txt`)/ig'
notice for the `` symbol.
| |
| John W. Krahn 2007-08-29, 4:00 am |
| Jeff Pang wrote:
> 2007/8/28, mojorising <moj0rising@hotmail.com>:
>
>
> Not tested it but it seems to be:
>
> perl -pi.old -e 's/$(`cat infra_east.txt`)/$(`cat infra_west.txt`)/ig'
>
> notice for the `` symbol.
`` and $() do the same thing, just like in perl where `` and qx() do the same
thing.
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
| |
| Jeff Pang 2007-08-29, 4:00 am |
| 2007/8/29, John W. Krahn <krahnj@telus.net>:
> Jeff Pang wrote:
>
> `` and $() do the same thing, just like in perl where `` and qx() do the same
> thing.
>
Thanks.Never knew that.
| |
| John W. Krahn 2007-08-29, 7:59 am |
| mojorising wrote:
> Hello!
Hello,
> I am trying to write a simple Perl script to edit a file using
> two other source files named infra_east.txt and infra_west.txt. Please
> excuse my ignorance on this. I am sure there are much better ways to
> do what I am doing but this is the best I can come up with on my
> currently limited knowledge. It's my first time dabbling with Perl.
>
> When I put the following line in my command console I get this error:
>
> Barewood found where operator expected at -e line 1, near "/s/emile"
> (Missing operator before emile?)
In Perl a bareword is a string without quotation marks around it. /s/ is the
match operator with the pattern 's' which is matched against the current
contents of the $_ variable. So perl is telling you that you need to put an
operator (perldoc perlop) between /s/ and 'emile'.
> syntax error at -e line 1, near "/s/emile"
> Excecution of -e aborted due to compilation errors.
>
>
> Here is the script:
> perl -i.old -p -e /s/$(cat infra_east.txt)/$(cat infra_west.txt)/ig
> configfile.cfg
A script would normally imply that you had written a Perl program and stored
it in an executable text file, what you have is usually called a "one-liner".
When you enter something on the command line the shell processes it first and
splits it on whitespace.
> I basically copied an example from this page. It worked when I tested
> it before. Not sure what's different. It appears exactly the same as
> what's on that page and my test script.
> http://open.itworld.com/nl/unix_sys_adm/09252002/
The examples from that page are:
perl -i -p -e s/floor/hamper/ig $file
perl -i -p -e 's/hamper = no/hamper = yes/' $file
perl -i -p -e 's/floor\s+yes/floor\tno/' $file
perl -i.bak -p -e s/floor/hamper/ig $file
In the second and third examples there are quotes around the string that the
-e switch executes as perl code. In the other two examples there are no
quotes but there are also no whitespace or shell meta-characters in the perl
code. Note also that there is no '/' character in front of the s///
substitution operator. If you use quotes then $(cat infra_east.txt) and $(cat
infra_west.txt) will not be iterpolated by the shell and perl will not
understand it correctly. If you don't use quotes then the shell will expand
$(cat infra_east.txt) and $(cat infra_west.txt) and they will probably contain
whitespace or regular expression meta-characters and then perl will not
understand it correctly.
This *may* work for you:
perl -i.old -pe"s/\Q$(cat infra_east.txt)/$(cat infra_west.txt)/ig" configfile.cfg
If either infra_east.txt or infra_west.txt contains a '/' character it may not
work. What you should really do is:
perl -i.old -pe'BEGIN { $in = `cat infra_east.txt`; $out = `cat
infra_west.txt` } s/\Q$in/$out/ig' configfile.cfg
> Can anyone give me a shove in the right direction? I have searched a
> grat deal and haven't found anything explaining this. Oddly, a Google
> search for this "barewood" error turns up no results. Weird.
Perl explains all its error and warning messages in the perldiag file, just
enter this from the command line:
perldoc perldiag
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
| |
| Mojorising 2007-08-30, 3:59 am |
| Thanks everyone! I have a much better understanding of what's going on
now and am on my way. Feel pretty silly about the "barewood" thing. I
need to keep my glasses on. :)
I'm working on something else right now so I won't be able to try all
this out for a bit but your comprehensive answers have definitely put
me on solid ground. I'm sure I'll have no trouble now since I know a
little more about what I'm doing.
Mike
|
|
|
|
|