Newbie needs help...

Discussion in 'Perl Misc' started by thierry, Jul 28, 2004.

  1. thierry

    thierry Guest

    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
     
    thierry, Jul 28, 2004
    #1
    1. Advertising

  2. thierry

    Matt Garrish Guest

    "thierry" <> wrote in message
    news:...
    > 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
     
    Matt Garrish, Jul 28, 2004
    #2
    1. Advertising

  3. thierry

    Paul Lalli Guest

    On Wed, 28 Jul 2004, thierry wrote:

    > 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
     
    Paul Lalli, Jul 28, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SD
    Replies:
    1
    Views:
    448
  2. D
    Replies:
    0
    Views:
    473
  3. Alfred B. Stansbury

    Newbie needs help with perl compilation errors

    Alfred B. Stansbury, Oct 21, 2004, in forum: Perl
    Replies:
    1
    Views:
    746
    Joe Smith
    Oct 22, 2004
  4. T.Horton

    NEWBIE NEEDS HELP!

    T.Horton, Mar 4, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    318
    T.Horton
    Mar 4, 2004
  5. Mike

    Newbie needs some help

    Mike, May 22, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    293
    [MSFT]
    May 25, 2004
Loading...

Share This Page