beanscake has asked for the wisdom of the Perl Monks concerning the following question:

Hello forks i want to write script that will scan given folders for email and write them to a file, i know am doing it wrong i would appreciate enlightenment thanks below is my poor code

use strict; use warnings; use 5.010; use POSIX qw(strftime); use List::MoreUtils qw(uniq); use Data::Dumper qw(Dumper); my $range = 50; my $minimum = 100; my $random_number = int(rand($range)) + $minimum; my $date = strftime "Y%m%d", localtime; my $filenadme =$exname.$random_number.$date.'.txt'; @files = </Users/beanscake/Download/*>; #scan folders for email foreach $file (@files) { open (MYFILE, $file); open(my $fh, '>', $filenadme); while (<MYFILE>) { chomp; my @emails = split(' '); my @filtered = uniq(@emails); foreach my $emails (@filtered) { if($emails =~ /^\w+\@([\da-zA-Z\-]{1,}\.){1,}[\da-zA-Z-]{2 +,6}$/) { print $fh "$emails\n"; print "$emails\n"; } } } close $fh; close MYFILE; print "done\n";
The beginning of knowledge is the discovery of something we do not understand.
    Frank Herbert (1920 - 1986)

Replies are listed 'Best First'.
Re: Need help! on this piece of code.
by NetWallah (Canon) on Mar 26, 2015 at 04:19 UTC
    Ok - here are a few obvious things from first glance:
    • Why are you using random numbers for file names ? Start at 1, then increment by 1.
    • Where does $exname get initialized ?
    • You probably meant strftime "%Y%m%d", localtime (Missing % in front of Y)
    • You are overwriting the same file in each "$file loop. Move the random number, and file name concatanation INSIDE the loop
    • Close the file you open inside the loop, inside the loop.
    • Indent your code better for readability
    • Post sample output, and/or expected output.

            "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

Re: Need help! on this piece of code.
by Dumu (Monk) on Mar 26, 2015 at 10:26 UTC
    Tiny additional suggestion: change $filenadme to $filename.
Re: Need help! on this piece of code.
by Dumu (Monk) on May 06, 2015 at 09:33 UTC