Email Address Arguments

2004-10-12 Thread Errin Larsen
Hi Perl Mongers,

I'm trying to parse some command line options.

I'm expecting either no arguments, email addresses or email addresses
and file names/piped input.  This script will take the email addresses
and send the contents of a file to them, or the output of a piped
command.  So, I would expect something like this:

  # ls -la | mailer [EMAIL PROTECTED] [EMAIL PROTECTED]

or

  # ls -la | mailer

or

  # mailer [EMAIL PROTECTED] ls.out

so, I can check for no arguments with:
  if( @ARGV ) {
 #process args here
  }

and, I can match email addresses with this regex:
  /[EMAIL PROTECTED]/

I guess I'm asking for help on putting this stuff together.  When I
get done, I'd like to see a single string with the email addresses in
it, separated by commas.  I've been trying lots of stuff, but none of
it is working.  I can't seem to strip off the email addresses from the
front of @ARGV without getting hung up when I get to the end, or if
there is an (incorrectly) placed argument in the middle of addresses
that is NOT an address, like this:

  # ls -la | mailer [EMAIL PROTECTED] wrong.com [EMAIL PROTECTED]

I know I'm just missing something simple.

I did think about using one of the getopt()/getopts() modules, but I'd
rather not have to use a command-line flag/option to make this all
work.  Can you guys help me out?

--Errin

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Email Address Arguments

2004-10-12 Thread Wiggins d Anconia
> Hi Perl Mongers,
> 
> I'm trying to parse some command line options.
> 
> I'm expecting either no arguments, email addresses or email addresses
> and file names/piped input.  This script will take the email addresses
> and send the contents of a file to them, or the output of a piped
> command.  So, I would expect something like this:
> 
>   # ls -la | mailer [EMAIL PROTECTED] [EMAIL PROTECTED]
> 

So in this case you have two arguments in @ARGV and waiting text on
STDIN? Is it this last part that is confusing you. 

> or
> 
>   # ls -la | mailer
> 

Ok an easy one nothing in @ARGV.

> or
> 
>   # mailer [EMAIL PROTECTED] ls.out
> 

Ok so in this case ls.out as you point out won't have an @ character, so
we can assume that it is a file to read.  So the question becomes, does
this have to be the last argument, can there be multiple files, is it an
error if an argument doesn't look like an email AND isn't a file?  These
are design issues, but all can be worked around depending on the answers.

> so, I can check for no arguments with:
>   if( @ARGV ) {
>  #process args here
>   }
> 
> and, I can match email addresses with this regex:
>   /[EMAIL PROTECTED]/
> 

Well you can start to match email addresses.  It is better to match them
with Email::Valid once you have what you think is an address.

> I guess I'm asking for help on putting this stuff together.  When I
> get done, I'd like to see a single string with the email addresses in
> it, separated by commas.  I've been trying lots of stuff, but none of
> it is working.  I can't seem to strip off the email addresses from the
> front of @ARGV without getting hung up when I get to the end, or if
> there is an (incorrectly) placed argument in the middle of addresses
> that is NOT an address, like this:
> 

What have you tried?  Where did you fail? You know better than to post
without code :-).

>   # ls -la | mailer [EMAIL PROTECTED] wrong.com
[EMAIL PROTECTED]
> 
> I know I'm just missing something simple.
> 

Again, what have you tried?

So it goes something like, check for arguments, check that the arguments
look like email addresses, if not then maybe it is a file, check to see
if it exists (throw warning/error), if so then push it to a list and go
to the next one. If it is a file you could push it to a different list.
Then check STDIN for input, store it to an array for your message. Then
check your list of files, import them into the content list (or even
better maybe you want to attach them!!).  If something is missing throw
an error or set some defaults, if not send the message. Take it a chunk
at a time, run it hundreds of times with lots of print statements until
you have what you want.

> I did think about using one of the getopt()/getopts() modules, but I'd
> rather not have to use a command-line flag/option to make this all
> work.  Can you guys help me out?
> 

Consider the AppConfig module too, it has some more capability that
might come in handy this time.

> --Errin
> 

Don't give up, never admit defeat

http://danconia.org


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Email Address Arguments

2004-10-12 Thread Errin Larsen
On Tue, 12 Oct 2004 09:26:12 -0600, Wiggins d Anconia
<[EMAIL PROTECTED]> wrote:
> > Hi Perl Mongers,
> >
> > I'm trying to parse some command line options.
> >
  <>
> >
> 
> So in this case you have two arguments in @ARGV and waiting text on
> STDIN? Is it this last part that is confusing you.
> 
  
  yes ... I'll explain below

  <>
> >
> > and, I can match email addresses with this regex:
> >   /[EMAIL PROTECTED]/
> >
> 
> Well you can start to match email addresses.  It is better to match them
> with Email::Valid once you have what you think is an address.
> 

  Yes ... I'm just not to the point where I'm making this pretty yet,
need to parse the arguments first and the above (dirty) regex works
for this purpose.

> > I guess I'm asking for help on putting this stuff together.  When I
  <>
> 
> What have you tried?  Where did you fail? You know better than to post
> without code :-).

  I know, I know.  I just was having a brain-empty kinda morning.  I
couldn't kick-start the thinking!

  <>
> 
> So it goes something like, check for arguments, check that the arguments
> look like email addresses, if not then maybe it is a file, check to see
> if it exists (throw warning/error), if so then push it to a list and go
> to the next one. If it is a file you could push it to a different list.
> Then check STDIN for input, store it to an array for your message. Then
> check your list of files, import them into the content list (or even
> better maybe you want to attach them!!).  If something is missing throw
> an error or set some defaults, if not send the message. Take it a chunk
> at a time, run it hundreds of times with lots of print statements until
> you have what you want.
> 
  
  The above is exactly what I needed to get me thinking!  Thanks!!

  <>
> 
> Consider the AppConfig module too, it has some more capability that
> might come in handy this time.

  I'll look into this, thanks.

I've cobbled some code together to test stuff out with:

#!/usr/bin/perl

use warnings;
use strict;

my @addresses;
my @message;

if( @ARGV ) {
print "There are arguments\n";
while( $ARGV[0] =~ /[EMAIL PROTECTED]/ ) {
push @addresses, $ARGV[0].', ';
shift;
}
print "@addresses\n";
} else {
print "There are no arguments\n";
}

while( <> ) {
if( /^.$/ ) {
last;
} else {
push @message, $_;
}
}

print "\n\nThe following message will be sent:\n";
print "@message\n";


I keep getting a warning when the file name's not on the command line.
 In other words, If I use standard input for manual input, or if I
pipe the input to the mailer script.

  # mailer [EMAIL PROTECTED] [EMAIL PROTECTED] test.txt

works fine, but:

  # ls -l | mailer [EMAIL PROTECTED] [EMAIL PROTECTED]
or
  # mailer [EMAIL PROTECTED] [EMAIL PROTECTED]

gives the following output:

# mailer [EMAIL PROTECTED] [EMAIL PROTECTED]
There are arguments
Use of uninitialized value in pattern match (m//) at ./mailtest4 line 15.
[EMAIL PROTECTED],  [EMAIL PROTECTED], 
This is a test message.
.


The following message will be sent:
This is a test message.


I know that the "Use of unintialized value ..." message has to do with
the fact that input is sitting on STDIN (or, will be), but I can't
figure out how to deal with it.

Thanks for any help

--Errin

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Email Address Arguments

2004-10-13 Thread Errin Larsen
I figured it out.  I thought I'd post what I found.

> I've cobbled some code together to test stuff out with:
> 
> #!/usr/bin/perl
> 
> use warnings;
> use strict;
> 
> my @addresses;
> my @message;
> 
> if( @ARGV ) {
> print "There are arguments\n";
> while( $ARGV[0] =~ /[EMAIL PROTECTED]/ ) {

  The above line of code was the culprit.  I added a check to make
sure @ARGV wasn't empty and everything worked out.  This line looks
like this now:
  while( @ARGV && $ARGV[0] =~ /[EMAIL PROTECTED]/ ) {

> push @addresses, $ARGV[0].', ';
> shift;
> }
> print "@addresses\n";
> } else {
> print "There are no arguments\n";
> }
> 
> while( <> ) {
> if( /^.$/ ) {
> last;
> } else {
> push @message, $_;
> }
> }
> 
> print "\n\nThe following message will be sent:\n";
> print "@message\n";
> 

--Errin

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]