Hi.

I did a double take on this one, because at first it wasn't at all clear to me what was happening. I think that most of this confusion was because you called "" (double quotes) shell escapes. What you are referring to is not "shell escaping", it is "double quote interpolation".

Anyway, the problem you are having seems to be related to Perl's DWIM ("Do What I Mean") attitude to string interpolation, and it auto-vivifying arrays when you don't want them. In your example code, you are auto-vivifying two arrays: @a and @c. If you want to know more details, I suggest inserting the following at the top of your script:

use strict; use warnings; use diagnostics;

Fortunately, there is a very simple solution - if you don't want Perl to treat square brackets ("[" and "]") as array subscripts, then escape them with a backslash ("\") in your strings.

$a = 'name'; $b = 5; my $c = 'name'; my $d = 5; print "$a\[$b\]" eq "$c\[$d\]" ? "ok\n" : "booger\n";

(As an aside, you should really try to avoid $a and $b as variable names. Whilst I'm sure they were used just as an illustration, these are reserved variables which are used in the 'sort' built-in function. Although "use strict" won't complain about them, there is a good reason for this: they are always package global symbols. Moral: always use 'my' and stay away from $a and $b.)

I hope that helps.

Cheers,

-- Dave :-)


$q=[split+qr,,,q,~swmi,.$,],+s.$.Em~w^,,.,s,.,$&&$$q[pos],eg,print

In reply to Re: variable scope affects variables in crace shell escapes by DaveH
in thread variable scope affects variables in crace shell escapes by colink

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.