Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is it possible to get the unexpanded name of a macro from the program input?

    Is it possible to write a program which sees and can store the underlying name of an unexpanded macro without any additional syntax on the macro itself?

    By way of example you would have a program which was invoked as

    Code:
    local mymacro macro_value
    myprog `mymacro'
    where the program could see and store the name "mymacro" as well as it's underlying value "macro_value" at `1' ? I know it's probably improper, but it would be nice to do for a little internal workflow program I have.

  • #2
    The trace output from the code below suggests that the Stata interpreter resolves the local macro before passing the resolved value to the called program. This is consistent with local macros being truly local to the scope within which they are defined - no downward inheritance.
    Code:
    capture program drop myprog
    program define myprog
    display "hello, world"
    end
    
    capture program drop gnxl
    program define gnxl
    local mymacro macro_value
    myprog `mymacro'
    end
    
    set trace on
    gnxl
    Code:
    . set trace on
    
    . gnxl
      ------------------------------------------------------- begin gnxl ---
      - local mymacro macro_value
      - myprog `mymacro'
      = myprog macro_value
        --------------------------------------------------- begin myprog ---
        - display "hello, world"
    hello, world
        ----------------------------------------------------- end myprog ---
      --------------------------------------------------------- end gnxl ---

    Comment


    • #3
      As William points out, once you supply a macro reference as argument, the program sees the contents of the macro but the name is lost. Conversely if you pass the name, then that's useless to the program because it can't see stuff in the calling program. That's what local means.

      I'd back up one level and tell us more on what you want to do.

      Comment


      • #4
        I casually looked at the trace too, and that's more or less what I expected. My idea is a little unconventional. I wanted to build a small wrapper for save which would be able to create a copy of tempfiles in another directory under the name of the tempfile macro and name it "xsave" or something. In this way, I could write and debug programs and create occasional saves where I wanted them that made the files available if I wanted to restart from the middle. Then when I was satisfied, I could just find-replace xsave to save throughout the program. It's perfectly possible to make the copy some other filename or to give it the same filename as the tempfile macro, it would just make the debug restart slightly easier if it used the macro name itself.

        This is a pure debugging workflow thing. It's not a big deal.

        Comment

        Working...
        X