Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Single variable Multiple Values

by PilotinControl (Monk)
on Jun 29, 2013 at 17:50 UTC ( #1041491=perlquestion: print w/replies, xml ) Need Help??
PilotinControl has asked for the wisdom of the Perl Monks concerning the following question:

Good Afternoon my fellow monks here is my question: I have a variable that is passed from one subroutine to another. In the second subroutine this variable could represent one or more values. What I am trying to accomplish is sending an email based on an Abbreviation for example: $emailverify in the previous sub could equal Bob, Tim, George etc. In a database Bob =, Tim = etc. I am having trouble making $emailverify = Bob in turn making Bob = $to and sending the file to Bob or Tim. Thanks in advance!

sub email { my $emailverify = shift; print ("(1) MICROSOFT ACCESS"); my $input = <STDIN>; $input = <STDIN> until defined $input; chop ($input); if ( $input == 1 ) { # SENDS MS ACCESS FILE my $from = '; my $your_file = 'file.mdb'; my $my_file = 'file.mdb'; my $mime_type = 'multipart/mixed'; my $mail_server = ''; my $to = $emailverify; my $msgone = "Dear $emailverify Your Files are Attached"; # CREATE MESSAGE my $msg = MIME::Lite->new( From => $from, To => $to, Type => $mime_type, Subject => 'Files Attached', ); # CREATE ATTACHMENT $msg->attach( Type => 'TEXT', Data => "$msgone" ); $msg->attach ( Type => 'application/msaccess', Path => $my_file, Filename => $your_file, Disposition => 'attachment' ) or die "Error adding $your_file: $!\n"; MIME::Lite->send('smtp', $mail_server, Timeout=>60); $msg->send or die "Couldn't Send $!";

Replies are listed 'Best First'.
Re: Single variable Multiple Values
by Perlbotics (Chancellor) on Jun 29, 2013 at 18:11 UTC
    If I understand your problem correctly, you could apply this quick patch:
    ... my $mime_type = 'multipart/mixed'; my $mail_server = ''; #--old: my $to = $emailverify; my $to = alias_to_emailaddress( $emailverify ); ...
    And then add something to resolve the name/alias to an email-address:
    sub alias_to_emailaddress { my ($alias) = @_; #-- e.g. "Bob"; #-- query your DB or start with a hash-lookup for testing... my %alias2email = ( 'BOB' => '', 'TIM' => '', ); #-- maybe normalise your alias first? my $email = $alias2email{ uc $alias }; #---- now add some error handling... #-- perlhaps you can use a default? # $email //= ''; #-- throw an exception (catch with 'eval' later) # die "No email-address found for '$alias'!" unless $email; return $email; }

      That worked! Thanks!

Re: Single variable Multiple Values
by Happy-the-monk (Abbot) on Jun 29, 2013 at 18:10 UTC

    I am having trouble making $emailverify = Bob in turn making Bob = $to and sending the file to Bob or Tim.

    Where to start?

    Perl has got all kinds of variables. You are using scalars in your code most of the time. That's the type of variable that generally fits a single value in it. With a little help from perlintro you will find arrays for variable lists and hashes for variable lists representing relationships of keys values. And of course references and objects that may virtually be or do anything or everything.

    However, you use a scalar, a single thing variable for an email address, and I think all you need now is that.
    You can concatenate a string in a scalar like this:

    $email = $email:1 . "," . $email_2;

    A number of email programmes treat such a string the way you want them, as a list of email addresses separated by the comma (",").

    Your mileage may vary ;-)

    Cheers, Sören

    (hooked on the Perl Programming language)

Re: Single variable Multiple Values
by poj (Prior) on Jun 29, 2013 at 19:17 UTC
    "..this variable could represent one or more values"

    Are you sending the same email to more than one person ?.

    For example; you want $to to be ',,' but the variable you are being passed is like 'me,you,'

Re: Single variable Multiple Values
by Anonymous Monk on Jun 30, 2013 at 05:16 UTC
    Wishful thinking here :)  $input = <STDIN> until defined $input;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1041491]
Approved by Happy-the-monk
[marto]: and you still can't get a decent cup of tea in that town
[davies]: The turnout, IIRC, was about 12%.
[&lt; &amp; &gt;]: 12% seriously? =)
NodeReaper thinks he was bad enough with one account \:

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2017-12-15 19:15 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (442 votes). Check out past polls.