Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array ref 1

Status
Not open for further replies.

rotis23

Programmer
Aug 29, 2002
121
GB
Hi All,

Got a problem using a reference to a main array within a package.

Why does this not work:

Code:
my $smtp = Net::SMTP->new(\@main::SMTPServerList, Timeout => $main::SMTPServerTimeout);

and this work:

Code:
my @templist = @main::SMTPServerList;
my $smtp = Net::SMTP->new(\@templist, Timeout => $main::SMTPServerTimeout);

Cheers, rotis23
 
What, exactly, do you mean by `not work' - what errors occur?

What about this?
Code:
my $smtp = Net::SMTP->new( [ @main::SMTPServerList ], Timeout => $main::SMTPServerTimeout);
 
In your example that works, you first create the array then create a reference to the array, that is why it works.

 
How is @SMTPServerList declared or first used?

this works:
Code:
@list = (1,2,3);
package test;
sub deref {
        my $ref = $_[0];
        print join( ', ', @{$_[0]} );
}
deref \@main::list;
but make @list a lexical and it fails:
Code:
my @list = (1,2,3);
package test;
sub deref {
        my $ref = $_[0];
        print join( ', ', @{$_[0]} );
}
deref \@main::list;

although I couldn't tell you why. Anyone?

fish

"As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilkes
 
sorry - "doesn't work" means that $smtp is undefined i.e. there was an error within the SMTP module.

ishnid - square brackets do work. Why?

fishiface - @SMTPServerList is declared in main as:

$SMTPServerList[0] = '127.0.0.1';
$SMTPServerList[1] = '127.0.0.2';

Thanks for your help people!

rotis23
 
fish - lexicals are different to package variables, so when you declare a lexical called @list, it doesn't create a corresponding @main::list, which would be a different variable. To illustrate:
Code:
#!/usr/bin/perl -w
use strict;


@main::test = qw/ar afds/;
my @test = qw/blah blah/;

print @test, "\n";
print @main::test, "\n";

rotis23 - simple answer is that I don't know! It seems that the module was unable to see @main::SMTPServerList, though I can't think of a reason (unless you're changing the array after passing it in but before the module uses it, though that's pure speculation). The square brackets set up an anonymous array reference containing the same contents as @SMTPServerList and uses that instead of the original array itself. It's similar to the original solution you posted (with the temporary array), except that it requires less typing. Glad it worked.
 
ishnid - thanks for answer. Can you point me to where I could read more on this and, specifically, when I should use lexicals instead of package variables?

thanks

"As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilkes
 
There's an article here that I seem to remember being useful. Let me know what you think.

My general rule of thumb on scoping is that you should always use lexicals unless you have a specific reason not to.
 
What an excellent FAQ. I'd strongly recommend it to anyone feeling a little fuzzy about $main::, my(), local() or our(). Thanks ishnid - have a star.

f

"As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilkes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top