Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Accesing single element without args

by Anonymous Monk
on Mar 31, 2015 at 02:56 UTC ( [id://1121933]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

to be more precise. Here is the code of a sub

sub interface_control { my $s = shift; my (@r, $v, @h); $s->get_handles(rtrs=>\@r, vars=>\$v, hosts=>\@h);

get_handles returns the handle for the args specified. These args are stored as hash/key pair in a file

Call to the sub is made via interface_control($s)

MY question is , i want to specifically access, one of the element in the array @r, say only $r[0]. Is there a way to access this or pass any reference to interface_control call

Replies are listed 'Best First'.
Re: Accessing single element without args
by Athanasius (Archbishop) on Mar 31, 2015 at 03:26 UTC

    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,

      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,

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1121933]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-23 20:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found