Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

why can't I collapse reference variables?

3 views
Skip to first unread message

Mark_Galeck

unread,
Nov 27, 2009, 10:48:09 PM11/27/09
to scr...@perl.org
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 :)

--------------------------------
If I can do this:

$ref = \@foobar;
print @$ref;

then why can't I do this:

print @\@foobar;
------------------------------------
I have this kind of problem often, let me give another example, I
think it is similar, let me know if not.

Why are all the printouts different:

@foobar = ();
print \@foobar; #prints as reference to ARRAY

$foobar = \();
print $foobar; # prints as reference to SCALAR

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

James E Keenan

unread,
Nov 28, 2009, 9:07:22 AM11/28/09
to scr...@perl.org

On Nov 27, 2009, at 10:48 PM, Mark_Galeck wrote:

> $ref = \@foobar;
> print @$ref;
>
> then why can't I do this:
>
> print @\@foobar;

use strict;
use warnings;

> ------------------------------------
> I have this kind of problem often,

If you're having this problem often, it's probably because you're not
using strictures and warnings often.


G. Wade Johnson

unread,
Nov 28, 2009, 9:37:43 AM11/28/09
to Mark_Galeck, scr...@perl.org
Hello Mark,

On Fri, 27 Nov 2009 19:48:09 -0800 (PST)
Mark_Galeck <mark_galeck...@yahoo.com> 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

Vlado Keselj

unread,
Nov 30, 2009, 6:18:59 AM11/30/09
to Mark_Galeck, scr...@perl.org

On Fri, 27 Nov 2009, Mark_Galeck 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 :)

I cannot provide brief explanations, but this may help:

> --------------------------------
> If I can do this:
>
> $ref = \@foobar;
> print @$ref;
>
> then why can't I do this:
>
> print @\@foobar;

use: print @{ \@foobar };

It seems to work.

> ------------------------------------
> I have this kind of problem often, let me give another example, I
> think it is similar, let me know if not.
>
> Why are all the printouts different:
>
> @foobar = ();
> print \@foobar; #prints as reference to ARRAY

This is OK.

>
> $foobar = \();
> print $foobar; # prints as reference to SCALAR

use: $foobar = []; print $foobar;

and you get the output as before.

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

use: print [];

For creating a reference to a literal array, use: [1, 2, 3] instead of
\(1,2,3)

Cheers,
--Vlado

0 new messages