perl.beginners
[Top] [All Lists]

Re: Info from flat file

Subject: Re: Info from flat file
From: Chas. Owens
Date: Tue, 25 Mar 2008 03:43:22 -0400
Newsgroups: perl.beginners

On Tue, Mar 25, 2008 at 3:32 AM, Manoj <manojkumarg@xxxxxxxxxx> wrote:
> Chas,
>
>  I have a question on this. Will the pattern $/ used considered for other
>  opened files also? I was trying to match the IP with ip-host file and
>  considering the whole ip-host text file as single line even though there is
>  newline.
>
>  Can you shower some light in this?
snip

Yes, $/ is global for all files.  If you only need the input separator
to be special for a section of your code you can always say

{
    local $/ = "+=========================+\n";
    while (<>) {
        #do stuff
    }
}

You can even next the locals:

{
    local $/ = "+=========================+\n";
    while (my $record = <>) { # this is record by record
        my ($file) = $record =~/file: (\S+)/;
        local $/ = "\n"; # set input separator to line by line for this scope
        open my $fh, "<", "$file"
            or die "could not open $file: $!";
        while (my $line = <$fh>) { #this is line by line
            #do stuff
        }
    } # $/ goes back to "+=========================+\n"
} # $/ goes back to its default value ("\n")

You can read more here:

http://perldoc.perl.org/functions/local.html
http://perldoc.perl.org/perlsub.html#Temporary-Values-via-local()
http://perldoc.perl.org/perlvar.html#$INPUT_RECORD_SEPARATOR

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

<Prev in Thread] Current Thread [Next in Thread>