Re: Putting a line in a specific place in a file



samasama wrote:
anno4000@xxxxxxxxxxxxxxxxxxxxxx wrote:
samasama <bryan@xxxxxxxxxxxxxx> wrote in comp.lang.perl.misc:

[positioning a file handle]

This finds the lines correctly but if I try to print after $gpg_line,
the line below gets fubar.
I know I could use Tie::File, but i really want to understand what's
going on and how to insert my line after $gpg_line correctly.
Well, you can't.

A file is really a sequence of bytes, no more, no less. You can
append to the end of a file, but writing anywhere else implies
overwriting what's already there.


Duh... Yeah I feel dumb : P I don't know why I thought I could just
print the line.

Pity we can no longer refer to the FAQ for this question.

Perlfaq5 "How do I change one line in a file/delete a line..." used to
explain this problem in detail. These days it just refers to Tie::File,
another tool that makes files look like a sequence (a Perl array) of lines.

Yeah, Tie::File is great, but I needed to learn the steps in writing my
own parser, for future projects.

While, I've figured out what I needed to do and I'll paste the last of
the code below... I still don't understand what if ( ( $. == $base_line
.. /gpgkey\=/i ) =~ /E/ ) { translates too.
It looks like $. == $base_line go down using .. until we hit gpgkey,
and I have no idea what the /E/ means. I'd really like to understand
that.

Perhaps a demonstration may help:

$ seq 10 18 | perl -lne' my $x = 3 .. /16/; print "Line Number: $. Contents:
$_ Flip-flop: $x" '
Line Number: 1 Contents: 10 Flip-flop:
Line Number: 2 Contents: 11 Flip-flop:
Line Number: 3 Contents: 12 Flip-flop: 1
Line Number: 4 Contents: 13 Flip-flop: 2
Line Number: 5 Contents: 14 Flip-flop: 3
Line Number: 6 Contents: 15 Flip-flop: 4
Line Number: 7 Contents: 16 Flip-flop: 5E0
Line Number: 8 Contents: 17 Flip-flop:
Line Number: 9 Contents: 18 Flip-flop:



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
.



Relevant Pages

  • Re: RFC: utils.pm
    ... yeah; ... but I'm tired to writing this god damn line each time I want to ... > when dealing with ie. CGI parameters, ... > sub as_boolean { ...
    (comp.lang.perl.misc)
  • Re: What to do when your son tell you he hates you.
    ... i'm saying that employers sometimes ask for the impossible ... Yeah, I remember my husband once showing me an employment ad requiring ... HR person writing the criteria and apply anyway, ...
    (misc.kids)
  • Re: Ping Bill
    ... I get tired of writing also. ... resizing/cropping/selecting of pictures that eats up a lot of time. ... Yeah, it's not accurate, but ASCII art has it's limitations. ... Get started with a SEO report of your site: ...
    (alt.internet.search-engines)
  • Re: Complex Made Simple: Update
    ... Yeah, there's a certain logic in the whole book. ... More awkward writing. ... "Understanding Godel isn't about following his formal proof. ...
    (sci.math)
  • Re: The Case for Working With Your Hands
    ... It's interesting to see how his thinking (or at least his writing) has ... I suspect the first one took months to write. ... Yeah, or friends off which he bounced his ideas. ... The _New Atlantis_ piece was published originally in the hard-copy ...
    (rec.crafts.metalworking)