$test <> ${test}

Discussion in 'Perl Misc' started by Curtis M. West, Oct 4, 2006.

  1. hello NG

    can anyone tell me, what the difference between $test and ${test} is?

    my $test = "abc";
    print $test;

    my $test = "abc";
    print ${test};

    both of the above examples do exactly the same?!

    tnx & greet, curtis
     
    Curtis M. West, Oct 4, 2006
    #1
    1. Advertising

  2. Curtis M. West

    -berlin.de Guest

    Curtis M. West <> wrote in comp.lang.perl.misc:
    > hello NG
    >
    > can anyone tell me, what the difference between $test and ${test} is?
    >
    > my $test = "abc";
    > print $test;
    >
    > my $test = "abc";
    > print ${test};
    >
    > both of the above examples do exactly the same?!


    That's because the meaning of $test and ${test} is exactly the same.

    The difference is only relevant when a variable is interpolated in a
    string, as in "this is the value: $test". If what follows the
    variable name could also be part of the variable name, the {}
    can be used to separate the name from surrounding text:
    "here$testthere" would try to interpolate the variable $testthere.
    "here${test}there" interpolates $test.

    Anno
     
    -berlin.de, Oct 4, 2006
    #2
    1. Advertising

  3. Curtis M. West <> wrote:

    > Subject: $test <> ${test}



    [
    I doubt you want the diamond operator there.

    I expect you meant either
    $test ne ${test}
    or
    $test != ${test}

    :)
    ]


    > can anyone tell me, what the difference between $test and ${test} is?



    There are no semantic differences.

    I think the syntactic difference is rather obvious. :)


    > my $test = "abc";
    > print $test;
    >
    > my $test = "abc";
    > print ${test};
    >
    > both of the above examples do exactly the same?!



    Yes.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 4, 2006
    #3
  4. Curtis M. West

    -berlin.de Guest

    Ferry Bolhar <> wrote in comp.lang.perl.misc:
    > Curtis M. West:
    >
    > > can anyone tell me, what the difference between $test and ${test} is?

    >
    > The braces are required in 2 cases:
    >
    > 1) If you use a variable name in an otherwise ambiguous context:
    >
    > $name = 'Config';
    > print "$name_Startup"; # prints nothing, looks for a variable
    > $name_Startup
    > print "${name}_Startup"; # prints "Config"
    >
    > because '_' is a valid character in variable names, the braces tell
    > the parse that the name of the scalar is delimited by them and the
    > rest is part of the string literal.
    >
    > 2) When you build variable names at run-time (symbolic references):


    Ah, but that is a different case.

    > $foobar = 'FooBar';
    > $x = 'foo';
    > $y = 'bar';
    > print ${$x . $y}; # prints "FooBar"


    This is general de-reference syntax with a symbolic reference. Here
    the braces form a block whose return value (the last statement) is
    taken as a reference.

    That is not the case in the first example. If it were, "${name}" would
    be a symref through a bareword. It wouldn't be applicable to lexical
    variables.

    Anno
     
    -berlin.de, Oct 5, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sting
    Replies:
    5
    Views:
    1,655
    Natty Gur
    Jun 8, 2004
  2. Robert Allan Schwartz
    Replies:
    7
    Views:
    395
    Marco Manfredini
    Aug 13, 2004
  3. Chris
    Replies:
    1
    Views:
    682
    Chris
    May 11, 2006
  4. Edvard Majakari
    Replies:
    4
    Views:
    706
    Edvard Majakari
    Feb 25, 2005
  5. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    760
    Skybuck Flying
    Oct 4, 2009
Loading...

Share This Page