in reply to Re: Command line question
in thread Command line question

I'm guessing that ${SOURCE} and friends are being interpolated somehow (even though they're in single quotes)

Those quotes are processed by the shell. In the bourne shell or a derivative like bash, single quote don't perform any interpolation.

$ echo "${PATH}" .:/home/ikegami/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/gam +es $ echo '${PATH}' ${PATH}

( Here on end, assuming the bourne shell or a derivative like bash )

So Perl sees

BEGIN { $^I = ''; } while (<>) { s+${SOURCE}/${SAMS}/++g; s+${SOURCE}/${SAMLIB}/++g; s+${SOURCE}/${SMCLIB}/+${SOURCE}/${KENO}/+g; if ( not m+^${SOURCE}/+ ) { s+${SOURCE}/+\$$\{OBJECT\}/+g; print } }

On the other hand, Perl regexp and replace expressions *do* interpolate. So the above is equivalent to

BEGIN { $^I = ''; } while (<>) { s+//++g; s+//++g; s+//+//+g; if ( not m+^/+ ) { s+/+\${OBJECT}/+g; print } }

Depending on what the OP wants to do, he needs to use -e"" instead of -e'', to escape the dollar signs, or to define those variables in Perl.

Replies are listed 'Best First'.
Re^3: Command line question
by parv (Parson) on Jun 18, 2008 at 00:48 UTC
    My guess is that the code snippet is from a make file (while we are guessing).
Re^3: Command line question
by kyle (Abbot) on Jun 18, 2008 at 00:34 UTC

    Ordinarily, I'd agree with you, but I get the impression that the OP's code works as-is, and he's just trying to figure out what it does and how. My guess is that the whole thing is in double quotes, and what perl actually sees is:

    BEGIN { $^I = ""; } LINE: while (defined($_ = <ARGV>)) { s[value-of-SOURCE/value-of-SAMS/][]g; s[value-of-SOURCE/value-of-SAMLIB/][]g; s[value-of-SOURCE/value-of-SMCLIB/][value-of-SOURCE/value-of-KENO/ +]g; if (not m[^value-of-SOURCE/]) { s[value-of-SOURCE/][$${OBJECT}/]g; print $_; } }

    That $${OBJECT} in there still seems wrong, though, and the OP is ambiguous about whether this is working code or not, so I'm pretty far from sure about this.

      Yeah, you're dead on. This line is from a Makefile from an old code. This line has been stumping us a bit, and I'm the only one that knows Perl (and I don't know it that well), so I got lost trying to search this through as so many of the delimiters were odd to me (the s+ and ++g etc...).

      So this is working code that does what it's supposed to, but I'm having trouble totally seeing what it does. I'd like to actually get the .depend file before the Perl script, then again after it to compare ... it may shed some light for me.

      So what you're thinking is that each instance of ${something} is actually some value, stored in ${something} being passed in to the Perl script? Is that roughly correct?

      Finally, could the $${OBJECT} be right? Could it be substituting the value of ${OBJECT}, with a preceding "$" sign into the code, to be read and interpreted by the shell?

      I may be way off and talking over my head too, if it doesn't make sense, don't try to make it as it's probably wrong.

      Thanks!

      ~Jack

        I'd like to actually get the .depend file before the Perl script, then again after it to compare

        No problem! Change "perl -ni -e" to "perl -ni.bak -e", and you'll find a ".depend.bak" file that has its contents before perl got a hold of it. If there's already a .bak file being produced, you can use "perl -ni.before.tha.mangalang -e" or whatever.

        So what you're thinking is that each instance of ${something} is actually some value, stored in ${something} being passed in to the Perl script? Is that roughly correct?

        That's roughly correct, yes. Perl actually never sees them as variables. The interpolation is done by the shell (or make) before Perl comes into it. From its point of view, they're literal strings.

        Finally, could the $${OBJECT} be right?

        I don't think so. If that's really what perl sees, it's going to come out as an empty string because within the context of the Perl that's running, there is no $OBJECT variable with a value. I think that it's actually coming out as "\${OBJECT}", and winds up in the .depend file that way. This is one part of this whole thing I'm really not sure about, so take my words with salt, please.