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

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

20040728 Edit by ysth: change title from "newbie needs help" and add code tags

Replies are listed 'Best First'.
Re: Perl data types and references
by Zaxo (Archbishop) on Jul 28, 2004 at 22:03 UTC
    1. Yes, they are equivalent. Parens are only needed if you have a list of scalar variables you want to assign from the first elements of @_.
    2. $config is assigned from a hash reference. The curlies produce one in that context.
    3. Also equivalent, fat comma ('=>') takes care of quoting the preceeding bare word.
    4. Because $config is a reference.

    See tye's References quick reference.

    After Compline,
    Zaxo

Re: Perl data types and references
by Enlil (Parson) on Jul 28, 2004 at 22:04 UTC
    Is my (@args) = @_; equivalent to my @sargs = @_;?
    Yes it is

    my $config = {"delete" => 0, soFiles =>[]};
    Is it right that $config is a hash table?
    Technically $config is not a hash table but a reference to a hash
    is these 2 lines are equivalent ?
    my $config = {"delete" => 0, soFiles => }; my $config = {"delete" => 0, "soFiles" => };
    Yes they are.
    $config->{delete} = 1; # why -> is it a reference ?

    Yes it is a reference

    why not to use : $config{delete} = 1 ?
    Because it is a reference and writing it that way (ie $config{delete} will look for a structure named %config and create/modify a key named delete to 1 (depending on strict and if %config exists). The first way does the same to the hash which $config refers to.

    -enlil

Re: Perl data types and references
by Joost (Canon) on Jul 28, 2004 at 22:12 UTC
    Please use code tags for all code, it makes it a lot easier to read. Anyway:

    my (@sargs) = @_;
    is it equivalent with : my @sargs = @_; ? (without bracket)
    Since @args = already forces list context, yes, they are equivalent. But note that
    my $var = @list;
    is not equivalent to
    my ($var) = @list;
    my $config = {"delete" => 0, soFiles => []};
    Is it right that $config is a hash table ?
    Well, $config is a reference to a hash table; a normal hash is initialized with a list: ( key => value ...) while a reference to an anonymous hash is created with { key => value .. }

    is these 2 lines are equivalent ?
    my $config = {"delete" => 0, soFiles => }; my $config = {"delete" => 0, "soFiles" => };
    Yes, keys that only contain alphanumeric characters can be left unquoted before the => operator.

    You should use an even number of element in a hash, though. (there is NO value for the soFiles key in your example. that isn't possible in a hash)

    After that, delete is initialized like this :
    $config->{delete} = 1; # why -> is it a reference ?
    why not to use :
    $config{delete} = 1
    $config->{ } is used to dereference the reference in $config. the reason that you can't use $config{delete} is because $config{delete} indicates an element in the hash %config, while $config->{delete} indicates an element in the hash pointed to by $config.

    You seem to have a reasonable understanding of programming techniques, but a little less sure of Perl itself. You might find the camel book enlightening. Also, putting this at the top of your code will help you spot bugs:

    #/usr/bin/perl use strict; use warnings; use diagnostics; # optional; turns on more descriptive warnings

    And we don't usually mind questions, so ask away. :-)

Re: Perl data types and references
by borisz (Canon) on Jul 28, 2004 at 22:06 UTC
    my (@sargs) = @_; is it equivalent with : my @sargs = @_; ? (without bracket)
    yes
    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 a hash reference, you create it with {}.
    is these 2 lines are equivalent ? my $config = {"delete" => 0, soFiles => }; my $config = {"delete" => 0, "soFiles" => };
    yes, => is a synonym for the comma, expect that any word to the left of it is interpreted as a string. But you need a value to the right of => or your hash is not created correct use use warnings;.
    After that, delete is initialized like this : $config->{delete} = 1; # why -> is it a reference ? why not to use : $config{delete} = 1 ?
    You created it as a has reference with the {}.
    my %conf = ( abc => 12 ); print $conf{abc}; # this is a hash my $conf_ref = { abc => 13 }; print $conf_ref->{abc}; # conf_ref is a reference to a hash
    Boris
Re: Perl data types and references
by PodMaster (Abbot) on Jul 29, 2004 at 07:20 UTC
    Here's a little tip
    C:\>perl -MO=Deparse,-p -e"my @fo = @bar;" (my(@fo) = @bar); -e syntax OK C:\>perl -MO=Deparse,-p -e"my(@fo) = @bar;" (my(@fo) = @bar); -e syntax OK C:\>perl -MO=Deparse,-p -e"my $f = { a => 1 };" (my $f = {'a', 1}); -e syntax OK C:\>perl -MO=Deparse,-p -e"my $f = { "a" => 1 };" (my $f = {'a', 1}); -e syntax OK
    Most of the time B::Deparse will show you how perl parsed your code.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Perl data types and references
by CountZero (Bishop) on Jul 28, 2004 at 22:11 UTC
    my (@sargs) = @_; and my @sargs = @_; are equivalent.

    my $config = {"delete" => 0, soFiles => []}; is a scalar variable holding a reference to a hash-table with two elements.  my %config = ( "delete" => 0, soFiles => [] ); indeed constructs a "classic" hash-table.

    my $config = {"delete" => 0, soFiles => }; my $config = {"delete" => 0, "soFiles" => };
    are equivalent as the => automatically quotes its lefthand argument.

    You must indeed use $config->{delete} = 1; as $config holds a reference to the hash.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Perl data types and references
by TilRMan (Friar) on Jul 29, 2004 at 13:26 UTC
Re: Perl data types and references
by thierry (Acolyte) on Jul 29, 2004 at 13:20 UTC
    THANKS a lot for your help. thierry