Replacing a line

Discussion in 'Perl Misc' started by Ved, Aug 6, 2007.

  1. Ved

    Ved Guest

    Hi all,
    I have to replace commented line in a list of " .cpp" file .
    Line looks like this:
    //#define BBPRxChanRouteFileLoadInput 1

    i.e. The line format :
    //#define (FileName)FileLoadInput 1
    is to be replaced with
    #define (FileName)FileLoadInput 1

    I have written a code using Tie::File which is mentioned below.
    I want to do two things:

    1) Remove trailing .cpp in $dir ?

    2) What to do in this to get (/ //#define $dirFileLoadInput 1 /) in
    the for loop ?


    For above example file name in my.rxfiles is:
    BBPRxChanRoute.cpp

    Thanks in advance
    Ved
    #######################################################


    #!/usr/local/bin/perl
    use strict;
    use warnings;
    use Tie::File;

    my $dir_list = 'my.rxfiles'; #contains list of .cpp files
    open my $fh,'<',$dir_list or die "Cannot read $dir_list: $!\n";
    while (my $dir = <$fh>) {
    chop $dir;
    my $file = "$dir";
    chomp $file;
    if (-e $dir) {
    process_one_file($dir);
    } else {
    warn "File $dir does not exist; skipping\n";
    }
    }

    #Using core module Tie::File to process a file in this subroutine
    sub process_one_file {
    my $dir = shift;
    print "Processing $dir\n";
    tie my @array, 'Tie::File', $dir or die "tie error $dir: $!
    \n" ;
    for (@array) {
    if (/\//#define $dirFileLoadInput 1/) #what to do here ??
    {
    $_ = #define $dirFileLoadinput 1 ;
    last;
    }
    }
    }
     
    Ved, Aug 6, 2007
    #1
    1. Advertising

  2. Ved

    J. Gleixner Guest

    Ved wrote:
    > Hi all,
    > I have to replace commented line in a list of " .cpp" file .
    > Line looks like this:
    > //#define BBPRxChanRouteFileLoadInput 1
    >
    > i.e. The line format :
    > //#define (FileName)FileLoadInput 1
    > is to be replaced with
    > #define (FileName)FileLoadInput 1
    >
    > I have written a code using Tie::File which is mentioned below.
    > I want to do two things:
    >
    > 1) Remove trailing .cpp in $dir ?


    perldoc -f rename

    >
    > 2) What to do in this to get (/ //#define $dirFileLoadInput 1 /) in
    > the for loop ?


    > #!/usr/local/bin/perl
    > use strict;
    > use warnings;
    > use Tie::File;
    >
    > my $dir_list = 'my.rxfiles'; #contains list of .cpp files
    > open my $fh,'<',$dir_list or die "Cannot read $dir_list: $!\n";
    > while (my $dir = <$fh>) {
    > chop $dir;
    > my $file = "$dir";
    > chomp $file;


    Why have $dir and $file? Use more descriptive variable
    names.

    open( my $cpp_files, '<', $dir_list )
    or die "Cannot read $dir_list: $!\n";
    while ( my $cpp_file = <$cpp_files> )
    {
    chomp $cpp_file;
    if ( -e $cpp_file )
    {
    process_one( $cpp_file );
    }
    else { ...}
    }

    > if (-e $dir) {
    > process_one_file($dir);
    > } else {
    > warn "File $dir does not exist; skipping\n";
    > }
    > }
    >
    > #Using core module Tie::File to process a file in this subroutine
    > sub process_one_file {
    > my $dir = shift;


    Since you're processing a 'file' not a 'dir'ectory, name your
    variables accordingly:

    my $file = shift;

    print "Processing $file\n";
    etc.

    > print "Processing $dir\n";
    > tie my @array, 'Tie::File', $dir or die "tie error $dir: $!
    > \n" ;
    > for (@array) {
    > if (/\//#define $dirFileLoadInput 1/) #what to do here ??
    > {
    > $_ = #define $dirFileLoadinput 1 ;
    > last;


    You only want to modify the first one?

    > }
    > }
    > }
    >


    for( @array )
    {
    # Need to escape the $
    if( m{^//#define \$dirFileLoadInput 1$} )
    {
    s{^//}{};
    last;
    }
    }

    Documentation on regular expressions:

    perldoc perlretut
     
    J. Gleixner, Aug 6, 2007
    #2
    1. Advertising

  3. Ved

    Ved Guest

    > > for (@array) {
    > > if (/\//#define $dirFileLoadInput 1/) #what to do here ??
    > > {
    > > $_ = #define $dirFileLoadinput 1 ;
    > > last;

    >
    > You only want to modify the first one?
    >
    > > }


    No, I want to modify all
    Thanks for the reply.
     
    Ved, Aug 6, 2007
    #3
  4. Ved

    J. Gleixner Guest

    Ved wrote:
    >>> for (@array) {
    >>> if (/\//#define $dirFileLoadInput 1/) #what to do here ??
    >>> {
    >>> $_ = #define $dirFileLoadinput 1 ;
    >>> last;

    >> You only want to modify the first one?


    I should have said "the first occurrence of that line in the file."

    >>
    >>> }

    >
    > No, I want to modify all


    If you want to modify all occurrences, then remove the 'last;'

    perldoc -f last

    You could shorten it a lot, using a few command line options (untested):

    perl -pi -e 's{^//#define \$dirFileLoadInput 1$}{#define
    \$dirFileLoadInput 1}g' `cat file_containing_cpp_files`

    Or a bit shorter as:

    perl -pi -e 's{^//}{} if m{^//#define \$dirFileLoadInput 1$}' `cat
    file_containing_cpp_files`
     
    J. Gleixner, Aug 6, 2007
    #4
  5. Ved <> wrote:

    > //#define (FileName)FileLoadInput 1
    > is to be replaced with
    > #define (FileName)FileLoadInput 1


    > 1) Remove trailing .cpp in $dir ?



    $dir =~ s/\.cpp$//;


    > 2) What to do in this to get (/ //#define $dirFileLoadInput 1 /) in
    > the for loop ?



    > if (/\//#define $dirFileLoadInput 1/) #what to do here ??



    s!//(#define ${dir}FileLoadInput 1)!$1!;


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad McClellan, Aug 6, 2007
    #5
  6. Ved

    Ved Guest

    Thanks for replies,

    Below is the code I have modified now.
    $module_name and $cpp_file are coming perfectly fine.

    But, I am not able to see this:
    print "Finally I am here \n";

    It means that "if" condition is not being fuifilled.
    if( m{^//#define \$module_nameFileLoadInput $} )
    ^
    |
    Is something missing at (this place)
    i.e. after $module_name in the if condtion ?


    Regards
    Ved




    #####################################################

    #!/usr/local/bin/perl
    use strict;
    use warnings;
    use Tie::File;

    my $cpp_file_list = 'my.rxfiles';
    open my $fh,'<',$cpp_file_list or die "Cannot read $cpp_file_list:
    $!\n";
    while (my $cpp_file = <$fh>) {
    chop $cpp_file;
    my $module_name = "$cpp_file";
    $module_name =~ s/\.cpp$//;#removing trainling .cpp
    print "this is name $module_name\n";
    print "this is CPP $cpp_file\n";
    chomp $module_name;#added
    if (-e $cpp_file) {
    process_one_file($cpp_file);
    } else {
    warn "File $cpp_file does not exist; skipping\n";
    }
    }

    #Using core module Tie::File to process a file in this subroutine
    sub process_one_file {
    my $cpp_file = shift;
    print "Processing $cpp_file\n";
    tie my @array, 'Tie::File', $cpp_file or die "tie error
    $cpp_file: $!\n" ;

    for (@array)
    {
    print "I am at this place \n";
    # Need to escape the $
    if( m{^//#define \$module_nameFileLoadInput $} )
    #if( s!//(#define ${module_name}FileLoadInput 1)!$1!)
    {
    print "Finally I am here \n";
    s{^//}{};
    # last;
    }
    }
    }
     
    Ved, Aug 7, 2007
    #6
  7. Ved <> wrote:

    > It means that "if" condition is not being fuifilled.
    > if( m{^//#define \$module_nameFileLoadInput $} )
    > ^
    > |
    > Is something missing at (this place)



    Yes, a closing curly bracket.


    > i.e. after $module_name in the if condtion ?



    You also need an opening curly bracket in the appropriate place.

    You also do NOT need (or want) the dollar sign to be backslashed.


    > while (my $cpp_file = <$fh>) {
    > chop $cpp_file;



    You should use chomp() instead of chop() for removing newlines.


    > my $module_name = "$cpp_file";



    my $module_name = $cpp_file;

    See:

    perldoc -q vars

    What’s wrong with always quoting "$vars"?



    > $module_name =~ s/\.cpp$//;#removing trainling .cpp
    > print "this is name $module_name\n";
    > print "this is CPP $cpp_file\n";
    > chomp $module_name;#added



    Why did you think that adding that was a good idea?

    What did you expect that it would do for you?

    (hint: it does nothing for you here.)


    > if( m{^//#define \$module_nameFileLoadInput $} )
    > #if( s!//(#define ${module_name}FileLoadInput 1)!$1!)



    I did not mean that you should put the s/// in the if condition.

    I meant that you should use the s/// _instead of_ the if condition.

    There is no need to check if it matches first, simply do the s///
    and it will do nothing if it does not happen to match.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad McClellan, Aug 7, 2007
    #7
  8. Ved

    Ved Guest

    On Aug 7, 3:17 am, "J. Gleixner" <>
    wrote:
    > Ved wrote:
    > > Hi all,
    > > I have to replace commented line in a list of " .cpp" file .
    > > Line looks like this:
    > > //#define BBPRxChanRouteFileLoadInput 1

    >
    > > i.e. The line format :
    > > //#define (FileName)FileLoadInput 1
    > > is to be replaced with
    > > #define (FileName)FileLoadInput 1

    >
    > > I have written a code using Tie::File which is mentioned below.
    > > I want to do two things:

    >
    > > 1) Remove trailing .cpp in $dir ?

    >
    > perldoc -f rename
    >
    >
    >
    > > 2) What to do in this to get (/ //#define $dirFileLoadInput 1 /) in
    > > the for loop ?
    > > #!/usr/local/bin/perl
    > > use strict;
    > > use warnings;
    > > use Tie::File;

    >
    > > my $dir_list = 'my.rxfiles'; #contains list of .cpp files
    > > open my $fh,'<',$dir_list or die "Cannot read $dir_list: $!\n";
    > > while (my $dir = <$fh>) {
    > > chop $dir;
    > > my $file = "$dir";
    > > chomp $file;

    >
    > Why have $dir and $file? Use more descriptive variable
    > names.
    >
    > open( my $cpp_files, '<', $dir_list )
    > or die "Cannot read $dir_list: $!\n";
    > while ( my $cpp_file = <$cpp_files> )
    > {
    > chomp $cpp_file;
    > if ( -e $cpp_file )
    > {
    > process_one( $cpp_file );
    > }
    > else { ...}
    >
    > }
    > > if (-e $dir) {
    > > process_one_file($dir);
    > > } else {
    > > warn "File $dir does not exist; skipping\n";
    > > }
    > > }

    >
    > > #Using core module Tie::File to process a file in this subroutine
    > > sub process_one_file {
    > > my $dir = shift;

    >
    > Since you're processing a 'file' not a 'dir'ectory, name your
    > variables accordingly:
    >
    > my $file = shift;
    >
    > print "Processing $file\n";
    > etc.
    >
    > > print "Processing $dir\n";
    > > tie my @array, 'Tie::File', $dir or die "tie error $dir: $!
    > > \n" ;
    > > for (@array) {
    > > if (/\//#define $dirFileLoadInput 1/) #what to do here ??
    > > {
    > > $_ = #define $dirFileLoadinput 1 ;
    > > last;

    >
    > You only want to modify the first one?
    >
    > > }
    > > }
    > > }

    >
    > for( @array )
    > {
    > # Need to escape the $
    > if( m{^//#define \$dirFileLoadInput 1$} )
    > {
    > s{^//}{};
    > last;
    > }
    > }
    >
    > Documentation on regular expressions:
    >
    > perldoc perlretut

    Hi Gleixner,

    Why is it not entering in this condition ?

    if( m{^//#define \$module_nameFileLoadInput 1$} )
     
    Ved, Aug 8, 2007
    #8
  9. Ved <> wrote:

    [ Please learn to compose followups properly.
    Quoting 100 lines to add 2 lines will make you go invisible...
    ]


    > Why is it not entering in this condition ?
    >
    > if( m{^//#define \$module_nameFileLoadInput 1$} )



    Probably because the string in $_ does not contain a '$' followed
    by an 'm' followed by an 'o' followed by a 'd'...




    You should attempt to write a short and complete program that we
    can run that illustrates your problem.

    Have you seen the Posting Guidelines that are posted here frequently?


    ------------------------------
    #!/usr/bin/perl
    use warnings;
    use strict;

    $_ = '//#define BBPRxChanRouteFileLoadInput 1';
    my $module_name = 'BBPRxChanRoute';
    if( m{^//#define ${module_name}FileLoadInput 1$} )
    { print "true\n" }
    else
    { print "false\n" }
    ------------------------------


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad McClellan, Aug 9, 2007
    #9
  10. Ved

    J. Gleixner Guest

    Ved wrote:
    > On Aug 7, 3:17 am, "J. Gleixner" <>
    > wrote:
    >> Ved wrote:
    >>> Hi all,
    >>> I have to replace commented line in a list of " .cpp" file .
    >>> Line looks like this:
    >>> //#define BBPRxChanRouteFileLoadInput 1
    >>> i.e. The line format :
    >>> //#define (FileName)FileLoadInput 1
    >>> is to be replaced with
    >>> #define (FileName)FileLoadInput 1

    [...]
    >> for( @array )
    >> {
    >> # Need to escape the $
    >> if( m{^//#define \$dirFileLoadInput 1$} )
    >> {
    >> s{^//}{};
    >> last;
    >> }
    >> }
    >>
    >> Documentation on regular expressions:
    >>
    >> perldoc perlretut

    > Hi Gleixner,
    >
    > Why is it not entering in this condition ?
    >
    > if( m{^//#define \$module_nameFileLoadInput 1$} )
    >


    Sorry, my fault. I misssed that $dir was a variable, in
    your match. For some reason I thought you were trying to
    actually match a string that contained '$dirFileLoadInput'.

    ${module_name}FileLoadInput
     
    J. Gleixner, Aug 9, 2007
    #10
  11. Ved

    Ved Guest

    On Aug 9, 5:38 am, Tad McClellan <> wrote:
    > Ved <> wrote:
    >
    > [ Please learn to compose followups properly.
    > Quoting 100 lines to add 2 lines will make you go invisible...
    > ]
    >
    > > Why is it not entering in this condition ?

    >
    > > if( m{^//#define \$module_nameFileLoadInput 1$} )

    >
    > Probably because the string in $_ does not contain a '$' followed
    > by an 'm' followed by an 'o' followed by a 'd'...
    >
    > You should attempt to write a short and complete program that we
    > can run that illustrates your problem.
    >
    > Have you seen the Posting Guidelines that are posted here frequently?
    >
    > ------------------------------
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    >
    > $_ = '//#define BBPRxChanRouteFileLoadInput 1';
    > my $module_name = 'BBPRxChanRoute';
    > if( m{^//#define ${module_name}FileLoadInput 1$} )
    > { print "true\n" }
    > else
    > { print "false\n" }
    > ------------------------------
    >
    > --
    > Tad McClellan
    > email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"



    Hi Tad and Gleixner,
    Thanks for the replies.
    Tad I will be more adherant to the guidelines in future.

    Now the code looks like this:
    ################################################################
    #!/usr/local/bin/perl
    #use strict;
    use warnings;
    use Tie::File;

    my $cpp_file_list = 'my.rxfiles';
    open my $fh,'<',$cpp_file_list or die "Cannot read $cpp_file_list:
    $!\n";
    while (my $cpp_file = <$fh>) {
    chop $cpp_file;
    our $module_name = "$cpp_file";
    # my $module_name = "$cpp_file";
    $module_name =~ s/\.cpp$//;#removing trainling .cpp
    print "this is name $module_name\n";
    print "this is CPP $cpp_file\n";
    if (-e $cpp_file) {
    process_one_file($cpp_file);
    } else {
    warn "File $cpp_file does not exist; skipping\n";
    }
    }

    #Using core module Tie::File to process a file in this subroutine
    sub process_one_file {
    my $cpp_file = shift;
    print "Processing $cpp_file\n";
    tie my @array, 'Tie::File', $cpp_file or die "tie error
    $cpp_file: $!\n" ;

    for (@array) #Each line should come one by one
    {
    if( m{^//#define ${module_name}FileLoadInput 1$} )
    { print "true $module_name \n"
    s{^//}{}; # Is something wrong here ?
    }
    else
    #{ print "false\n" }
    { print "false $module_name \n"}

    }


    }

    ############## my.rxfiles contains this ###############
    abc.cpp


    ############### abc.cpp contains this ###########
    //#define abcFileLoadInput 1
    // #define abcFileLoadInput 1

    ##################################################
    ##################################################
    PROBLEM (1)
    Now when I use "strict" I am getting following error:

    Variable "$module_name" is not imported at rx_top_script.pl line 30.
    Variable "$module_name" is not imported at rx_top_script.pl line 31.
    Variable "$module_name" is not imported at rx_top_script.pl line 36.
    Global symbol "$module_name" requires explicit package name at
    rx_top_script.pl line 30.
    Global symbol "$module_name" requires explicit package name at
    rx_top_script.pl line 31.
    Global symbol "$module_name" requires explicit package name at
    rx_top_script.pl line 36.
    ###################
    When I comment strict I am getting this which perfectly fine:
    this is name abc
    this is CPP abc.cpp
    Processing abc.cpp
    true abc
    false abc
    ###################
    PROBLEM(2)
    I thought to ignore STRICT for the time being and try to achieve what
    I desire.
    So in the for loop I put the line :
    s{^//}{};
    ##################
    But it gave me this error:
    syntax error at rx_top_script.pl line 32, near "s{^//}{}"
    ##################

    Do I need to rectify the STRICT issue(problem) to get rid of
    PROBLEM(2) ?

    If yes :
    Is this the best link to solve PROBLEM(1) ?
    http://www.perl.com/pub/a/2002/04/23/mod_perl.html

    Or is there something even better?

    Regards
    Ved
     
    Ved, Aug 11, 2007
    #11
  12. Ved <> wrote:
    > On Aug 9, 5:38 am, Tad McClellan <> wrote:
    >> Ved <> wrote:
    >>
    >> [ Please learn to compose followups properly.
    >> Quoting 100 lines to add 2 lines will make you go invisible...
    >> ]



    [ Please learn to compose followups properly.
    Please do this very soon.
    ]


    >> You should attempt to write a short and complete program that we
    >> can run that illustrates your problem.



    You should attempt to write a short and complete program that we
    can run that illustrates your problem.


    > Tad I will be more adherant to the guidelines in future.



    Now is the future!


    > #use strict;



    You lose all of the benfits of the strict pragma when you comment it out.


    > chop $cpp_file;



    You should still use chomp() instead of chop() for removing newlines.


    > our $module_name = "$cpp_file";

    ^^^
    ^^^

    Why did you put that there?

    Why are you still quoting that lone variable?

    Are you reading the followups?

    Is there anybody in there? Just nod if you can hear me.


    > if( m{^//#define ${module_name}FileLoadInput 1$} )
    > { print "true $module_name \n"
    > s{^//}{}; # Is something wrong here ?
    > }
    > else
    > { print "false $module_name \n"}




    As I also told you before, there is no need to test the match
    before doing the substitution, so you should not test the
    match before doing the substitution.

    if ( s{^//(#define ${module_name}FileLoadInput 1)}{$1} ) {
    print "true $module_name\n"
    }
    else {
    print "false $module_name\n"
    }



    > PROBLEM (1)
    > Now when I use "strict" I am getting following error:


    > Global symbol "$module_name" requires explicit package name at
    > rx_top_script.pl line 30.



    "Coping with Scoping":

    http://perl.plover.com/FAQs/Namespaces.html

    and

    perldoc strict

    and

    perldoc my



    > PROBLEM(2)
    > I thought to ignore STRICT for the time being and try to achieve what
    > I desire.



    That is not a good thought. Abandon it.

    Instead learn how to control the scope of variables in Perl.


    > Do I need to rectify the STRICT issue(problem)



    Yes. Unless you _like_ spending time finding bugs that a program
    could have found for you.


    > to get rid of
    > PROBLEM(2) ?



    but not for that reason.

    Rather for the reason that using strict will automatically find
    bugs in your programs for you, so you don't have to find them yourself.


    > If yes :
    > Is this the best link to solve PROBLEM(1) ?
    > http://www.perl.com/pub/a/2002/04/23/mod_perl.html



    No, since that is about mod_perl, and you are not using mod_perl (or are you?).


    > Or is there something even better?



    You should always prefer lexical (my) variables over package (our)
    variables, except when you can't.

    I see nothing in what you are trying to do that indicates that
    you can't, so you should be using lexical (my) variables.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad McClellan, Aug 11, 2007
    #12
  13. Ved

    Ved Guest

    On Aug 6, 7:35 pm, Ved <> wrote:
    > Hi all,
    > I have to replace commented line in a list of " .cpp" file .
    > Line looks like this:
    > //#define BBPRxChanRouteFileLoadInput 1
    >
    > i.e. The line format :
    > //#define (FileName)FileLoadInput 1
    > is to be replaced with
    > #define (FileName)FileLoadInput 1
    >
    > I have written a code using Tie::File which is mentioned below.
    > I want to do two things:
    >
    > 1) Remove trailing .cpp in $dir ?
    >
    > 2) What to do in this to get (/ //#define $dirFileLoadInput 1 /) in
    > the for loop ?
    >
    > For above example file name in my.rxfiles is:
    > BBPRxChanRoute.cpp
    >
    > Thanks in advance
    > Ved
    > #######################################################
    >
    > #!/usr/local/bin/perl
    > use strict;
    > use warnings;
    > use Tie::File;
    >
    > my $dir_list = 'my.rxfiles'; #contains list of .cpp files
    > open my $fh,'<',$dir_list or die "Cannot read $dir_list: $!\n";
    > while (my $dir = <$fh>) {
    > chop $dir;
    > my $file = "$dir";
    > chomp $file;
    > if (-e $dir) {
    > process_one_file($dir);
    > } else {
    > warn "File $dir does not exist; skipping\n";
    > }
    > }
    >
    > #Using core module Tie::File to process a file in this subroutine
    > sub process_one_file {
    > my $dir = shift;
    > print "Processing $dir\n";
    > tie my @array, 'Tie::File', $dir or die "tie error $dir: $!
    > \n" ;
    > for (@array) {
    > if (/\//#define $dirFileLoadInput 1/) #what to do here ??
    > {
    > $_ = #define $dirFileLoadinput 1 ;
    > last;
    > }
    > }
    > }


    ##################################################
    Hi All,
    Thanks for the replies.
    I am posting the code for the completeness of the thread.
    My task is being accomplished with this code, though I am still not
    sure if it is perfect code.

    #!/usr/local/bin/perl
    use strict;
    use warnings;
    use Tie::File;

    my $cpp_file_list = 'my.rxfiles';
    open my $fh,'<',$cpp_file_list or die "Cannot read $cpp_file_list:
    $!\n";
    while (my $cpp_file = <$fh>) {
    chomp $cpp_file;
    my $module_name = "$cpp_file";
    $module_name =~ s/\.cpp$//;#removing trainling .cpp
    print "this is name $module_name\n";
    print "this is CPP $cpp_file\n";
    if (-e $cpp_file) {
    process_one_file($cpp_file,$module_name);
    } else {
    warn "File $cpp_file does not exist; skipping\n";
    }
    }

    #Using core module Tie::File to process a file in this subroutine
    sub process_one_file {
    my($cpp_file,$module_name)=@_;
    $cpp_file = shift;
    print "Processing $cpp_file\n";
    tie my @array, 'Tie::File', $cpp_file or die "tie error
    $cpp_file: $!\n" ;

    for (@array) #Each line should come one by one
    {
    #if ( s{^//(#define ${module_name}FileLoadInput 1)}{$1} ) {
    if ( s{//(#define ${module_name}FileLoadInput 1)}{$1} ) {
    print "true $module_name \n"
    # print " $array \n"
    }
    else
    { print "false $module_name \n"}

    }

    }
     
    Ved, Aug 16, 2007
    #13
  14. Ved

    Ved Guest

    Hi All,
    Thanks for the replies.
    I am posting the code for the completeness of the thread.
    My task is being accomplished with this code, though I am still not
    sure if it is perfect code.

    #!/usr/local/bin/perl
    use strict;
    use warnings;
    use Tie::File;

    my $cpp_file_list = 'my.rxfiles';
    open my $fh,'<',$cpp_file_list or die "Cannot read $cpp_file_list:
    $!\n";
    while (my $cpp_file = <$fh>) {
    chomp $cpp_file;
    my $module_name = "$cpp_file";
    $module_name =~ s/\.cpp$//;#removing trainling .cpp
    print "this is name $module_name\n";
    print "this is CPP $cpp_file\n";
    if (-e $cpp_file) {
    process_one_file($cpp_file,$module_name);
    } else {
    warn "File $cpp_file does not exist; skipping\n";
    }
    }

    #Using core module Tie::File to process a file in this subroutine
    sub process_one_file {
    my($cpp_file,$module_name)=@_;
    $cpp_file = shift;
    print "Processing $cpp_file\n";
    tie my @array, 'Tie::File', $cpp_file or die "tie error
    $cpp_file: $!\n" ;

    for (@array) #Each line should come one by one
    {
    #if ( s{^//(#define ${module_name}FileLoadInput 1)}{$1} ) {
    if ( s{//(#define ${module_name}FileLoadInput 1)}{$1} ) {
    print "true $module_name \n"
    # print " $array \n"
    }
    else
    { print "false $module_name \n"}

    }

    }
     
    Ved, Aug 16, 2007
    #14
    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. Hugo
    Replies:
    10
    Views:
    1,392
    Matt Humphrey
    Oct 18, 2004
  2. kaushikshome
    Replies:
    4
    Views:
    822
    kaushikshome
    Sep 10, 2006
  3. scad
    Replies:
    23
    Views:
    1,218
    Alf P. Steinbach
    May 17, 2009
  4. Rob Meade

    Replacing - and not Replacing...

    Rob Meade, Apr 5, 2005, in forum: ASP General
    Replies:
    5
    Views:
    314
    Chris Hohmann
    Apr 11, 2005
  5. Marek Stepanek
    Replies:
    12
    Views:
    438
    Peter J. Holzer
    Sep 2, 2006
Loading...

Share This Page