Beginner: read and print same file

I have to bother you once again. I have a script with __DATA__ which will be
transformed later into a LaTeX Table. Just now I am stuck to insert these
DATA after a keyword "% begin", but I am not finding the solution.

Wasteful to capture in the pattern match just to throw it away. It's
also wasteful to test a substitution before doing it. You can use the
if condition to check whether the substitution happened and avoid
pattern matching first.
> {
> s/(% begin)/"$1\n" . join ("\n",@lines)/e; ###And here my problem!

Considering the print statements that follow this part, I have to think
you've missed a fundamental concept of reading and writing files. As
stated above, you didn't open the file in the right mode, but even if
you had, trying to read it and write to it at the same time cannot be
done by a simple read. If you can't read the file in and manipulate it
in memory, you might consider the Tie::File module.

You could have saved us some time by indicating the error messages you
got, rather than commenting them out and asking an unrelated question.

On 13.08.2006 23:00, in article ,
"Michele Dondi" <> wrote:
> On Sun, 13 Aug 2006 20:50:32 +0200, Marek Stepanek <>
> wrote:
>
>> I have to bother you once again. I have a script with __DATA__ which will be
>> transformed later into a LaTeX Table. Just now I am stuck to insert these
>> DATA after a keyword "% begin", but I am not finding the solution.
>
> Since your actual question seems to have been already addressed... an
> ad for a relatively unknown, but very nice, piece of software:
> PerlTeX. Did you try it?
>
>
> Michele

I am blushing, but I don't understand this one ! I will read once again
tomorrow:

perldoc -f open
perldoc perlopentut
perldoc Tie::File

Just for now I changed Matt's suggestion to:

my $out_file = "calc_hours_table02.tex";
# open my $out, '>>', $out_file or die "Could not open $out_file for
writing: $!"; # how I have to handle "my $out" later in my script?
open OUT, "+<$out_file" or die "Could not open $out_file for writing: $!";

which is inserting at the END of my out_file the @lines, instead of
inserting it AFTER % begin :

[please don't intersperse your questions in quoted text]
> # how I have to handle "my $out" later in my script?
>

You can use a scalar to hold the filehandle:

print $out "I'm printing!";

There are a number of benfits to using a scalar, but rather than give
you an incomplete explanation I'll leave you to read up on it.
> open OUT, "+<$out_file" or die "Could not open $out_file for writing: $!";
> which is inserting at the END of my out_file the @lines, instead of
> inserting it AFTER % begin

If you want to do an in-place edit then you can use '+<', but not the
way you're trying to read/manipulate the file. You'd need to be using
syswrite/sysseek, and that's not for the faint of heart. Have a look at
perlopentut. It will explain the behaviour you're experiencing.

Again, though, the normal solutions are to slurp the file in,
manipulate it and write it back out or use Tie::File.

MG> If you want to do an in-place edit then you can use '+<', but not the
MG> way you're trying to read/manipulate the file. You'd need to be using
MG> syswrite/sysseek, and that's not for the faint of heart. Have a look at
MG> perlopentut. It will explain the behaviour you're experiencing.

MG> Again, though, the normal solutions are to slurp the file in,
MG> manipulate it and write it back out or use Tie::File.

a future feature of File::Slurp will be the subs edit_file and
edit_file_lines. they will slurp in a file, call a code ref you pass in
with either the file or each line in $_, then write back out $_ to the
same file.

dunno when this will be done. it won't be much module code and most of
the work will be in option handling and writing tests. volunteers are
welcome to help!

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!