Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Passing array to a subroutine

by Anonymous Monk
on Sep 13, 2017 at 13:42 UTC ( #1199302=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a subroutine defined

sub sadk_flap_mx_ifd_laser { my $r = shift; my $sub = sub_name; return $r->error_handler('S', "$sub: MX-VC does not support laser +off/on") if (exists $r->{MX_VC_CHASSIS} && $r->{MX_VC_CHASSIS}); my $args = get_args(["FLAP", "IFD"],@_); return $r->error_handler('S',"$sub: FAIL. Missing mandatory argume +nt 'IFD' in API call") unless (defined $args->{IFD}); $args->{IFD} = [$args->{IFD}] unless (ref $args->{IFD} ne "ARRAY") +; return $r->error_handler('S', "$sub: Empty list in argument 'IFD'" +) if (@{$args->{IFD}} == 0); $args->{FLAP} = 1 unless (defined $args->{FLAP}); my ($i,$ifd, $fpc); foreach $ifd (@{$args->{IFD}}) { ($fpc) = $ifd =~ /\w+-(\d+)\/\d+\/\d+/; $r->mode_shell; put_log("\n\n"); $r->put_log("Turn off/on laser on interface $ifd $args->{FLAP} + times"); foreach $i (1..$args->{FLAP}) { $r->shell(cmd => "cprod -A fpc$fpc -c \"test ifdev $ifd la +ser off\""); sleep 20; $r->shell(cmd => "cprod -A fpc$fpc -c \"test ifdev $ifd la +ser on\""); sleep 20; } } put_log("\n\n"); $r->mode_cli; return 1; }

I am trying to call this subroutine from a different file

$t->sadk_flap_mx_ifd_laser(ifd=>@child_interfaces, flap=>$flaps);

While executing the code, I see there are errors. I doubt am not passing the correct value . @childinterfaces have the valures, et-14/1/1, et-5/1/0..like that till 10 interfaces.

Sep 13 06:27:05 JT.pm:2879 ERROR JT::get_args: Argument "ET-5/1/0" is invalid JT::error_handler HARD at /volume/labtools/lib/JT.pm line 2739. JT::get_args('ARRAY(0x11ce6c00)', 'ifd', 'et-14/1/1', 'et-5/1/0', 'et-1/0/0', 'flap', 10) called at /volume/labtools/lib/Testsuites/PDT/lib/SADK.pm line 1032 Testsuites::PDT::lib::SADK::sadk_flap_mx_ifd_laser('JT::Test=HASH(0xdde63d0)', 'ifd', 'et-14/1/1', 'et-5/1/0', 'et-1/0/0', 'flap', 10) called at UC705_FT_testcases.pm line 267 UC705_FT_testcases::ae_child_link_flap_remote_lasers called at (eval 704) line 1

canu help here

Replies are listed 'Best First'.
Re: Passing array to a subroutine
by 1nickt (Prior) on Sep 13, 2017 at 13:44 UTC

    Pass a reference to the array instead:

    $t->sadk_flap_mx_ifd_laser( ifd => \@child_interfaces, flap => $flaps +);
    Then dereference it in the subroutine:
    sub sadk_flap_mx_ifd_laser { my %args = @_; my @child_interfaces = @{ $args{'ifd'} }; my $flap = $args{'flap'}; ... }

    Hope this helps!

    Update: removed unrelated link to perldsc, the correct doc is perlreftut as pointed out below by haukex.


    The way forward always starts with a minimal test.
Re: Passing array to a subroutine
by haukex (Abbot) on Sep 13, 2017 at 13:51 UTC

    1nickt gave you a solution, just to explain what's going on: In Perl, arguments to subroutines/methods are by default flattened into one big list. So for example, if you have an array my @x = ('y', 'z'); and a function call like func(a=>@x, b=>'c');, and considering that the => is just a special kind of comma, what the function ends up getting called with is ('a', 'y', 'z', 'b', 'c'). This flattening is described in perlsub. To learn more about references, which are scalars and won't get flattened, see perlreftut.

    A few minor updates for clarification.

Re: Passing array to a subroutine
by Anonymous Monk on Sep 13, 2017 at 14:14 UTC
    Conceptually, a "reference" is a piece of data which refers to another piece of data somewhere else. Let's say that you have a function that takes two arguments. If, say, the second of those arguments is a reference, the subroutine has access to everything that the reference refers-to. A piece of data will not be garbage-collected as long as any references to it exist anywhere.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1199302]
Front-paged by Corion
help
Chatterbox?
[ambrus]: the annotation mentions Brisbane as even worse, and I add Debrecen
[marto]: :)
[ambrus]: robby: dunno, I would like to visit London some time, although there are other places I'd like to visit more
[marto]: "Okay, sure, there are in fact museums and art galleries and other culturally enlightened things to do in Glasgow, but that wouldn't have made a funny comic"
LanX .oO( cemetery breweries ? )

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2017-12-15 11:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (431 votes). Check out past polls.

    Notices?