in reply to Accesing single element without args

If I understand correctly, you want to pass $r[0] back from sub interface_control to its caller? One way is to pass in a variable reference:

my $r; my $s = ...; interface_control($s, \$r); # use $r here ... sub interface_control { my ($s, $r_ref) = @_; my (@r, $v, @h); $s->get_handles(rtrs=>\@r, vars=>\$v, hosts=>\@h); $$r_ref = $r[0]; # dereference $r_ref and write to its referent ...

See perlreftut. (Or, of course, you could just return $r[0] at the end of the subroutine.)

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Accessing single element without args
by Anonymous Monk on Mar 31, 2015 at 03:38 UTC

    The problem here is i can't modify interface_control. It is a global sub which is accessed by many users

    @r, has list of routers r[0]....rn, . I want to display output only for r[0] and not for other router

    If u see the below code ( under sub interface_control), i want to print blah only for r[0]. If i call interface_control($s), it may end up printing for all routers

    foreach my $rh (@r) { print ("blah blah");

      Here is one (inelegant) approach which might work for you:

      #! perl use strict; use warnings; use Capture::Tiny ':all'; my ($stdout, $stderr, @result) = capture { interface_control(); }; my @lines = split "\n", $stdout; print $lines[0], "\n"; sub interface_control { my @r; get_handles(\@r); print "blah blah for $_\n" for @r; } sub get_handles { @{ $_[0] } = 11 .. 13; }

      Output:

      14:00 >perl 1204_SoPW.pl blah blah for 11 14:00 >

      Update: Removed unnecessary parentheses from print statement, plus a blank line.

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        That makes sense :). I will try to use it if my code reviewer approves :)

        A final query on my question. As i want to print only r[0], is there a way to set the array @r to only r[0] during interface_control call

        . I mean like this
        push (@r, $r[0])

        I know inside interface_control sub, i can set this, but interface_control can call from outside set this value ?

      Well, then there is nothing you can do, short of doing something foolish like using PadWalker