Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

deference and inreference operators

by drock (Beadle)
on Sep 24, 2004 at 15:46 UTC ( #393558=perlquestion: print w/replies, xml ) Need Help??

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

I would like some basic questions answered: what exactly does => and -> mean and what is the difference? what are the difference between ' and ". I know ' is NOT interpolated and " is but what exactly does interpolted mean? thanks, derek

Replies are listed 'Best First'.
Re: deference and inreference operators
by ikegami (Patriarch) on Sep 24, 2004 at 15:57 UTC

    => is exactly like the list seperator ,, but you can put barewords on the left hand side.

    @a = ('a', 4); # ok @a = ('a' => 4); # ok @a = (a => 4); # ok @a = (a, 4); # XXX ERROR print("Hello" => " " => "World" => "\n"); # Why not. # Often used for hashes: %h = ( key => 'value' );

    -> dereferences a reference:

    @a = qw( a b c ); $ap = \@a; print($ap->[2]); # prints: c %h = ( key => 'value' ); $hp = \%h; print($hp->{key}); # prints: value $obj = MyClass->new(); $obj->method();

    To interpolate something (as relevant here) is to substitute it with its value:

    $a = 'test'; print('$a\n'); # prints: $a\n print("$a\n"); # prints: test and a newline print("\$a\n"); # prints: $a and a newline @a = qw( a b c ); print('@a\n'); # prints: @a\n print("@a\n"); # prints: abc and a newline print("\@a\n"); # prints: @a and a newline
Re: deference and inreference operators
by Zaxo (Archbishop) on Sep 24, 2004 at 15:55 UTC

    You should take your time and work through the perlsyn and perlop manpages (or perldocs). That will give you a solid foundation as you learn perl. merlyn's Llama book would be a gentler alternative.

    "Interpolated" means that the values of variables are substituted where the variables occur. That happens for scalars and arrays - items with the $ and @ sigils.

    After Compline,

Re: deference and inreference operators
by davido (Cardinal) on Sep 24, 2004 at 16:13 UTC

    First I'll answer the quoting question. Try the following snippet.

    my $text = "Hello world\n"; my $var = 'Hello world\n'; print $text; print $var;

    In the case of $text, we used " (double quotes) and thus, \n was expanded to mean a newline. In the case of $var, we used single quotes, and \n remained the literal characters \ and n.

    Now try this code:

    my $text = "Hello world\n"; print "$text"; print '$text';

    In the case of the first print statement, the variable $text is interpolated within the string (its value is put into the string). In the case of the second print statement, $text is the literal text printed.

    Now for =>. => is sometimes called the "fat comma". It's most common use is in declaring elements of a hash, as in:

    my %hash = ( This => 10, That => 20 );

    The fat comma also has the effect of 'single quoting' the text immediately to its left. So the above snippet is equal to:

    my %hash = ( 'This' , 10, 'That' , 20 );

    Now for ->. That is used for dereferencing, which is an entirely different subject, having nothing to do with the => fat comma. If $aref is a reference to an array, one of the notations you can use to dereference that ref, and grab a single element is to use the dereference operator ->, like this: $aref->[3]. That's the same as:  ${$aref}[3], and a lot less ambiguous than something like $$aref[3].

    As Zaxo has stated, you should have a look at perlsyn, perlop, and perlreftut.


Re: deference and inreference operators
by ggg (Scribe) on Sep 24, 2004 at 22:30 UTC
    Thanks for asking that, drock. The -> operator has puzzled me for a while, too.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://393558]
Approved by kutsu
Front-paged by Theo
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2023-01-29 05:56 GMT
Find Nodes?
    Voting Booth?

    No recent polls found