[comp.lang.perl] Novice Question: How is -e supposed to work?

whenry@lindy.stanford.edu (homo obsolescensis) (08/16/90)

I a perl tyro and I am having trouble using the -e switch.  the man
pages give the following example: (lets call this file "go")

	#!/usr/bin/perl -pi.bak
	s/foo/bar/;

and intimate that the result of running go  on file bas will be a
changed file bas.

however, when I try any of the following, the results are the same as
if only the -p switch were set.  that is, the changes are printed to
the screen only, not to the original file.

	go bas
	go <bas

and of course

	go bas >bas		or
	go <bas >bas
just clobber bas

Do I misinterpret what is supposed to be happening?
This happens in patch level 18 on a sun.

Thanks, 
Walter Henry

schaefer@ogicse.ogi.edu (Barton E. Schaefer) (08/16/90)

In article <1990Aug16.070010.26529@morrow.stanford.edu> whenry@lindy.stanford.edu (homo obsolescensis) writes:
} 
} I am having trouble using the -e switch.  the man
} pages give the following example: (lets call this file "go")
} 
} 	#!/usr/bin/perl -pi.bak
} 	s/foo/bar/;
} 
} and intimate that the result of running go  on file bas will be a
} changed file bas.
} 
} however, when I try any of the following, the results are the same as
} if only the -p switch were set.  that is, the changes are printed to
} the screen only, not to the original file.

I'm curious what this has to do with the -e switch (as mentioned in
the subject header).  As speculation, I'll bet that you aren't really
using a file named "go" containing the above two lines, but rather
you are trying to use

	perl -pei.bak 's/foo/bar/;' < bas

Even though that looks sensible to me, it isn't sensible to perl.  (Why
not, Larry?  Is it that tough to parse?)  When perl gets to the 'e' in
"pei.bak" it quits reading from that argument and skips ahead to the
expression.  The "i.bak" is therefore never seen.  Use

	perl -pi.bak -e 's/foo/bar/;' < bas

and everything will be fine.
-- 
Bart Schaefer						schaefer@cse.ogi.edu

whenry@lindy.stanford.edu (homo obsolescensis) (08/17/90)

In article <> schaefer@ogicse.ogi.edu (Barton E. Schaefer) writes:
>In article <> whenry@lindy.stanford.edu (homo obsolescensis) writes:
>} 
>} I am having trouble using the -e switch.  the man
>} pages give the following example: (lets call this file "go")
>} 
>} 	#!/usr/bin/perl -pi.bak
>} 	s/foo/bar/;
>} 
>I'm curious what this has to do with the -e switch (as mentioned in
>the subject header).  

Urgh, am I embarassed.  my question is about -i, not -e.  just a typo,
caused by lack of sleep.  the above fragment is given in the man pages 
as an equivalent to 

	perl -p -i.bak -e "s/foo/bar/;" ...

Mr. Schaefer suggests

>	perl -pi.bak -e 's/foo/bar/;' < bas
>
>and everything will be fine.

This does indeed work as far as -e is concerned (s/foo/bar is done)
but not as far as -i (or, more likely I dont understand -i).  shouldnt
your fragment result in bas being overwritten with the changed text?
and the original bas renamed to bas.bak?
when I do what you suggest bas is just written to the screen (with, of
course, the changed text).  the man pages give yet a third
"equivalent" example (much longer, so I wont quote it), in which
bas is indeed overwritten (and a .bak made)

(Im really sorry about the confusion caused by my typo and much
appreciate your answer, which I learned something useful anyway).
Walter Henry

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (08/17/90)

In article <1990Aug16.070010.26529@morrow.stanford.edu> whenry@lindy.stanford.edu (homo obsolescensis) writes:
: 
: I a perl tyro and I am having trouble using the -e switch.  the man
: pages give the following example: (lets call this file "go")
: 
: 	#!/usr/bin/perl -pi.bak
: 	s/foo/bar/;
: 
: and intimate that the result of running go  on file bas will be a
: changed file bas.
: 
: however, when I try any of the following, the results are the same as
: if only the -p switch were set.  that is, the changes are printed to
: the screen only, not to the original file.
: 
: 	go bas
: 	go <bas
: 
: and of course
: 
: 	go bas >bas		or
: 	go <bas >bas
: just clobber bas
: 
: Do I misinterpret what is supposed to be happening?

	go bas

ought to work the way you expect.  The file bas should be renamed bas.bak
and a new file bas should be created.  Is your first line actually
#!/usr/bin/perl or is it possibly some longer pathname that happens
to be just long enough to wipe out the switch?  That's the only thing
I can think of, apart from mangled kits.

Larry

schaefer@ogicse.ogi.edu (Barton E. Schaefer) (08/17/90)

In article <1990Aug16.172303.12982@morrow.stanford.edu> whenry@lindy.stanford.edu (homo obsolescensis) writes:
} In article <> schaefer@ogicse.ogi.edu (Barton E. Schaefer) writes:
} >	perl -pi.bak -e 's/foo/bar/;' < bas
} 
} This does indeed work as far as -e is concerned (s/foo/bar is done)
} but not as far as -i (or, more likely I dont understand -i).  shouldnt
} your fragment result in bas being overwritten with the changed text?

No, because I also made a typo.  Get rid of that < character.

If it doesn't work when you use

	perl -pi.bak -e 's/foo/bar/;' bas

then something funny is going on.
-- 
Bart Schaefer						schaefer@cse.ogi.edu