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

Hi all,

I am creating different telnet handles in a loop in diff threads and I want to store that handle in global array. I want to telnet to multiple targets simultaneously.

I am modifying legacy program so I need that global array with all telnet handles.

#!/usr/local/bin/perl -w use warnings; use threads; use threads::shared; use Net::Telnet; my (@TelnetHandler) :shared; my @UEListAll = (); my @threads; $RigNumber = "619"; my $i = 1; for ($i=1; $i <= 5; $i++) { my $retVal = `host ue$i-rig$RigNumber`; if($retVal =~ /not found/) { print "UE NOT CONNECTED \n"; } else { push(@UEListAll, $i); } } print "ARRAY : @UEListAll \n"; foreach my $TempUENum (@UEListAll) { my $telhost = "ue$TempUENum-rig619"; print "HOST: $telhost \n"; my $t = threads->new(\&ConnectToTelnet,$TempUENum,"$telhost",5,'#' +,1,"return", \@TelnetHandler); push(@threads,$t); } foreach (@threads) { my $num = $_->join; print "done with $num\n"; } print "\n****** THREAD ******** \n"; foreach my $tmp (@TelnetHandler) { print "Main TelNet Handle: $tmp \n"; } print "End of main program\n"; sub ConnectToTelnet { my ($ue, $host, $timeout, $prompt, $telnetmode, $errmode, $Ary_ref) += @_; print "Connecting to UE$ue: $host \n"; my $tid = threads->tid(); print "\nStarting Thread: $tid \n"; my $t_send = new Net::Telnet ( Host => $host, Timeout => $timeout, Prompt => '/'.$prompt.'$/', Errmode => $errmode, Telnetmode => $telnetmode ); if(!defined(t_send)) { print "Can't Telnet to UE \n"; } else { print "TELNET HANDLE UE$ue: $t_send \n"; $TelnetHandler[$ue] = \$t_send; push (@TelnetHandler, \$t_send); } }

I am getting an error run time error where I am trying to store value in an array. Please help me. thanks --girija

Replies are listed 'Best First'.
Re: How to pass a object value to global array in threads
by BrowserUk (Patriarch) on Oct 26, 2015 at 08:11 UTC

    As I told you in Re: Invalid value for shared scalar -while assigning object value to array - In Threads, what you are trying to do won't work.

    Not because of all the simple syntax errors you are making; but because performing concurrent operations on a single shared telnet session simply does not work. Telnet sessions are not designed to be shared.

    I even pointed you at Re: How do you share an object(Telnet Session) between threads? that explains why it won't work in more detail; and why it is completely unnecessary to share telnet sessions anyway.

    YOu are trying to do the wrong thing the wrong way; and there is a much simpler solution to your problem if you would take the time to understand it.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Yes I am trying to do wrong thing. but I am not able to get is in the explanation that 4rth thread will return the information to calling thread. but again i will be having same problem using that handle. See my problem is because of legacy code. I required to store all the telnet handle to an global array and that array of handles is used every where. so with your solution i will also have same problem.

      May be my question how to get all telnet start at a same time and get it handle stored in a array for further use. Is there is any better way to do it?

        See my problem is because of legacy code. I required to store all the telnet handle to an global array and that array of handles is used every where.

        That simply makes no sense. If modern code (of any type or language) cannot successfully make concurrent use of a shared telnet session; neither can legacy code.

        And if you cannot use the telnet handle concurrently, there is no reason and it makes no sense, to store them in a shared global array.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: How to pass a object value to global array in threads
by shmem (Chancellor) on Oct 26, 2015 at 07:38 UTC
    I am getting an error run time error where I am trying to store value in an array

    You are passing a reference of the array TelnetHandler to sub ConnectToTelnet, and assign it there to $Ary_ref , but later, you use the array @TelnetHandler:

    push (@TelnetHandler, \$t_send);

    That should be

    push (@$Ary_ref, \$t_send);

    update: Please use strict to see what is going wrong.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: How to pass a object value to global array in threads
by Anonymous Monk on Oct 26, 2015 at 08:48 UTC
    why are you insisting while BrowserUK has already given you a very satisfying explanation? It's like you never read his suggestions
      more like OP is turning a blind eye and hoping someone else will confirm his ideas are possible ... an optimitist

        Amazing that the same basic question comes up in the same month!

        I confirm that you can't share a Telnet object between threads. After understanding the responses to my post on the subject, the docs make sense: only scalers and references to scalers can be shared. A Telnet object is neither. It would be really handy if in the future this could be done, but until then... try a Telnet server thread that passes commands, status, and data via a shared common area. I.E. create your own mini API.

        For a simple example see: SDR Scanner(aka Threaded wxPerl Example)

        James

        There's never enough time to do it right, but always enough time to do it over...

      i did read that explanation couple of times but not tried also same solution but not satisfying my requirement. Not able to understand how to do it. nothing like i am not reading or something like that. believe me i did read and tried that option too.

         but not tried also same solution

        What does that mean? Are you saing you tried the solution but it didn't work? Or are you saying you didn't try the solution?

         but not satisfying my requirement.

        Your requirement is impossible :)

        A square peg cannot fit into a round hole