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

In Perl 5.8 release documentation says
The syntaxes @a->[...] and %h->{...} have now been deprecated

So question is
What is the correct syntax for 5.8 version.

Edited 2003-06-11 by Ovid

  • Comment on The syntaxes @a->[...] and %h->{...} have now been deprecated

Replies are listed 'Best First'.
Re: The syntaxes @a->[...] and %h->{...} have now been deprecated
by BrowserUk (Patriarch) on Jun 11, 2003 at 12:34 UTC

    $a->[...];

    $h->{...};

    This has always been the correct syntax. The forms you show only worked because of an ommision.

    There was a recent thread discussing this %h->{foo} vs. $h{foo}.. which you may like to read as it will save it being repeated again here.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


      Thank you very much. My query is now resolved.
Re: The syntaxes @a->[...] and %h->{...} have now been deprecated
by tachyon (Chancellor) on Jun 11, 2003 at 12:31 UTC
    @a = ( 0, 1, 2, 3, 4 ); print 'could ', @a->[2], $/; print 'use ', $a[2], $/; print 'or(bad)', @a[2], $/;

    The 'bad' syntax works but gets a single element 'slice' and is in array (not scalar) context which may behave in slightly unexpected ways for which I can not generate a one line example. Same holds for hashes $h{foo} instead of %h->{foo}

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      The 'bad' syntax works but gets a single element 'slice' and is in array (not scalar) context which may behave in slightly unexpected ways for which I can not generate a one line example.

      I don't think this really has anything to do with slices. Nor is it a matter of context. It's just a syntactical freak of Perl nature... Frankenstyle.

      Some examples:

      perl -le '@foo = qw( f o o ); print @foo->[0]' # Frankenstyle perl -le '@foo = qw( f o o ); print @foo->[0,1,2]' # Frankenslice? perl -le '@foo = qw( f o o ); print @foo[0,1,2]' # Working slice. #### And a demo of context... perl -wle 'sub c {print wantarray ? "list" : "scalar" } my @foo; @foo += c;' perl -wle 'sub c {print wantarray ? "list" : "scalar" } my @foo; $foo[ +0] = c;' perl -wle 'sub c {print wantarray ? "list" : "scalar" } my @foo; @foo[ +0,1] = c;' # And Frankenstyle.... perl -wle 'sub c {print wantarray ? "list" : "scalar" } my @foo; @foo- +>[0] = c;'

      -sauoq
      "My two cents aren't worth a dime.";
      
Re: The syntaxes @a->[...] and %h->{...} have now been deprecated
by nite_man (Deacon) on Jun 11, 2003 at 12:41 UTC
    $a->[0] and $h->{first}
    or
    $$a[0] and $$h{first}
    Look at perlref.
          
    --------------------------------
    SV* sv_bless(SV* sv, HV* stash);
    
Re: The syntaxes @a->[...] and %h->{...} have now been deprecated
by Aragorn (Curate) on Jun 11, 2003 at 13:15 UTC
    In for example Perl 5.005, this gets executes without problems:
    #!/usr/bin/perl -w use strict; my @ary = qw(one two three); my %hash = (one => "One", two => "Two", three => "Three"); print @ary->[0]; print %hash->{one};
    Perl 5.005 doesn't complain at all, but 5.8.0 (rightly) complains about it. Use either arrays (hashes) or array (hash) references, and not some weird mix of the 2.

    Arjen

Re: The syntaxes @a->[...] and %h->{...} have now been deprecated
by Skeeve (Parson) on Jun 11, 2003 at 12:16 UTC
    I never saw that syntax. What should it mean? Do you mean something like %$h->{...} and not %h->{...}?
      Even I also don't know. perl 5.8 release document says...... Using arrays or hashes as references (e.g. "%foo->{bar}" has been deprecated for a while. Now you will get an optional warning.
Re: The syntaxes @a->[...] and %h->{...} have now been deprecated
by rakesh1377 (Novice) on Jun 11, 2003 at 12:25 UTC
    Perl 5.8 release document says...... Using arrays or hashes as references (e.g. "%foo->{bar}" has been deprecated for a while. Now you will get an optional warning.