lexical declaration and array dereference

Discussion in 'Perl Misc' started by ko, Aug 27, 2003.

  1. ko

    ko Guest

    Quick question. The code snippet works under the following builds:

    v5.6.1 built for MSWin32-x86-multi-thread (Activestate 635)
    v5.6.1 built for i386-freebsd (pkg_info = perl-5.6.1_11)

    ==CODE==
    use strict;
    use warnings;

    my $string = '1 2 3 4';
    my @$ar = split(/\s+/,$string);
    print "@$ar\n";

    ==RESULTS==
    1 2 3 4

    But if run under Activestate 804 or 5.005_03 for i386-freebsd (taking
    out 'use warnings, and replacing with #!/usr/bin/perl -w), I get the
    following error message:

    'Can't declare array dereferences in my at...'

    Looking at the error message, I would guess that you're only allowed
    to declare scalar, array, or hash variables? Being a novice
    programmer, I thought the declaration was ok through autovivification.
    Could someone please explain what I am missing and why it works under
    Perl 5.6.1?

    Thanks in advance
    keith
    ko, Aug 27, 2003
    #1
    1. Advertising

  2. ko

    Uri Guttman Guest

    >>>>> "k" == ko <> writes:

    k> Quick question. The code snippet works under the following builds:
    k> v5.6.1 built for MSWin32-x86-multi-thread (Activestate 635)
    k> v5.6.1 built for i386-freebsd (pkg_info = perl-5.6.1_11)

    k> my @$ar = split(/\s+/,$string);

    k> But if run under Activestate 804 or 5.005_03 for i386-freebsd (taking
    k> out 'use warnings, and replacing with #!/usr/bin/perl -w), I get the
    k> following error message:

    k> 'Can't declare array dereferences in my at...'

    it has nothing to do with build numbers but perl versions. i tried that
    on 5.004_04, 5.005_02, 5.005_03 and 5.6.1. it works only on 5.6.1 so
    they probably added the ability to autovivify and my declare in one
    statement.

    k> Looking at the error message, I would guess that you're only allowed
    k> to declare scalar, array, or hash variables? Being a novice
    k> programmer, I thought the declaration was ok through autovivification.
    k> Could someone please explain what I am missing and why it works under
    k> Perl 5.6.1?

    if you want that construct, then use 5.6.1.

    i would stick with:

    my $a = [ split(/\s+/,$string) ] ;

    which does the same thing and works in all perl5 versions

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
    Uri Guttman, Aug 27, 2003
    #2
    1. Advertising

  3. Also sprach Uri Guttman:

    >>>>>> "k" == ko <> writes:

    >
    > k> Quick question. The code snippet works under the following builds:
    > k> v5.6.1 built for MSWin32-x86-multi-thread (Activestate 635)
    > k> v5.6.1 built for i386-freebsd (pkg_info = perl-5.6.1_11)
    >
    > k> my @$ar = split(/\s+/,$string);
    >
    > k> But if run under Activestate 804 or 5.005_03 for i386-freebsd (taking
    > k> out 'use warnings, and replacing with #!/usr/bin/perl -w), I get the
    > k> following error message:
    >
    > k> 'Can't declare array dereferences in my at...'
    >
    > it has nothing to do with build numbers but perl versions. i tried that
    > on 5.004_04, 5.005_02, 5.005_03 and 5.6.1. it works only on 5.6.1 so
    > they probably added the ability to autovivify and my declare in one
    > statement.


    Interesting. I didn't know that you can do this in 5.6.1. Looks a little
    like an accidental feature to me, particularly since it is no longer
    there in 5.8.x. Also reminds me of a discussion recently held among the
    p5porters whether

    my @hash{ qw/s l i c e/ } = ();

    should be allowed (I think the consensus eventually being that it's not
    a good idea).

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval, Aug 27, 2003
    #3
  4. ko

    ko Guest

    Uri Guttman wrote:

    > i would stick with:
    >
    > my $a = [ split(/\s+/,$string) ] ;
    >
    > which does the same thing and works in all perl5 versions


    Thank you. That's much more readable than what I originally had.

    Unfortunately, being the novice programmer, I sometimes make mistakes
    with context and therefore end up creating intermediate variable(s) -
    tends to make me see the correct context better for some reason. My
    first thought to fix the problem was:

    my @a = split(/\s+/,$string);
    my $ar = \@a;
    some_sub($ar,@other_args);

    And other times it just plain escapes me to directly use return values.
    I'm still at a point where it takes me three or four steps to accomplish
    a task that could be done in one or two steps :(
    ko, Aug 27, 2003
    #4
    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. Howard
    Replies:
    0
    Views:
    375
    Howard
    Jul 1, 2003
  2. Srinu

    Two dimentional array dereference in C

    Srinu, Nov 19, 2007, in forum: C Programming
    Replies:
    10
    Views:
    958
    Barry Schwarz
    Nov 21, 2007
  3. Tomás Ó hÉilidhe

    Dereference an array pointer... UB?

    Tomás Ó hÉilidhe, Feb 11, 2008, in forum: C Programming
    Replies:
    33
    Views:
    937
    Chris Torek
    Feb 28, 2008
  4. David McNerney
    Replies:
    5
    Views:
    125
    David McNerney
    May 5, 2006
  5. craigandjeanne

    dereference an array problem

    craigandjeanne, Jun 1, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    108
    Matt Garrish
    Jun 1, 2006
Loading...

Share This Page