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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing arrays to sub 1

Status
Not open for further replies.

devilpanther

Programmer
Oct 8, 2004
57
IL
I read that I can't pass more than one array to a sub.
Is there a way to bypass this, some magic trick that the books don't list, maybe. Because this is very limiting.


I know that the answer will be "no", but I thought i should ask anyway, just in case, you never know.



thanks in advance.
 
Before perl 5.6 (I think) you couldn't easily pass multiple lists to a sub, but like chazoid showed, it is not only possible using references, its very easy and works like "magic". :)
 
Or, if you're into prototypes...
Code:
sub foo(\@\@) {
    
    $refArr1 = shift;
    $refArr2 = shift;

    # dereferencing:
    @arr1 = @{$refArr1};
    @arr2 = @{$refArr2};

}

foo(@arr1, @arr2);
...but if you're not, read up on perlsub and get familliar with the caveats before plunging too deep.

________________________________________
Andrew

I work for a gift card company!
 
References are nice little things. Not only can you pass a lot of vars with them, it doesnt matter what type they are.
You could create a 4 dimensional hash and pass it with several arrays into a sub.
 
I should say that with the example in my previous post, the dereferenced arrays should have been declared with 'my' to keep them local to the subroutine. This will keep you from altering the original array within the sub.
If you do want to work with the original array, you just sort of dereference on the fly -

push (@{$refArr1},"new");


ircf, I was just playing around with prototypes, and the docs aren't too clear on it, but is the correct usage to declare the sub before using it like this?

sub foo(\@\@);

foo(@a,@b);

sub foo (\@\@) {
etc..
}
 
Well, it'll throw an error if you try to call a prototyped sub before the prototype for that sub is loaded. You can either declare the entire sub above it's first use, or just declare the prototype before use. It's pretty much the same structure if you were programming in C/C++.

And to be honest, I've never used prototypes. I've read a lot of documentation and managed to retain a great deal of general perl knowledge (largely due to offering tips on sites like this), but more often than not, the simpler solutions work just as well. The important snippet from the doc:
Some folks would prefer full alphanumeric prototypes. Alphanumerics have been intentionally left out of prototypes for the express purpose of someday in the future adding named, formal parameters. The current mechanism's main goal is to let module writers provide better diagnostics for module users. Larry feels the notation quite understandable to Perl programmers, and that it will not intrude greatly upon the meat of the module, nor make it harder to read. The line noise is visually encapsulated into a small pill that's easy to swallow.

It's probably best to prototype new functions, not retrofit prototyping into older ones. That's because you must be especially careful about silent impositions of differing list versus scalar contexts. For example, if you decide that a function should take just one parameter, like this:
Code:
    sub func ($) {
        my $n = shift;
        print "you gave me $n\n";
    }
and someone has been calling it with an array or expression returning a list:
Code:
    func(@foo);
    func( split /:/ );
Then you've just supplied an automatic scalar() in front of their argument, which can be more than a bit surprising. The old @foo which used to hold one thing doesn't get passed in. Instead, the func() now gets passed in 1, that is, the number of elements in @foo. And the split() gets called in a scalar context and starts scribbling on your @_ parameter list.

This is all very powerful, of course, and should be used only in moderation to make the world a better place.

________________________________________
Andrew

I work for a gift card company!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top