in reply to Pass Arrays to a subroutine

As mentioned in perlsub, parameters passed into a subroutine are passed as a flat list. From the subroutine's perspective, it's impossible to determine where @VRF_names ends, and @spines_ip begins. It's all just a flat list of values.

You want to pass in a reference to an array:

sub BuildLeaf { my ( $hostname, $lo_add, $ospf_process, $ospf_area, $bgp_as, $bgp_pass, $VRF_names_aref, $spines_ip_aref ) = @_; # Do your stuff here. }

And then in the sub call, change @{$vrf_info[0]} and @{$spine_info[1]} to $vrf_info[0] and $spine_info[1]. Within the subroutine, you will need to dereference the array-refs appropriately. See perlref and perlreftut for details. We can't really guess how you might be using the passed-in arrays without seeing the code, so it would be impractical for us to demonstrate the appropriate array-ref dereferencing for your particular use case. But the documentation is good, although possibly a bit opaque on first read. The online book Modern Perl would probably offer a gentler introduction to wielding references.


Comment on Re: Pass Arrays to a subroutine
Select or Download Code