tags:

views:

94

answers:

3

I'm trying to insert/add a line 'COMMENT DUMMY' at the beginnig of a file as a first row if /PATTERN/ not found. I know how to do this with OPEN CLOSE function. Probably after reading the file it should look something like this:

open F, ">", $fn or die "could not open file: $!"; ;
     print F "COMMENT DUMMY\n", @array;
close F;

But I have a need to implement this with the use of the Tie::File function and don't know how.

use strict; 
use warnings; 
use Tie::File;


my $fn = 'test.txt';
tie my @lines, 'Tie::File', $fn or die "could not tie file: $!";

untie @lines; 
+1  A: 

unshift works:

use Tie::File;
my $fn = 'test.txt';
tie my @lines, 'Tie::File', $fn or die "could not tie file: $!";
unshift @lines, "COMMENT DUMMY\n";
untie @lines;
Kinopiko
+1 for the idea.
mr.b
It's working but I need a little more assistance because the code adds 'COMMENT DUMMY' after each processing regardless of /PATTERN/. I added this but it's not working. What I'm doing wrong. if (my $lines !~ /PATTERN/) { unshift @lines, "COMMENT DUMMY\n"; }
thebourneid
How about trying to do that yourself? Hint: wrap the `unshift` in an `if` block. Come back and ask again if you get stuck.
Kinopiko
@thebourneid there's no such thing as `$lines`, and declaring it in the if isn't going to get you anywhere except to let you fail a regex match against undef without `strict vars` complaining :)
hobbs
sorry for the bothering. I wrapped themn already but will find the answer myself.thanks
thebourneid
@Kinopiko : The OP specifically requested to add the line if the file contains `/PATTERN/`. It's not like he came back with the requirement afterwards. Why are you playing hardball?
Zaid
Kinopiko, I didn't mean to play hardball. I realized that instead of making hasty and stupid comments I should try to find the answer myself after you pointed me to the right direction and that is what I did. That's all.Thanks again for your help, and I really mean it.
thebourneid
+1  A: 

Kinopiko's pointed you in the right direction. To complete your need, I'd do the following:

use strict;
use warnings;
use Tie::File;

my $fileName = 'test.txt';

tie my @lines, 'Tie::File', $fileName or die "Unable to tie $fileName: $!";

unshift @lines, "DUMMY COMMENT\n" if grep { /PATTERN/ } @lines;

untie @lines;

Explanation

  • You may already know that although the if statement comes after the unshift statement in writing, it gets evaluated first.
  • When you see grep, think of it as a list filter. Basically, it takes your @lines list and uses it to create a new list with just elements that match /PATTERN/.
  • The if statement evaluates to true if the new, filtered list contains any elements, and false if the list is empty. Based on this, the "DUMMY COMMENT\n" line is added to your @lines list.
Zaid
A: 

The point of tie is to make one thing behave like another. Since you are tie-ing a file to an array, it now acts like an array. You use array operators to do whatever you need to do.

brian d foy