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

Somewhere in Main application call the dirtree_sub below to choose a working area. Once user select the work area through dirtree, it will set the path in $area entry box. But when I add $dirtree_frame->destroy in dirtree_sub below, it print warning:

Useless use of reference constructor in void context at geometry_place +.pl line 564. Useless use of reference constructor in void context at geometry_place +.pl line 570.

Any idea how to solve this?

Some code in main application:

my $area = $mw->Entry(-background => "white", -foreground => "black", +-width => 51)->place(-x=>100, -y=>$ystart + 60); $area->insert('end', "$workarea"); sub dirtree_sub { if ($area->get() ne "") { $CWD = $area->get(); } else { $CWD = Cwd::cwd(); }; my $dirtree_frame = $mw->Toplevel; $dirtree_frame->title("Select work area"); my $DIR_TREE = $dirtree_frame->Scrolled('DirTree', -scrollbars => "osoe", -width => 50, -height => 25, -exportselection => 1, -browsecmd => sub {$CWD = shift}, -command => sub {\&show_cwd(); $dir +tree_frame->destroy})->pack(-fill => "both", -expa +nd => 1); $DIR_TREE->chdir($CWD); my $button_frame = $dirtree_frame->Frame()->pack(-side => "bottom" +); $button_frame->Button(-text => "Ok", -command => sub {\&show_cwd(); $dirtree_fram +e->destroy})->pack(-side => "left"); $button_frame->Button(-text => "Exit", -command => [$dirtree_frame=>'destroy'])->pa +ck(-side => "left"); }; sub show_cwd { $area->delete('0.0', 'end'); $area->insert('end', $CWD); }

Replies are listed 'Best First'.
Re: Useless use of reference constructor in void context
by Athanasius (Archbishop) on Jul 29, 2012 at 15:12 UTC

    Hello myfamilygeneral, and welcome to the Monastery!

    The problem is not with $dirtree_frame->destroy, but with the immediately preceding term \&show_cwd(). Consider the following:

    #! perl use strict; use warnings; sub foo { print "in foo()\n"; } sub bar { \&foo(); print "in bar()\n"; } bar();

    Output:

    Useless use of reference constructor in void context at test.pl line 1 +2. in foo() in bar()

    But remove the backslash (which does nothing useful anyway) from before &foo(), and the warning goes away.

    Update: The & sigil probably does nothing useful either. See What's the difference between calling a function as &foo and foo()?

    HTH,

    Athanasius <°(((><contra mundum

      s/The & sigil probably does nothing useful either./The & sigil does nothing useful here, and, generally, may do something you won't like./;
      Thanks a lot this was very useful.
Re: Useless use of reference constructor in void context
by NetWallah (Canon) on Jul 29, 2012 at 14:33 UTC
    Please use "code" tags to preserve code formatting.

    In this snippet:

    sub {\&show_cwd(); $dirtree_frame->destroy})->pack(-fill => "both", -expand => 1 +);
    The "sub" returns a code-reference
    The "->pack" is a dereference followed by a call to the "pack" method.

    Since the subref does not de-reference to something that allows a method call, perl complains.

    Update 1: Sorry - this is rubbish. I just noticed the "), which means you are calling "pack" on whatever is returned by "$dirtree_frame->Scrolled".

    Update 2: New theory :

    $dirtree_frame is not created properly.
    Evidence is the TWO messages complaining about the object - one for each method call (Scrolled and destroy).
    Update 3: Athanasius (++) , below is right. And thanks for fixing the formatting, and Welcome to the Monastery!

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

Re: Useless use of reference constructor in void context
by zentara (Cardinal) on Jul 29, 2012 at 15:10 UTC
    If you havn't figured it out yet, please post a complete running example so we can run the code.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh