Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Passing arrays and references to arrays

by GrandFather (Cardinal)
on Aug 10, 2005 at 10:40 UTC ( #482558=perlquestion: print w/ replies, xml ) Need Help??
GrandFather has asked for the wisdom of the Perl Monks concerning the following question:

In sample code for Tk::MatchEntry there is code similar to this fragment:

my @choices = [qw/one two three four/]; my $me = $mw->MatchEntry(-choices => @choices);

However this also works:

my @choices = (qw/one two three four/); my $me = $mw->MatchEntry(-choices => \@choices);

as does this:

my $choices = [qw/one two three four/]; my $me = $mw->MatchEntry(-choices => \@$choices);

While I think I understand what the last two variants are doing, I'm a little fuzzy on the first one - is it assigning an anonymous array to element 0 of @choices, or is something else going on? Is there any significant difference between each of the techniques?


Perl is Huffman encoded by design.

Comment on Passing arrays and references to arrays
Select or Download Code
Re: Passing arrays and references to arrays
by Taulmarill (Deacon) on Aug 10, 2005 at 11:01 UTC
    I'm a little fuzzy on the first one - is it assigning an anonymous array to element 0 of @choices, or is something else going on?

    i think so, too. but if you don't trust yourself, go on and use Data::Dumper to find out.
    i don't see a difference between these three techniques from the called funktion point of view. however, i would do this a bit different:
    my $choices = [qw/one two three four/]; my $me = $mw->MatchEntry(-choices => $choices);
    or did i miss some obvious reason why that wouldn't work?

      It does work. Variant number 4 :). Guess I was array fixated by the first two variants.


      Perl is Huffman encoded by design.
        If I understand everything correctly then I don't think this is a fourth variant:
        \@$choices
        compared to
        $choices
        I think you need to look up array refs and pay particular attention to referencing and dereferencing. In both cases you have an array ref, however in the first you deref it only to then make it a reference again. The second doesn't do this unneccessary merry go round.

        HTH.
Re: Passing arrays and references to arrays
by davorg (Chancellor) on Aug 10, 2005 at 11:23 UTC

    You're exactly right in your analysis of what is going on and I'd consider that a serious bug in the documentation of the module. It actually gets worse when you see the rest of that code sample:

    my @choices = [ qw/one one.green one.blue one.yellow two.blue two.green two.cyan three.red three.white three.yellow/ ]; $mw->Button->pack(-side => 'left'); my $me = $mw->MatchEntry( -choices => @choices, -fixedwidth => 1, -ignorecase => 1, -maxheight => 5, -entercmd => sub { print "callback: -entercmd\n"; }, -onecmd => sub { print "callback: -onecmd \n"; }, -tabcmd => sub { print "callback: -tabcmd \n"; }, -zerocmd => sub { print "callback: -zerocmd \n"; }, )->pack(-side => 'left', -padx => 50);

    Now consider what happens if @choices contains more than one element (as you'd expect to be able to do with an array). It gets particularly bad if the array has an even number of elements - all of the following parameters will be broken.

    I recommend you raise a bug on rt.cpan.org and, in the meantime, make a note of the problem on AnnoCPAN.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      I already raised a bug, but without fully understanding the code. I'll go back and modify the bug report.


      Perl is Huffman encoded by design.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-07-31 10:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls