in reply to Grep file before sending through socket

I want to grep for errors in the file before the transmission starts ... If I find errors, I'll move the file to another directory so it can't be sent. ... is there another way to search a file all at once before I open it in the above code to start the send process?

Since you need to open the file before grepping it, try something like

open FILE, "$senddir/$file" or die "$senddir/$file: $!" my $file = do { local $/; <FILE> }; close(FILE); if ( $file =~ m/$errors/mo ) { # move the file } else { # open the socket and send $file, as before } }
where $errors is a regular expression that you set up in advance with the patterns that mean "error" to you. (Or, you can hard-code it). The /m on the regex allows you to use ^ and $ to match lines within $file.

Replies are listed 'Best First'.
Re: Re: Grep file before sending through socket
by wileykt (Acolyte) on Dec 11, 2001 at 20:46 UTC
    dws,
    Thanks, this works. I was wondering if you could help me interpret the following line.
    my $file = do { local $/; <FILE> };
    I know $/ is a line separator, but in this context, what is it doing? I'm fairly new to perl and some things are still cryptic to me. Thanks
      I was wondering if you could help me interpret the following line.   my $file = do { local $/; <FILE> }; This is called "slurp mode". Setting $/ to undef means that the entire file will be read. Localizing $/ within a short-lived scope both sets $/ to undef, and ensures that it will be restored on scope exit.

      This is equivalent in effect to writing   my $file = join('', <FILE>); but without the extra overhead to build up a temporary array and set of strings to populate it with.