String parsed wrong by perl

Discussion in 'Perl Misc' started by A. Farber, Mar 21, 2009.

  1. A. Farber

    A. Farber Guest

    Hi,

    I've got a string in my bigger program that parses wrong.
    I've prepared a one-liner which shows the problem:

    $ perl -e 'print qq{[color=#FF0000:$self->{BBCODE}]$card->{HTML}[/
    color:$self->{BBCODE}]}'
    Search pattern not terminated at -e line 1.

    $ perl -v
    This is perl, v5.8.8 built for i386-openbsd

    As a workaround I have to split the string:

    $ perl -e 'print qq{[color=#FF0000:$self->{BBCODE}]$card->{HTML}} . qq
    {[/color:$self->{BBCODE}]}'
    [color=#FF0000:][/color:]

    Any comments? I haven't seen this happen often

    Regards
    Alex
     
    A. Farber, Mar 21, 2009
    #1
    1. Advertising

  2. "A. Farber" <> wrote in
    news::

    > I've got a string in my bigger program that parses wrong.
    > I've prepared a one-liner which shows the problem:
    >
    > $ perl -e 'print qq{[color=#FF0000:$self->{BBCODE}]$card->{HTML}[/
    > color:$self->{BBCODE}]}'
    > Search pattern not terminated at -e line 1.


    Can you explain, in words, what should be printed by the statement
    above?

    > Any comments?


    This is not the way to interpolate elements pointed to by hash
    references into strings.

    > I haven't seen this happen often


    I don't know what *this* is but I am indeed saddened to see code like
    above and I do hope it is rare.

    (s)sprintf would help.

    So would a proper templating system.

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
     
    A. Sinan Unur, Mar 21, 2009
    #2
    1. Advertising

  3. A. Farber

    A. Farber Guest

    On 21 Mrz., 14:04, "A. Sinan Unur" <> wrote:
    > "A. Farber" <> wrote innews::
    > This is not the way to interpolate elements pointed to by hash
    > references into strings.


    $ perl -e '$href={a,1}; print "$href->{a}\n";'
    1

    $ perl -v

    This is perl, v5.8.8 built for i386-openbsd

    Regards
    Alex
     
    A. Farber, Mar 21, 2009
    #3
  4. A. Farber <> wrote:
    > I've got a string in my bigger program that parses wrong.
    > I've prepared a one-liner which shows the problem:


    > $ perl -e 'print qq{[color=#FF0000:$self->{BBCODE}]$card->{HTML}[/color:$self->{BBCODE}]}'
    > Search pattern not terminated at -e line 1.


    What do you expect? With 'qq' you tell that the string is to be
    interpolated. Then you have somewhere in the string

    $card->{HTML}[/color:$self->{BBCODE}]

    Now, apart from not having a hash reference called '$card' anywhere
    in scope you try to tread

    $card->{HTML}

    as an array (reference), due to the following '['. And as the array
    index you have

    /color:$self->{BBCODE}

    which only makes sense as a regexp, due to the leading '/'.
    But since there's no trailing '/' you get told that the
    search pattern is not terminated.

    > $ perl -v
    > This is perl, v5.8.8 built for i386-openbsd


    > As a workaround I have to split the string:


    > $ perl -e 'print qq{[color=#FF0000:$self->{BBCODE}]$card->{HTML}} . qq
    > {[/color:$self->{BBCODE}]}'
    > [color=#FF0000:][/color:]


    That works because you avoid '$card->{HTML}' being seen as an
    array (reference).

    Another way around that would be to write

    $ perl -e 'print qq{[color=#FF0000:$self->{BBCODE}]$card->{HTML}\[/color:$self->{BBCODE}]}'

    since then the '[' following '$card->{HTML}' can't be mistaken
    to be an access of an array element.

    But, please, why do would use such a horrible mess at all?

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Mar 21, 2009
    #4
  5. "A. Farber" <> writes:

    > Hi,
    >
    > I've got a string in my bigger program that parses wrong.
    > I've prepared a one-liner which shows the problem:
    >
    > $ perl -e 'print qq{[color=#FF0000:$self->{BBCODE}]$card->{HTML}[/
    > color:$self->{BBCODE}]}'
    > Search pattern not terminated at -e line 1.


    What happes is that perl assumes that you want to access $card as it
    is an hash of arrays (or at least that $card->{HTML} is an arrayref).

    To do this it has to parse '/color:$self->{BBCODE}' as an expression
    into this array. And this is clearly an unterminated search pattern.

    //Makholm
     
    Peter Makholm, Mar 21, 2009
    #5
  6. A. Farber <> wrote:
    > On 21 Mrz., 14:04, "A. Sinan Unur" <> wrote:
    > > "A. Farber" <> wrote innews::
    > > This is not the way to interpolate elements pointed to by hash
    > > references into strings.


    > $ perl -e '$href={a,1}; print "$href->{a}\n";'
    > 1


    What do you mean by that? The result looks pretty fine and
    to be expected. Don't you think so? And in your original post
    you where using something that looked more like this

    $ perl -e '$href={a,(1,2,3)}; print "$href->{a}[0]\n";'
    1

    but with some invalid expression instead of '0' as the array
    index (and $href never defined).

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Mar 21, 2009
    #6
  7. A. Farber

    A. Farber Guest

    Hello,

    On 21 Mrz., 15:18, (Jens Thoms Toerring) wrote:
    > A. Farber <> wrote:
    > > $ perl -e 'print qq{[color=#FF0000:$self->{BBCODE}]$card->{HTML}[/color:$self->{BBCODE}]}'
    > > Search pattern not terminated at -e line 1.

    >
    > What do you expect? With 'qq' you tell that the string is to be
    > interpolated. Then you have somewhere in the string
    >
    > $card->{HTML}[/color:$self->{BBCODE}]
    >
    > Now, apart from not having a hash reference called '$card' anywhere
    > in scope you try to tread


    Yes it's missing in the oneliner, but I obviously have
    the $card in scope in my program, so this doesn't matter.

    > $card->{HTML}
    >
    > as an array (reference), due to the following '['. And as the array
    > index you have


    Yes, that is it - thanks for explanation.

    > But, please, why do would use such a horrible mess at all?


    I'm surprised why people jump at my code,
    I don't think it's that horrible.

    I'm just trying to list cards of red and black
    suits in a post of a phpBB3-forum, like here:
    http://preferans.de/viewtopic.php?f=5&t=9

    If phpBB3 has decided to code font colors by
    [color=#FF0000:random]blah[/color:random]
    then it isn't my fault, isn't it?

    And the suggested usage of sprintf wouldn't help anything.
    For me "$href->{x}: $href->{y}, $href->{z}"
    is not less readable than
    sprintf "%s: %s, %s", $href->{x}, $href->{y}, $href->{z};

    Regards
    Alex
     
    A. Farber, Mar 21, 2009
    #7
  8. A. Farber

    A. Farber Guest

    On 21 Mrz., 15:25, (Jens Thoms Toerring) wrote:
    > A. Farber <> wrote:
    > > $ perl -e '$href={a,1}; print "$href->{a}\n";'
    > > 1

    >
    > What do you mean by that?


    I mean by that that "A. Sinan Unur" is wrong when saying
    "This is not the way to interpolate elements pointed to by hash
    references into strings."

    Regards
    Alex
     
    A. Farber, Mar 21, 2009
    #8
  9. A. Farber

    A. Farber Guest

    Hello Peter,

    On 21 Mrz., 15:23, Peter Makholm <> wrote:
    > > $ perl -e 'print qq{[color=#FF0000:$self->{BBCODE}]$card->{HTML}[/
    > > color:$self->{BBCODE}]}'
    > > Search pattern not terminated at -e line 1.

    >
    > What happens is that perl assumes that you want to access $card as it
    > is an hash of arrays (or at least that $card->{HTML} is an arrayref).
    >
    > To do this it has to parse '/color:$self->{BBCODE}' as an expression
    > into this array. And this is clearly an unterminated search pattern.


    yes, thanks. Now I understand it was parsing
    it as an array index (I missed that before).

    I'm just still a bit surprised, that perl tries
    to parse a regex from inside of a string...
    It never parses "xxx/blah/xxx" doesn't it?

    Regards
    Alex
     
    A. Farber, Mar 21, 2009
    #9
  10. A. Farber <> wrote:
    > On 21 Mrz., 15:18, (Jens Thoms Toerring) wrote:
    >> A. Farber <> wrote:


    >> > $ perl -e 'print qq{[color=#FF0000:$self->{BBCODE}]$card->{HTML}[/color:$self->{BBCODE}]}'



    >> But, please, why do would use such a horrible mess at all?

    >
    > I'm surprised why people jump at my code,
    > I don't think it's that horrible.



    It is horrible for maintenance.

    When debugging code you are often looking for uses of variables.

    Having uses of variables embedded between loads of (necessarily)
    homely data makes them harder to find.


    > If phpBB3 has decided to code font colors by
    > [color=#FF0000:random]blah[/color:random]
    > then it isn't my fault, isn't it?



    Of course not, but that is the data.

    The complaints are about the code, not about the data.

    The ugliness of the data cannot be controlled by the programmer,
    the ugliness of the code can be.


    > And the suggested usage of sprintf wouldn't help anything.
    > For me "$href->{x}: $href->{y}, $href->{z}"

    ^^ ^^
    ^^ ^^
    > is not less readable than
    > sprintf "%s: %s, %s", $href->{x}, $href->{y}, $href->{z};



    That is because there are only 4 characters of data in your
    contrived example, while there are 25 characters of data
    in your real code.

    [color=#FF0000:%s]%s[/color:%s]
    ^^^^^^^^^^^^^^^ ^ ^^^^^^^^ ^
    ^^^^^^^^^^^^^^^ ^ ^^^^^^^^ ^

    Finding a needle next to a stalk of hay is easier than finding
    a needle in a haystack. :)

    printf makes it easy to see the variables that are used in the statement:

    printf '[color=#FF0000:%s]%s[/color:%s]',
    $self->{BBCODE}, $card->{HTML}, $self->{BBCODE};


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Mar 21, 2009
    #10
  11. A. Farber <> wrote:
    > Hello Peter,
    >
    > On 21 Mrz., 15:23, Peter Makholm <> wrote:
    >> > $ perl -e 'print qq{[color=#FF0000:$self->{BBCODE}]$card->{HTML}[/
    >> > color:$self->{BBCODE}]}'
    >> > Search pattern not terminated at -e line 1.

    >>
    >> What happens is that perl assumes that you want to access $card as it
    >> is an hash of arrays (or at least that $card->{HTML} is an arrayref).
    >>
    >> To do this it has to parse '/color:$self->{BBCODE}' as an expression
    >> into this array. And this is clearly an unterminated search pattern.

    >
    > yes, thanks. Now I understand it was parsing
    > it as an array index (I missed that before).
    >
    > I'm just still a bit surprised, that perl tries

    ^^^^^^^^^^^^^^^

    Note that if you had used printf(), you would not have been surprised.

    The surprise results from interpolation.

    If there is no interpolation, there is no surprise!


    > to parse a regex from inside of a string...



    It doesn't.

    It tries to parse expressions that appear in subscripts though.

    $_ = $some_array[/color:$self->{BBCODE}];

    has the same syntax error as

    $_ = "$some_array[/color:$self->{BBCODE}]";

    does.

    Would you be surprised by a syntax error in that first one? Probably not.


    > It never parses "xxx/blah/xxx" doesn't it?



    No, because that is merely a string, there are no subscripts that
    need to be evaluated in order to complete interpolation.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Mar 21, 2009
    #11
  12. "A. Farber" <> wrote in news:04bb5eff-d4a1-
    :

    > On 21 Mrz., 15:25, (Jens Thoms Toerring) wrote:
    >> A. Farber <> wrote:
    >> > $ perl -e '$href={a,1}; print "$href->{a}\n";'
    >> > 1

    >>
    >> What do you mean by that?

    >
    > I mean by that that "A. Sinan Unur" is wrong when saying
    > "This is not the way to interpolate elements pointed to by hash
    > references into strings."


    You are missing the point.

    I did not say you cannot interpolate this way.

    While your simple example clearly works, things get very hairy very
    quickly as your question shows.

    You can fit a square peg in a round hole but why do it?

    Use (s)printf or a proper templating system.

    Sinan
    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
     
    A. Sinan Unur, Mar 21, 2009
    #12
  13. A. Farber

    A. Farber Guest

    On 21 Mrz., 18:09, Tad J McClellan <> wrote:
    > Finding a needle next to a stalk of hay is easier than finding
    > a needle in a haystack.  :)
    >
    > printf makes it easy to see the variables that are used in the statement:
    >
    >     printf '[color=#FF0000:%s]%s[/color:%s]',
    >            $self->{BBCODE}, $card->{HTML}, $self->{BBCODE};
    >


    :) Ok, maybe sprintf is better

    Thanks
     
    A. Farber, Mar 21, 2009
    #13
  14. On 2009-03-21, A. Farber <> wrote:
    > I'm just still a bit surprised, that perl tries
    > to parse a regex from inside of a string...


    It is not "inside of string". Some parts of double-quoted regions are
    STRINGS, some are CODE. E.g.

    >perl -wle "qq(abc) =~ /(.)(.)(.)/; print qq(${1+2})"

    c

    Likewise for $a
    Code:
    , $a{CODE}, and @a[CODE], @a{CODE}; modify for ->
    accordingly...  One can avoid this by backwacking $, {, [, or -.
    
    Hope this helps,
    Ilya
     
    Ilya Zakharevich, Mar 21, 2009
    #14
  15. A. Farber

    Guest

    On Sat, 21 Mar 2009 20:19:33 GMT, Ilya Zakharevich <> wrote:

    >On 2009-03-21, A. Farber <> wrote:
    >> I'm just still a bit surprised, that perl tries
    >> to parse a regex from inside of a string...

    >
    >It is not "inside of string". Some parts of double-quoted regions are
    >STRINGS, some are CODE. E.g.
    >
    > >perl -wle "qq(abc) =~ /(.)(.)(.)/; print qq(${1+2})"

    > c
    >
    >Likewise for $a
    Code:
    , $a{CODE}, and @a[CODE], @a{CODE}; modify for ->
    >accordingly...  One can avoid this by backwacking $, {, [, or -.[/color]
    ^^^^^^^^^^^[color=blue]
    >
    >Hope this helps,
    >Ilya[/color]
    
    Whoa, now there's a technical term "backwacking".
    What the hell does that mean?
    
    Is that like itchy finger hoosamacalla?
    
    Unfortunately, I've seen this term before but never, ever, would would give
    creadence to its existence as legitimate phrase used to technically describe
    anything, nor validate it's legitimacy.
    
    -sln
     
    , Mar 22, 2009
    #15
    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. Guest
    Replies:
    1
    Views:
    566
    darrel
    Mar 18, 2005
  2. Achim Domma (SyynX Solutions GmbH)

    When/How often ist Web.config parsed

    Achim Domma (SyynX Solutions GmbH), Aug 30, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    522
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Aug 30, 2005
  3. YuliaG
    Replies:
    2
    Views:
    463
    Arnaud Berger
    Apr 4, 2005
  4. Steven Bethard
    Replies:
    7
    Views:
    469
    Steven Bethard
    Sep 13, 2005
  5. SpreadTooThin

    get document as a string to be parsed by json

    SpreadTooThin, Nov 24, 2011, in forum: Javascript
    Replies:
    0
    Views:
    543
    SpreadTooThin
    Nov 24, 2011
Loading...

Share This Page