in reply to Syntax for Slice of dereferenced array

Hmm, playing around with some simpler cases, I think part of the problem was my use of x, y, and z as hash keys. The statement:my ($x,$y,$s)= @{$self}{x,y,s}; really bamboozled the compiler, and even when I commented out the rest of the function, I got errors inside the comments! The parser was messing up everything once it started on this line.

Even though I'm using barewords as the index of a hash, it's treating the s and y as quote-like matching operators, and all you-know-what breaks loose.

—John

Replies are listed 'Best First'.
(MeowChow) Re2: Syntax for Slice of dereferenced array
by MeowChow (Vicar) on Jul 22, 2001 at 04:11 UTC
    Try
      
    ($x,$y,$s)= @{$self}{'x,y,s'};
    Unless your key is of the form /\w+/, Perl will interpret it as code returning a list of keys.

    update: <CajunMan>Confusion</CajunMan> ... didn't understand what you were trying to do - if you want trouble-free barewords, do it like so:
      
    ($x,$y,$s)= @{$self}{x=>y=>s=>};
    Of course, the form you have it in now makes more sense, unless you're obfuscating.

       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
      Huh? Isn't 'x,y,s' a single key? I'm using 'x','y','s' now.

      But, basically the bareword thing doesn't work on slices, right?

        Mostly. Perl only gives special treatment to single barewords (with optional white space) used as hash keys (inside $h{here} or before =>). @h{x,y,z} isn't using a single bareword so it doesn't get special treatment so it gets parsed as "x",tr,z and then goes looking for two more commas to finish off the tr.

        Note that "x" is also a Perl keyword but can still be used as a bareword here since "x" is only a binary operator and there is no left-hand side in front of "x" so Perl won't treat it as the operator.

        However, you can write a slice and give it a single key, @h{y}, and the special bareword rules apply there. It isn't the "slice" that breaks the rule, it is having something more than a single bareword (+whitespace).

                - tye (but my friends call me "Tye")