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

Please help! In the following code I cannot get the value of $tura outside the subroutine:
my $sef1=$mw1->BrowseEntry(-variable=>\$it1,-width=>22, -state=>'reado +nly', -browsecmd=>\&tura)->place(-x=>250,-y=>53); sub tura{ my $client=MongoDB::MongoClient->new("host"=>"10.9.602.179:27017") +; my $db=$client->get_database("test"); my $col_tura=$db->get_collection('atco_data')->find({"unit_id" => +"1" , "nume"=>"$it1"}, {"tura"=>1}); my @cursor_tura=$col_tura->all; my $tura=$cursor_tura[0]->{'tura'}; print "$tura\n"; }

Replies are listed 'Best First'.
Re: variable access
by choroba (Cardinal) on Jan 06, 2015 at 09:57 UTC
    You declared the variable with my. It means its scope is limited - it exists only till the following closing curly bracket.

    Why do you need the variable? You can declare it outside the sub to make it accessible to the outer code, but there might be a better solution to your problem - but we don't know the problem.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      I need that variable for another querry in MongoDB and my problem is that I cannot use it outside the subroutine, I printed it only to see it.
Re: variable access
by marto (Cardinal) on Jan 06, 2015 at 09:58 UTC
      I used "return" but no result. I cheked "perlsub" before asking for help.
        I used "return" ...

        But how did you use return? Are you saying that adding the statement
            return $tura;
        after the debug print statement that showed you that  $tura was something you expected and then assigning the returned value to another lexical
            my $other_lexical = tura();
        passed nothing back from the subroutine? This seems doubtful...


        Give a man a fish:  <%-(-(-(-<

Re: variable access
by Anonymous Monk on Jan 06, 2015 at 10:19 UTC
    See Tk::BrowseEntry, you don't need to use variable option, the callback gets the option text
    #!/usr/bin/perl -- use strict; use warnings; use Tk; use Tk::BrowseEntry; Main( @ARGV ); exit( 0 ); sub Main { GoTk(); GoTk(); } sub GoTk { my $mw = tkinit; my $b = $mw->BrowseEntry( -browsecmd => \&tura, )->pack; $b->insert("end", "Tapioca"); $b->insert("end", "Figgy duff"); $b->insert("end", "Haggis"); $mw->MainLoop; } sub tura { print "@_\n"; } __END__ $ perl tk-scope-BrowseEntry-tura.pl Tk::BrowseEntry=HASH(0xb9298c) Haggis Tk::BrowseEntry=HASH(0xb9298c) Tapioca Tk::BrowseEntry=HASH(0xb9298c) Figgy duff Tk::BrowseEntry=HASH(0xfb3844) Figgy duff
      Thank you! I will try to adapt your code, but I'm not looking for the list from BrowseEntry, I need a value (unique) from a querry from a MongoDB when I select a value from the list.

        Thank you! I will try to adapt your code, but I'm not looking for the list from BrowseEntry, I need a value (unique) from a querry from a MongoDB when I select a value from the list.

        Um, ok ... see Re: Tk: Creating label in hash (I almost wrote one of these again, but I guess I got confused as to your question)

        If that doesn't help, need more info, for example, where is the value of $tura supposed to end up?