http://www.perlmonks.org?node_id=235013

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

I have a slight problem. I have some code that is being run inside mason and I have a preprocessing step that transforms strings in mason pages that look like:

  "<I18N id='1007'>I have <I18NARG pos='1'>$foo</I18NARG>KB left to transfer</I18N>"

These get processed into the language appropriate string before the compliation step. So what perl sees (for the default US-english locale) is:

  "I have $fooKB left to transfer"

Which causes a perl compilation error (because of use strict. To fix this particular instance of the problem I can say:

...<I18NARG pos='1'>${foo}</I18NARG>...

But that is not really clean. I would like the translating code to be able to insert a token to protect the variables automatically. The following works, but requires an extra variable interpolation:

my $EMPTY = ""; ... "$foo${EMPTY}KB"

Can anyone thing of some way to insert something into the string that will not actually get inserted into the string yet will cause a variable name to terminate?

Thanks,

-ben

Replies are listed 'Best First'.
Re: Distinguishing variables from surrounding text
by broquaint (Abbot) on Feb 13, 2003 at 16:37 UTC
    You were very close - you need to put brackets around the variable to separate it from the surrounding string e.g
    my $foo = 12345; print "I have ${foo}KB left to transfer\n"; __output__ I have 12345KB left to transfer

    HTH

    _________
    broquaint

Re: Distinguishing variables from surrounding text
by jmcnamara (Monsignor) on Feb 13, 2003 at 16:41 UTC

    I really don't see anything wrong with ${foo}.

    Nevertheless, you could use $, if you haven't set it:

    "$foo$,KB"

    Update: actually that won't work with -w, which is probably just as well.

    --
    John.

Re: Distinguishing variables from surrounding text
by Fletch (Bishop) on Feb 13, 2003 at 16:53 UTC

    Erm, I don't see why you say that using brackets to disambiguate isn't `clean', since that's what it's for (see perldoc perldata, Scalar value constructors). I mean if you really want unclean, use @{[ $foo ]} (but that's just ugly for ugly's sake; not to mention doing strange things to tied vars which pay attention to context).

      Sorry. To clarify, the reason that I don't like saying ${foo} has nothing to do with that syntax. It is in general fine. However for the most part I don't use that syntax in strings. I prefer to say $foo unless I see that the next character is going to cause problems. However in my given case I don't immediately see that the next character will be a problem because it is the leading < which should be fine. Except that I am rewriting the code deeper down. My question really is how can I make the rewriting safer by making sure that there is something safe where the tag used to be so that what the programmer expects to happen actually happens.

      -ben

        How about
        "."
        That's close, concat, open.