For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2005 > GetOpt::Long









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author GetOpt::Long
Wim W Olivier

2005-04-20, 3:56 pm

Hi all,

Is it possible to use GetOpt::Long (or something similar) in a subroutine
using @_ instead of in the standard way (using @ARGV)?
I want to have the following scenario, but use GetOpt in a subrouting within
the main script and pass options (and values) to the subroutine.
The values ($recipient, $subject, $body) must be configurable and they must
be able to take multiple values, i.e. multiple recipients for a message.


# Notify recipients via SMTP (email)
sub notify_email {
$ENV{"NTsendmail"} = $smtp;
$sender = "sender\@domain.co.za";
$recipient = "Wim.Olivier\@domain.co.za";
$subject = "ALERT";
$body = join " ", @_;
$mail = new NTsendmail;
$mail->send($sender, $recipient, $subject, $body);
}


All help appreciated! Thanks in advance.


Kind Regards,

Wim Olivier

________________________________________
________________________________________
________________________________________
__________

Standard Bank Disclaimer and Confidentiality Note

This e-mail, its attachments and any rights attaching hereto are, unless the context clearly indicates otherwise, the property of Standard Bank Group Limited and/or its subsidiaries ("the Group"). It is confidential, private and intended for the addressee
only.

Should you not be the addressee and receive this e-mail by mistake, kindly notify the sender, and delete this e-mail, immediately and do not disclose or use same in any manner whatsoever.

Views and opinions expressed in this e-mail are those of the sender unless clearly stated as those of the Group. The Group accepts no liability whatsoever for any loss or damages whatsoever and howsoever incurred, or suffered, resulting, or arising, from
the use of this email or its attachments.

The Group does not warrant the integrity of this e-mail nor that it is free of errors, viruses, interception or interference.

Licensed divisions of the Standard Bank Group are authorised financial services providers in terms of the Financial Advisory and Intermediary Services Act, No 37 of 2002 (FAIS).

For information about the Standard Bank Group Limited visit our website http://www.standardbank.co.za
________________________________________
________________________________________
________________________________________
___________
Tim Johnson

2005-04-20, 3:56 pm



How about something like this? It doesn't make it like GetOpt::Long,
but it does handle what you want. Or you could just require that people
pass an array to your subroutine and save yourself a little work. If
there is only one recipient, then it's a one-element array.

########################################
#####

my $recipients =3D 'tjohnson@sandisk.com';
notify_email('tjohnson@sandisk.com',$recipients,"test","this is my
body");
#my @recipients =3D qw(tjohnson@sandisk.com lkancherla@sandisk.com
tojo2000@sbcglobal.net);
#notify_email('tjohnson@sandisk.com',\@recipients,"test","this is the
body");

sub notify_email{
my ($sender,$rcpt,$subj,$body) =3D @_;
my $recipient;
if(ref($rcpt)){
if(ref($rcpt) eq 'ARRAY'){
$recipient =3D join(',',@{$rcpt});
}elsif(ref($rcpt) eq 'SCALAR'){
$recipient =3D ${$rcpt};
}else{
die "Invalid recipient parameter!\n";
}
}else{
$recipient =3D $rcpt;
}
print $recipient."\n";
}

########################################
#####

-----Original Message-----
From: Olivier, Wim W [mailto:Wim.Olivier@standardbank.co.za]=20
Sent: Wednesday, April 20, 2005 12:53 AM
To: Perl Beginners (E-mail)
Subject: GetOpt::Long

Hi all,

Is it possible to use GetOpt::Long (or something similar) in a
subroutine
using @_ instead of in the standard way (using @ARGV)?
I want to have the following scenario, but use GetOpt in a subrouting
within
the main script and pass options (and values) to the subroutine.
The values ($recipient, $subject, $body) must be configurable and they
must
be able to take multiple values, i.e. multiple recipients for a message.


# Notify recipients via SMTP (email)
sub notify_email {
$ENV{"NTsendmail"} =3D $smtp;
$sender =3D "sender\@domain.co.za";
$recipient =3D "Wim.Olivier\@domain.co.za";
$subject =3D "ALERT";
$body =3D join " ", @_;
$mail =3D new NTsendmail;
$mail->send($sender, $recipient, $subject, $body);
}


Bob Showalter

2005-04-20, 3:56 pm

Olivier, Wim W wrote:
> Hi all,
>
> Is it possible to use GetOpt::Long (or something similar) in a
> subroutine
> using @_ instead of in the standard way (using @ARGV)?



This should work:

sub foo {

local @ARGV = @_;
GetOptions(...blah...);

...
}
Offer Kaye

2005-04-20, 3:56 pm

On 4/20/05, Olivier, Wim W wrote:
> Hi all,
>=20
> Is it possible to use GetOpt::Long (or something similar) in a subroutine


Getargs::Long -=20
http://search.cpan.org/dist/Getargs-Long/

HTH,
--=20
Offer Kaye
Wim W Olivier

2005-04-21, 8:55 am

Hi Bob,

(Offer Kaye, thanks for the reply as well!)

I used the method below (local @ARGV = @_;) to get the values of @_ into
@ARGV for the use of Getopt::Long. It appears to be working fine like that.
I now have another problem with, it appears, syntax. The IF statement is
part of a block in the switch statement, but there is nothing wrong with the
switch statement itself, as there are many identical if statements there. If
I comment out this if statement, I don't get the error anymore. An clues as
to the correct syntax???

I run the subroutine as follows:

if ($conf{'pnl_check_for_analytics_email'} =~ 'ON')
{ notify_email -r Wim.Olivier@standardbank.co.za -r
testuser@standardbank.co.za -s "Subject line" -b "Message body" };


But I get the following error when it executes:

Bad switch statement (problem in the code block?)



Please see the code below:

========================================
====================================
==============
# Notify recipients via SMTP (email)
# Usage: notify_email -r user1@a.com -r user2@b.com -s "Subject line" -b
"Message body"
sub notify_email {
local @ARGV = @_; # Get the sub's params into the master param array for
GetOpt::Long
$ENV{"NTsendmail"} = $conf{'smtp'};
$sender = $conf{'smtpsender'};

my @recipients;
my @subject;
my $body;
GetOptions ("r=s" => \@recipients, # -r user1@a.com -r user2@b.com
-r user3@c.com
"s=s" => $subject, # -s "This is the subject
line..."
"b=s" => $body); # -b "This is the message
body..."
#@recipients = split(/,/,join(',',@recipients));
#$subject = join(" ", @subject);
#$body = join(" ", @body);

foreach $recipient (@recipients) {
print "$sender, $recipient, $subject, $body\n";
$mail = new NTsendmail;
$mail->send($sender, $recipient, $subject, $body);
}

}
========================================
====================================
==============

-----Original Message-----
From: Bob Showalter [mailto:Bob_Showalter@taylorwhite.com]
Sent: 20 April 2005 02:31 PM
To: Olivier, Wim W; Perl Beginners (E-mail)
Subject: RE: GetOpt::Long


Olivier, Wim W wrote:
> Hi all,
>
> Is it possible to use GetOpt::Long (or something similar) in a
> subroutine
> using @_ instead of in the standard way (using @ARGV)?



This should work:

sub foo {

local @ARGV = @_;
GetOptions(...blah...);

...
}

________________________________________
________________________________________
________________________________________
__________

Standard Bank Disclaimer and Confidentiality Note

This e-mail, its attachments and any rights attaching hereto are, unless the context clearly indicates otherwise, the property of Standard Bank Group Limited and/or its subsidiaries ("the Group"). It is confidential, private and intended for the addressee
only.

Should you not be the addressee and receive this e-mail by mistake, kindly notify the sender, and delete this e-mail, immediately and do not disclose or use same in any manner whatsoever.

Views and opinions expressed in this e-mail are those of the sender unless clearly stated as those of the Group. The Group accepts no liability whatsoever for any loss or damages whatsoever and howsoever incurred, or suffered, resulting, or arising, from
the use of this email or its attachments.

The Group does not warrant the integrity of this e-mail nor that it is free of errors, viruses, interception or interference.

Licensed divisions of the Standard Bank Group are authorised financial services providers in terms of the Financial Advisory and Intermediary Services Act, No 37 of 2002 (FAIS).

For information about the Standard Bank Group Limited visit our website http://www.standardbank.co.za
________________________________________
________________________________________
________________________________________
___________
Bob Showalter

2005-04-21, 3:56 pm

Olivier, Wim W wrote:
> Hi Bob,


Hi. Don't top-post please.

> If
> I comment out this if statement, I don't get the error anymore. An
> clues as
> to the correct syntax???
>
> I run the subroutine as follows:
>
> if ($conf{'pnl_check_for_analytics_email'} =~ 'ON')
> { notify_email -r Wim.Olivier@standardbank.co.za -r
> testuser@standardbank.co.za -s "Subject line" -b "Message body" };


You need to pass your arguments as a proper list, with commas and quotes:

notify_email('-r', 'Wim.Olivier@standardbank.co.za',
'-r', 'Wim.Olivier@standardbank.co.za',
'-s', 'Subject Line',
'-b', 'Message Body');

You can omit the parens if notify_email has been declared above this code,
but I dont' recommend it.
Wim W Olivier

2005-04-21, 3:56 pm

Hi Bob,

You gave me the following answer earlier:

sub foo {

local @ARGV = @_;
GetOptions(...blah...);

...
}

If I run it as a seperate script, commenting out the (local @ARGV = @_;)
line, then it works. But it does not work when called as a subroutine from
within the main script. I have also tried (my @ARGV = @_;).
Any idea why?

Here is my code now:
========================================
=========
# Notify recipients via SMTP (email)
# Usage: notify_email -r user1@a.com -r user2@b.com -s "Subject line" -b
"Message body"
sub notify_email {
local @ARGV = @_; # Get the sub's params into the master param array for
GetOpt::Long
$ENV{"NTsendmail"} = $conf{'smtp'};
$sender = $conf{'smtpsender'};

GetOptions ("r=s" => \@recipients, # -r user1@a.com -r user2@b.com
-r user3@c.com
"s=s" => \@subject, # -s "This is the subject
line..."
"b=s" => \@body); # -b "This is the message
body..."

$subject = join(" ", @subject);
$body = join(" ", @body);

foreach $recipient (@recipients) {
print "$sender, $recipient, $subject, $body\n";
$mail = new NTsendmail;
$mail->send($sender, $recipient, $subject, $body);
}
}
========================================
=========

________________________________________
________________________________________
________________________________________
__________

Standard Bank Disclaimer and Confidentiality Note

This e-mail, its attachments and any rights attaching hereto are, unless the context clearly indicates otherwise, the property of Standard Bank Group Limited and/or its subsidiaries ("the Group"). It is confidential, private and intended for the addressee
only.

Should you not be the addressee and receive this e-mail by mistake, kindly notify the sender, and delete this e-mail, immediately and do not disclose or use same in any manner whatsoever.

Views and opinions expressed in this e-mail are those of the sender unless clearly stated as those of the Group. The Group accepts no liability whatsoever for any loss or damages whatsoever and howsoever incurred, or suffered, resulting, or arising, from
the use of this email or its attachments.

The Group does not warrant the integrity of this e-mail nor that it is free of errors, viruses, interception or interference.

Licensed divisions of the Standard Bank Group are authorised financial services providers in terms of the Financial Advisory and Intermediary Services Act, No 37 of 2002 (FAIS).

For information about the Standard Bank Group Limited visit our website http://www.standardbank.co.za
________________________________________
________________________________________
________________________________________
___________
John Doe

2005-04-22, 3:57 pm

Hi Wim

> Hi Bob,
>
> You gave me the following answer earlier:
>
> sub foo {
>
> local @ARGV = @_;
> GetOptions(...blah...);
>
> ...
> }
>
> If I run it as a seperate script, commenting out the (local @ARGV = @_;)
> line, then it works. But it does not work when called as a subroutine from
> within the main script.


Can you be more specific about "does not work" and "called ... from within the
main script"?

Your code below is not a functional example, so I made some changes
(especially inserting "use strict" and "use warnings" which alway gives good
hints :-),
and it works - but maybe in the sense you mentioned that it works for you too:

=== begin script (test3.pl) ===
use strict; use warnings;
use Getopt::Long;

sub notify_email {
local @ARGV = @_;

my (@recipients, @subject, @body);
my %conf=(smtp=>'whatever', smtpsender=>'something');

$ENV{"NTsendmail"} = $conf{'smtp'};
my $sender = $conf{'smtpsender'};

GetOptions ("r=s" => \@recipients,
"s=s" => \@subject,
"b=s" => \@body);
my $subject = join(" ", @subject);
my $body = join(" ", @body);

foreach my $recipient (@recipients) {
print "$sender, $recipient, $subject, $body\n";
# $mail = new NTsendmail;
# $mail->send($sender, $recipient, $subject, $body);
}
}

notify_email (@ARGV);
=== end script ===

This prints:

something, rrrr, sss, bbbb


> I have also tried (my @ARGV = @_;).
> Any idea why?
>
> Here is my code now:
> ========================================
=========
> # Notify recipients via SMTP (email)
> # Usage: notify_email -r user1@a.com -r user2@b.com -s "Subject line" -b
> "Message body"
> sub notify_email {
> local @ARGV = @_; # Get the sub's params into the master param array
> for GetOpt::Long
> $ENV{"NTsendmail"} = $conf{'smtp'};
> $sender = $conf{'smtpsender'};
>
> GetOptions ("r=s" => \@recipients, # -r user1@a.com -r user2@b.com
> -r user3@c.com
> "s=s" => \@subject, # -s "This is the subject
> line..."
> "b=s" => \@body); # -b "This is the message
> body..."
>
> $subject = join(" ", @subject);
> $body = join(" ", @body);
>
> foreach $recipient (@recipients) {
> print "$sender, $recipient, $subject, $body\n";
> $mail = new NTsendmail;
> $mail->send($sender, $recipient, $subject, $body);
> }
> }
> ========================================
=========
>
> ________________________________________
___________________________________
> ________________________________________
_______________
>
> Standard Bank Disclaimer and Confidentiality Note

[...]


hth, joe
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com