help: foreach loop with filenames & wildcards 
Author Message
 help: foreach loop with filenames & wildcards

I'm writing a simple routine to scan a directory for filenames all ending in a
certain suffix (".pl" in this example) and email the list to someone.
I've been able to do this by throwing the list into an array and then
mailing the array, but I think I can cut my code in half by using
"foreach", but it doesn't seem to be sending anything now. Can
anybody help? Here's the code:

#!/usr/bin/perl

# this program mails a list of all files ending in .pl to $address

$address = &inputword("Who to mail this to?");

# get directory listing -> only filenames ending in ".pl"

open (Mail, "|Mail $address");
print Mail "Here is a list of all perl programs in the test directory.\n";

open (FILE, "*.pl");
foreach ($filename) {
    open (FILE, $filename);
    print Mail "$filename\n";
 }

close (Mail);

################## SUBROUTINES ########################

sub inputword
{

    print "$prompt ";
    $name=<STDIN>;
    chop($name);
    $name;

Quote:
}



Mon, 16 Nov 1998 03:00:00 GMT  
 help: foreach loop with filenames & wildcards

Quote:

>I'm writing a simple routine to scan a directory for filenames all ending in a
>certain suffix (".pl" in this example) and email the list to someone.
>I've been able to do this by throwing the list into an array and then
>mailing the array, but I think I can cut my code in half by using
>"foreach", but it doesn't seem to be sending anything now. Can
>anybody help? Here's the code:

>#!/usr/bin/perl

># this program mails a list of all files ending in .pl to $address

>$address = &inputword("Who to mail this to?");

># get directory listing -> only filenames ending in ".pl"

>open (Mail, "|Mail $address");
>print Mail "Here is a list of all perl programs in the test directory.\n";

!----------------------------------
Quote:
>open (FILE, "*.pl");
>foreach ($filename) {
>    open (FILE, $filename);
>    print Mail "$filename\n";
> }

!----------------------------------

# Instead of that, How about this (untested code)?



     open (FILE, $_);
     print Mail "$_.\n";
     close (FILE);
  }

# There are dozens of other ways including the SMNTP module from CPAN ...

Quote:
>close (Mail);

>################## SUBROUTINES ########################

>sub inputword
>{

>    print "$prompt ";
>    $name=<STDIN>;
>    chop($name);
>    $name;
>}

Good luck.

Peter Prymmer



Wed, 18 Nov 1998 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. need help with FOREACH and filename wildcards

2. foreach loop & current element index

3. Splice in foreach loop, then quit loop: legal?

4. foreach loop works, for loop not

5. Save output from foreach loop outside loop?

6. Help: a beginner cannot make for/foreach loop running

7. Need help getting out of a foreach loop

8. Q: filename wildcard

9. Match list of filenames with non-regex wildcards?

10. PERLFUNC: glob - expand filenames using wildcards

11. PERLFUNC: glob - expand filenames using wildcards

12. Checking if file exists (filename contains a wildcard)

 

 
Powered by phpBB® Forum Software