Hello Mark,

On Fri, 27 Nov 2009 19:48:09 -0800 (PST)
Mark_Galeck <[email protected]> wrote:

> Hello, the following question I posted on perl.beginners but for a few
> days there is no response, so maybe it is not a beginners question?
> Nah, it must be :)

This question bounces on some subtleties in Perl's references and
syntax.

> --------------------------------
> If I can do this:
> 
> $ref = \...@foobar;
> print @$ref;

Yes.

> then why can't I do this:
> 
> print @\...@foobar;

This appears to be a syntax error, probably having to do with the @\@
not parsing as you expect. If you re-write it as

print @{...@foobar};

is works as you probably intend. Unfortunately, I cannot imagine why
you would want to do that.
        
> ------------------------------------
> I have this kind of problem often, let me give another example, I
> think it is similar, let me know if not.

This is a completely different issue.

> Why are all the printouts different:
> 
> @foobar = ();
> print \...@foobar;  #prints as reference to ARRAY

As it should.
 
> $foobar = \();
> print $foobar;  # prints as reference to SCALAR

Although many people misunderstand it, () does not create a list. The
parens only provide grouping. In this case, you are taking a reference
to a grouped nothing and perl is interpreting that as a request for
a reference to an undef scalar.

> print \();  #prints as array of references to SCALAR, in this case
> empty array

Correct.

I would definitely suggest spending quality time with perlref and
perlreftut. They will help you work through the syntactic oddities in
references. There is a really good discussion of some of this at the
Perl Monastery (http://perlmonks.org/?node_id=779217)

Many people develop habits that reduce some of the confusion. I only
derefence references in one of two ways (out of several).

* If I need the whole item, I use the sigil and curlies. I never use
just the derefencing sigil.

   @{$foo} not @$foo

It reads better to me and is less prone to the kinds of syntax attack
you had above.

* If I need an element of a referenced array or hash, I use the arrow
notation.

  $foo->{'a'} not ${$foo}{'a'}

I find that the arrow helps me later in reading the code. The visual
reminder of the reference simplifies my own code reading.

I would definitely suggest you check out the Perl Monastery. It is
a great community for helping with questions like this.

G. Wade
-- 
Against logic there is no armor like ignorance. -- Laurence J. Peter

Reply via email to