in reply to Constants refferences

The short answer is that ARRAY_REF is a reference and STRING is not.

What you put between ${} or @{} needs to be a reference. By itself, STRING is not a reference—it's just a string. To get a reference to that string, use the backslash. Hence: "${\STRING}". It's the same as "${\'.+?'}".

If you have a bareword between ${}, or a literal string, that's taken as the name of a variable ("${STRING}" is the same as "$STRING"). If you're using strict, then that's going to bomb (unless that variable's been declared somewhere).

ARRAY_REF is already a reference, so you don't have to backslash it. You do, however, have to keep "@{ARRAY_REF}" from being interpreted as "@ARRAY_REF", and so the "+" is there to mark it as an expression and not just a bareword.

Replies are listed 'Best First'.
Re^2: Constants refferences
by gone2015 (Deacon) on Aug 21, 2008 at 17:28 UTC
    If you have a bareword between ${}, or a literal string, that's taken as the name of a variable ("${STRING}" is the same as "$STRING").

    It appears to be more complicated than that:

    use strict ; use warnings ; my $x = 57 ; print "\${x} = '", ${x}, "'\n" ; print "\${'x'} = '", ${'x'}, "'\n" ;
    gives:
    ${x} = '57' Can't use string ("x") as a SCALAR ref while "strict refs" in use .. +.
    Turning off "strict refs" gives:
    ${x} = '57' Use of uninitialized value in print ... ${'x'} = ''
    Replacing my $x by our $x, gives:
    ${x} = '57' ${'x'} = '57'
    The interpretation of the bareword case doesn't look desperately useful to me... Anybody know why Perl doesn't treat it as a subroutine/constant ?

    Sadly, this wouldn't help "${CONSTANT}" but it would mean that @{REF_ARRAY_CONSTANT} would do what might be expected.

      Ambiguous use of ${x} resolved to $x at - line 4.