Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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/ that till 10 interfaces.

Sep 13 06:27:05 ERROR JT::get_args: Argument "ET-5/1/0" is invalid JT::error_handler HARD at /volume/labtools/lib/ 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/ 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 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 (Monsignor) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1199302]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2018-06-21 07:22 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.