in reply to Spliting an mbox file

my $out = ">./coffee.tmp";
Does your file name have a '>' character in it? You should probably be using the three argument form of open instead.
open OUT, $out || die "Can't open $out!\n"; open FILE, $file || die "Can't open file oldcoffee!\n"; close FILE, $file || die "Can't close file oldcoffee!\n"; close OUT, $out || die "Can't close $out!\n";
Using the high precedence || operator means that the dies are superfluous. You should use the low precedence or operator instead. close accepts one argument, the filehandle. You should also include the $! variable in the error messages so you know why the program failed.
$mails[$#mails + 1] = $_;
Is better written as:
push @mails, $_;
If you want to split the mbox file into separate files based on the year in the /^From/ line then you probably want something like this:
#!/usr/bin/perl use warnings; use strict; my $file = 'oldcoffee.tmp'; my $out = 'coffee.tmp'; open FILE, '<', $file or die "Can't open file $file! $!\n"; while ( <FILE> ) { if ( /^From / && /(\d{4})$/ ) { # Update: changed 'readonly' to 'append' open OUT, '>>', "$1$out" or die "Can't open $1$out! $!\n"; } fileno OUT and print OUT; } close FILE or die "Can't close file $file! $!\n"; __END__

Replies are listed 'Best First'.
Re^2: Spliting an mbox file
by parv (Parson) on Aug 16, 2006 at 01:53 UTC
    open OUT, $out || die "Can't open $out!\n";
    Using the high precedence || operator means that the dies are superfluous. You should use the low precedence or operator instead. .

    Or, use parentheses if one wants to use '||' ...

    open( OUT, $out ) || die "Can't open $out: $!\n";