Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

DIFFERENCE BETWEEN VARIABLE

by iguane (Beadle)
on Jan 31, 2001 at 15:08 UTC ( #55457=perlquestion: print w/replies, xml ) Need Help??

iguane has asked for the wisdom of the Perl Monks concerning the following question:

Who could say to me the difference between

$output and ${'output'}


Because it'seems that the two element look diffrent when a script was execute.
Thanks

Replies are listed 'Best First'.
Re: DIFFERENCE BETWEEN VARIABLE
by ChOas (Curate) on Jan 31, 2001 at 15:18 UTC
    I can tell you that you should use:

    • strict
    • -w
    • $output

    when using strict, and -w ${'output'} results in:

    Can't use string ("output") as a SCALAR ref while "strict refs" in use at ./lal line 8.

    When you use ${'output'} you are trying to dereference 'output'...
    To be more exact: using $output means using whatever output contains.
    using ${'output'} means using whatever 'output' points to, since it is highly
    unlikely that 'output' is/will ever become a valid reference, this results in an
    error (caught by 'use strict')

    GreetZ!,
      ChOas

    print "profeth still\n" if /bird|devil/;
Re: DIFFERENCE BETWEEN VARIABLE
by autark (Friar) on Jan 31, 2001 at 18:36 UTC
    Actually, there is a difference between $output and ${'output'}:
    $output = "global"; { my $output = "my"; print $output; print ${'output'}; }
    This little example will first print out "my" then "global". This is because the latter is a symbolic reference to the $output variable - hence it will always refer to the global variable (which is stored in the package stash).

    ${output} on the other hand is not a symbolic reference, and will still refer to the lexical variable (if one exists).

    Autark.

Re: DIFFERENCE BETWEEN VARIABLE
by quidity (Pilgrim) on Jan 31, 2001 at 17:20 UTC

    There is no difference, as far as perl is concerned, this is often a useful construct (albeit without the ' ' characters) which allows variables to be interpolated in strings next to other [A-Za-z_] characters:

    $varname = 'poke'; print "${varname}mon\n";

    Will output:

    pokemon

    while you could not have said:

    print "$varnamemon\n";

    as there is no such variable. You could avoid this by using . instead, but you'll see instances of the construct above.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://55457]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2021-12-01 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (11 votes). Check out past polls.

    Notices?