CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
I'm having trouble passing data to a subroutine



May 29, 2000, 3:23 PM

Post #1 of 3 (644 views)
I'm having trouble passing data to a subroutine Can't Post

For some reason, my data isn't being recieved by a subroutine, but I don't see the problem. I'm hopeing that someone can point out my flaw.

Before I detail the code, I'll post the heart of it, in case there's something wrong with the call.

#I use the following to call a routine

#That routine is defined as follows:
sub email_notification
my (%post_info,$email,$anti_spam) = @_;
#.... the value in $email in the call
# isn't passed into it's namesake in the
# subroutine.

Here's the relevant code:

# The following code works fine.
# It's what I use to track down problems

sub diag
my ($message,$data) = @_;

if (open(FILE_DIAG,'>>diag.log'))
print FILE_DIAG '[',time,'] ',$message,' :: ',$data,"\r\n";
close FILE_DIAG;

# The following is the routine that
# isn't recieving the parameters

sub email_notification
my (%post_info,$email,$anti_spam) = @_;

my $sendmail_path = '/usr/sbin/sendmail';
my $to_address = $email;
my $from_login = 'something';
my $from_address = '';

&diag('e-mail notification',$email);

#.... the above shows me that it's not
# getting the data

# The following is where
# the routine is called

sub submit_post
my $last = '';
my %post_info;

my $email;
my $anti_spam = '';

#.... skipping some code....


# The diag confirms that $email has a value
# but the email_notification sub-routine
# doesn't recieve the data passed it to

So, does anyone see what I'm missing here? It's probably something obvious, but I can't see anything wrong.

Dr. Zed


May 29, 2000, 7:55 PM

Post #2 of 3 (644 views)
Re: I'm having trouble passing data to a subroutine [In reply to] Can't Post

Values sent to a subroutine are flattened into a list. You can "pull out" a hash or array from the arguments a subroutine gets:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

@a = (1,2,3);
@b = (4,5,6);

sub foo {
my (@c,@d) = @_;
print << "DEBUG";
c: @c
d: @d

@c has (1,2,3,4,5,6), and @d is empty.

You might need to pass by reference, or you can send the hash LAST:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


sub foo {
my ($first,$second,%hash) = @_;
# ...

Try that.


Jun 3, 2000, 5:38 PM

Post #3 of 3 (644 views)
Re: I'm having trouble passing data to a subroutine [In reply to] Can't Post

Thanks! That worked. I would have responded sooner, but I was having trouble posting responses.

You were indead correct that the values are just passed in a list. Putting the hash at the end worked. Anything other than a $variable should be put at the end.

As a note to others, if you want to pass more than one non-$ value (e.g. %'s and/or @'s), I believe the best way would be to use references (i.e. pointers). For example, I could have done the following:

my (%post_info,$email,$anti_spam) = @_;

my ($post_info_ref,$email,$anti_spam) = @_;
my (%post_info) = %$post_info_ref;

That would have the desired effect. The advantage of knowing this is that you can pass more than one reference without worrying about the data getting mixed.

Also, if you want the subroutine to be able to actually alter the data structure (rather than just being passed a copy), don't bother with the:

my (%post_info) = %$post_info_ref;

.... and instead, where you would normally use %post_info or $post_info{whatever} in the subroutine, you instead use %$post_info_ref or $$post_info_ref{whatever}

Essentially, you will be using the variable which was sent, rather than just the values.

If I'm wrong about any of this, let me know. I just wanted to post it in case anyone was having similar trouble.

Dr. Zed


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives