On Wed May 13 2009 @  9:22, Steve Bertrand wrote:
> Jim Gibson wrote:
> > The first argument of push should be an array, not a scalar (even if that
> > scalar is a reference to an array).
> > 
> >     push( @{$graph_data->[0]}, $x_axis );
> 
> I'll have to do a bit of reading, because I can't remember why the
> braces are important here. All I know is that it works ;)

The general rule for using a reference is write what you would have
written, but put the reference inside of brackets. So, for example, 

    Regular       Reference

    @array        @{$array_ref}
    %hash         %{$hash_ref}

If the reference itself is a simple scalar variable, then the brackets
aren't required. So both of the cases above could be written more simply as
@$array_ref and %$hash_ref. However, if the reference is not a simple
scalar variable, then the brackets are required, not optional.

In your case, the variable $graph_data->[0] is itself a reference, so the
brackets aren't optional.

The other thing that may be confusing you is that if you are referring to 
a single item from a hash or array, you can use the arrow syntax rather
than the bracket syntax.

So, deciding what you need is a two stage process:

  (1) Am I trying to use an entire hash or array reference?
        (a) Yes: wrap the reference inside of { } and put the proper sigil
            on it. Eg, @{$array_ref} %{$hash_ref}
        (b) Yes and the reference itself is a simple scalar: ok, then the
            braces are optional. Eg, @$array_ref, %hash_ref

  (2) Am I trying to use a scalar from an array or hash?
        (a) Yes: Instead of ${$ref} use $ref->

Some people say always use the brackets for whole arrays or hashes (ie, 1a
rather than 1b) simply because fewer choices are fewer things to remember
or mess up. Others hate the look of @{$array_ref} so much that they say use
1b whenever possible.

This may help: http://www.perlmonks.org/?node_id=69927

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to