Newbie needs help...

T

thierry

Hi,

I am learning perl, and I have some questions regarding a script I am
studying .
Here is a portion of my script :
my (@sargs) = @_;


is it equivalent with : my @sargs = @_; ? (without bracket)

After there is :
my $config = {"delete" => 0, soFiles => []};
Is it right that $config is a hash table ?
I have a doubt, because I would write : my %config = ( "delete" => 0,
soFiles => [] );


is these 2 lines are equivalent ?

my $config = {"delete" => 0, soFiles => };
my $config = {"delete" => 0, "soFiles" => };


After that, delete is initialized like this :

$config->{delete} = 1; # why -> is it a reference ?

why not to use : $config{delete} = 1 ?

Thanks for your help. And sorry for these questions...

thierry
 
M

Matt Garrish

thierry said:
Hi,

I am learning perl, and I have some questions regarding a script I am
studying .
Here is a portion of my script :
my (@sargs) = @_;

is it equivalent with : my @sargs = @_; ? (without bracket)

Yes. The parens are just noise in this case. They are necessary if you were
trying to assign the values to scalars, for example:

my ($var1, $var2, $var3) = @_;

But, there's no need to force list context on an array as you're doing
above.
After there is :
my $config = {"delete" => 0, soFiles => []};
Is it right that $config is a hash table ?
I have a doubt, because I would write : my %config = ( "delete" => 0,
soFiles => [] );

$config is not a hash, but a scalar that contains a reference to an
anonymous hash. You can create named hashes the way you're doing, or create
anonymous ones using braces (and scalars!). The big difference (aside from a
name) lies in how you access the values. In your named hash, you would
simply write $config{'delete'}, whereas in the anonymous hash you would
first have to dereference the reference: $$config{'delete'}.
is these 2 lines are equivalent ?

my $config = {"delete" => 0, soFiles => };
my $config = {"delete" => 0, "soFiles" => };

Yes, but more noise. There is no reason to use double quotes around the keys
(see perlfaq7).
After that, delete is initialized like this :

$config->{delete} = 1; # why -> is it a reference ?

As I mentioned above, $config contains a reference.
why not to use : $config{delete} = 1 ?

As I mentioned above, you can also use $$config{'delete'}. Either way, you
have to derefence reference. If the above worked, how would you
differentiate between %config and the anonymous hash in $config?

Time you got started on the perldocs and perlfaqs if you want a more
complete explanation (in particular: perldata, perlreftut and perlref).

Matt
 
P

Paul Lalli

Hi,

I am learning perl, and I have some questions regarding a script I am
studying .
Here is a portion of my script :
my (@sargs) = @_;


is it equivalent with : my @sargs = @_; ? (without bracket)

It will help to get your terminology correct.
() => Parentheses
[] => Brackets
{} => Braces

To answer the question, yes, those statements are equivalent. The
parenteses are unnecessary in this case.
After there is :
my $config = {"delete" => 0, soFiles => []};
Is it right that $config is a hash table ?

Not quite, no. $config is a reference to a hash, not an actual hash.
Read perldoc perlref for more info on references.
I have a doubt, because I would write : my %config = ( "delete" => 0,
soFiles => [] );

That would be a hash.
is these 2 lines are equivalent ?

my $config = {"delete" => 0, soFiles => };
my $config = {"delete" => 0, "soFiles" => };

Yes. The => operator is actually a different way of writing a simple
comma, with one important exception: it automatically quotes any bareword
on its left. Note that this works only for a single bareword, however.
In other words these two are *not* equivalent:

my %c = ( 'foo bar' => 0 );
my %c = ( foo bar => 0 );
and the second one will in fact give an error.
After that, delete is initialized like this :

$config->{delete} = 1; # why -> is it a reference ?

Yes. As mentioned above, $config is a hash reference, not a hash. The ->
operator is the method for dereferencing a reference and accessing an
element of the resulting hash.
why not to use : $config{delete} = 1 ?

That's how you would access the 'delete' key of a hash named %config. But
you didn't have a hash named %config. You had a hash reference named
$config.

Paul Lalli
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top