add a 3 lines of text in a file before "package" in file

Discussion in 'Perl Misc' started by mike, Sep 8, 2009.

  1. mike

    mike Guest

    Hi,

    I am using perl and need to add some text lines before the word
    "package" in the file. I have read about Tie::File
    and I am trying to use it as below. But I am unable to see how I can
    match the word "package" and add
    /* ---- Insert text here ---- */
    on the line before "package".

    Any ideas?

    //mike

    sub modify {
    my @line_array;
    my $line;
    my ($file) = @_;
    print "Modifying file, $file\n";
    tie @line_array, 'Tie::File', $file or die "Can not tie file:$!";
    for $line(@line_array) {
    //
    }
    untie @line_array;

    }
     
    mike, Sep 8, 2009
    #1
    1. Advertising

  2. mike

    Uri Guttman Guest

    >>>>> "BM" == Ben Morrow <> writes:

    BM> Quoth mike <>:
    >>
    >> I am using perl and need to add some text lines before the word
    >> "package" in the file. I have read about Tie::File
    >> and I am trying to use it as below. But I am unable to see how I can
    >> match the word "package" and add
    >> /* ---- Insert text here ---- */
    >> on the line before "package".


    BM> Is the file likely to be large (which probably means several hundred MB,
    BM> nowadays)? If not, it would be easier to use File::Slurp and do a simple
    BM> s/// on the whole file before writing it out again.


    yes, slurping is fine for many megabytes today which is something people
    have to learn. text files have not grown like ram sizes have (other than
    biogen and similar stuff). slurping this is easy and fast:

    (untested)

    use File::Slurp ;

    my $text = read_file( $file ) ;
    $text =~ s{(^.*package.*$)}
    {/* ---- Insert text here ---- */\n$1}m ;
    write_file( $file, $text ) ;

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Sep 8, 2009
    #2
    1. Advertising

  3. mike wrote:
    >
    > I am using perl and need to add some text lines before the word
    > "package" in the file.


    $ echo "one
    two
    three
    four package
    five
    six" | perl -pe'/package/&&print "new text 1\nnew text 2\nnew text 2\n"'
    one
    two
    three
    new text 1
    new text 2
    new text 2
    four package
    five
    six



    John
    --
    Those people who think they know everything are a great
    annoyance to those of us who do. -- Isaac Asimov
     
    John W. Krahn, Sep 8, 2009
    #3
  4. Ben Morrow wrote:
    > Quoth Tad J McClellan <>:
    >> mike <> wrote:
    >>> sub modify {
    >>> my @line_array;
    >>> my $line;
    >>> my ($file) = @_;
    >>> print "Modifying file, $file\n";
    >>> tie @line_array, 'Tie::File', $file or die "Can not tie file:$!";

    >>
    >> foreach my $i ( 0 .. $#line_array) {

    >
    > This will read the entire file to count the lines before it starts,
    > removing any benefit of using Tie::File.


    That is far from the only benefit of Tie::File. I don't think it is
    even one of the benefits of Tie::File to start with. Not that I think
    that Tie::File has all that many benefits to start with. One of the
    benefits it does have is letting you write into the middle of a file in
    a way that syntactically and logically easy, though quite inefficient.
    This benefit is maintained, and ISTM is the whole point.

    Xho
     
    Xho Jingleheimerschmidt, Sep 9, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Wright
    Replies:
    0
    Views:
    541
    Joe Wright
    Jul 27, 2003
  2. Murali
    Replies:
    2
    Views:
    595
    Jerry Coffin
    Mar 9, 2006
  3. dean
    Replies:
    0
    Views:
    259
  4. dean
    Replies:
    3
    Views:
    296
    Dennis Lee Bieber
    Apr 4, 2009
  5. Cah Sableng
    Replies:
    0
    Views:
    247
    Cah Sableng
    Apr 23, 2007
Loading...

Share This Page