in reply to Re: File regex question
in thread File regex question

Thanks!
I would appreciate if you could tell me how to implement the same in a program? I would start with open FH, "<file";

Replies are listed 'Best First'.
Re: File regex question
by Abigail-II (Bishop) on Sep 16, 2003 at 09:02 UTC
    I would start with open FH, "<file";

    No, you should not. You should always test the return value of open. Furthermore, I wouldn't use second class variables if I can avoid it. Finally, you may prefer 3-arg open over magic 2-arg open:

    open my $fh, "<", "file" or die "Failed to open 'file': $!";

    Abigail

Re: Re: Re: File regex question
by BrowserUk (Patriarch) on Sep 16, 2003 at 09:14 UTC

    Something like this (untested) code may be what your looking for.

    #! perl -w use strict; open IN, '<', 'file' or die $!; open OUT, '>', 'temp' or die $!; while( <IN> ) { s[ (?<=\\|/) (.*?) (?=\\|/) ][ (my$x=$1) =~ s{ }{_}g; $x ]gex; print OUT; } close IN; close OUT; rename 'file', 'file.bak'; rename 'temp', 'file';

    You could also look at using $INPLACE_EDIT ($^I) to have perl do the backup and rename for you.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.