Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between `$this`, `@that`, and `%those` in Perl?

Tags:

types

perl

sigils

What is the difference between $this, @that, and %those in Perl?

like image 751
Mare Avatar asked Apr 28 '10 16:04

Mare


People also ask

What is the difference between -> and => in Perl?

What is the exact difference between :: and -> in Perl? -> sometimes works where :: does not. -> is used for dereferencing; :: is used for referring to other packages.

What are the three categories of Perl variable?

Perl has three main variable types: scalars, arrays, and hashes. A scalar represents a single value: my $animal = "camel"; my $answer = 42; Scalar values can be strings, integers or floating point numbers, and Perl will automatically convert between them as required.

What is the meaning of $_ in Perl?

The most commonly used special variable is $_, which contains the default input and pattern-searching string. For example, in the following lines − #!/usr/bin/perl foreach ('hickory','dickory','doc') { print $_; print "\n"; }

What does $@ mean in Perl?

In these cases the value of $@ is the compile error, or the argument to die.


1 Answers

A useful mnemonic for Perl sigils are:

  • $calar
  • @rray
  • %ash

Matt Trout wrote a great comment on blog.fogus.me about Perl sigils which I think is useful so have pasted below:

Actually, perl sigils don’t denote variable type – they denote conjugation – $ is ‘the’, @ is ‘these’, % is ‘map of’ or so – variable type is denoted via [] or {}. You can see this with:

my $foo = 'foo'; my @foo = ('zero', 'one', 'two'); my $second_foo = $foo[1]; my @first_and_third_foos = @foo[0,2]; my %foo = (key1 => 'value1', key2 => 'value2', key3 => 'value3'); my $key2_foo = $foo{key2}; my ($key1_foo, $key3_foo) = @foo{'key1','key3'}; 

so looking at the sigil when skimming perl code tells you what you’re going to -get- rather than what you’re operating on, pretty much.

This is, admittedly, really confusing until you get used to it, but once you -are- used to it it can be an extremely useful tool for absorbing information while skimming code.

You’re still perfectly entitled to hate it, of course, but it’s an interesting concept and I figure you might prefer to hate what’s -actually- going on rather than what you thought was going on :)

like image 135
draegtun Avatar answered Sep 26 '22 16:09

draegtun