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

Hi perl monks, newbie here so please be gentle.... been working with perl for a few years so not a total novice. Slightly above that ;)

I think I've figured out the solution but just wanted to bounce it off the knowledgeable monks.

Problem: I'm using nested macros and the string I'm passing to one macro is then used as an input to another. The challenge I believe I'm facing is that the double quotes are stripped at the first level and therefore at the second level the input is basically truncated at the first space.

e.g. macro1 "This is the input string to macro 2" macro2 then uses this string to call another and sends macro2 This is the input string to macro 2... therefore realistically whats passed is "macro2 This". What I believe is the solution is to either double double quote of qq. e.g. macro1 qq"This is the input string to macro 2" or macro1 "\"This is the input string to macro 2\"" Anyone else overcome similar issues to this? Thanks monks
  • Comment on Double double quoting - nested macro calls

Replies are listed 'Best First'.
Re: Double double quoting - nested macro calls
by AnomalousMonk (Archbishop) on Apr 22, 2011 at 22:10 UTC

    If you think you have a solution, can you give a concrete example of it in operation – some short, standalone, runnable code? From your description, I cannot envision what your solution might be.

    Approaching from another angle, what you describe sounds a bit like a text templating system, so maybe take a look at Text::Template.

      Thanks for the reply guys... haven't got code examples to hand I'm afraid. Something I was trying to get sorted in work today and finished for the weekend.

      Basically a hierarchy of macros:
      1) run a command on a remote machine & validate output etc.
      2) Under the covers this calls another macro that rsh's and calls a small little standalone perl program to do what we want on the remote machine.
      3) This standalone perl program then adds a string to a file.

      So at each level a set of double quotes in essence is removed... so I suspect I need to pad out with "extras" to allow for the hierarchy?

      By the time we get to the standalone perl program its essentially getting a

      print This is the test string

      rather than

      print "This is the test string"

        ... By the time we get to the standalone perl program its essentially getting a ...

        I think you misunderstand, see How do I post a question effectively?

        For a recent example of this principal in action see the programs posted in Relative URI, they're small, compile and run, and they reproduce the problem ; they show sample input, desired output, actual (and erroneous) output.

        Hi :)

        I am not a Unix GURU, so some of the usage for rsh that I have below is probably jacked up, but Sounds like this is the scenario to me.

        $string = "This is my string"; my $output = `rsh -l remoteuser host.example.com "perlprog.pl $string" +`;

        With the above code, $string is going to equal This is my string not "This is my string". Therefore when rsh is called as an external application, the resulting command on the remote system is perlprog.pl This is my string instead of the desired perlprog.pl "This is my string"

        Seems like you should be able to add the double quotes to the parameter by escaping them. Similar to the following.

        $string = "This is my string"; my $output = `rsh -l remoteuser host.example.com "perlprog.pl \"$strin +g\""`;
Re: Double double quoting - nested macro calls
by wind (Priest) on Apr 22, 2011 at 22:01 UTC
    You're first solution is exactly the same as your original quoted string.
    qq"This is the input string to macro 2"
    should be:
    qq{"This is the input string to macro 2"}
Re: Double double quoting - nested macro calls
by John M. Dlugosz (Monsignor) on Apr 23, 2011 at 00:51 UTC
    What is a macro? Perl doesn't have macros. Can you show what you're actually doing?