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

In my time of PL/1 programming, I liked the PUT DATA statement during debugging: It outputs the name of a variable, together with its value:

x=5; y=7; PUT DATA (x,y);
outputs
x=5 y=7
I wonder whether something similar can be implemented in Perl. Of course the naive approach
use Data::Dumper; sub show { local $Data::Dumper::Terse=1; local $SHOW::expr=shift; local $SHOW::val=eval { $expr }; $SHOW::expr . ($@ ? " throws exception: $@" : ('='.Dumper($SHOW::expr))); }
fails if $expr contains my variables from the calling context. I guess a useful implementation would have been to interfere somehow with the compiler (in a similar way the debugger is behaving). Could someone give me some pointer on how this could be implemented? Or maybe is there already some module on CPAN which I could use?

-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re: PL/1's PUT DATA in Perl - is this possible?
by BrowserUk (Patriarch) on Nov 07, 2008 at 09:59 UTC

    You might like to look at Smart::Comments. With that, you place the variables of interest into a comment card and it gets traced.

    The nice thing is that when your done debugging, you comment out the use Smart::Comments line and the trace output stops, but stays there for next time.

    It is a source filter which are generally frowned upon, but as you disable it for production, the problems associated with source filters go away.

    It's only downside (IMO) is that it's output is too verbose, producing multipe short lines for each trace line, but that can be fixed if it annoys you too.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      You can also just use the comments and never even have a use Smart::Comments line in the file at all or worry about removing/commenting it out for production. Just run with a wrapper which calls perl appropriately when you want them turned on (zsh function; adjust accordingly for other inferior shells . . . :):

      scom () { local level="" if [[ $1 = (([#]##),#)## ]] then level="=$1" shift fi perl -MSmart::Comments${level} "$@" }

      By default it runs with the normal "###" level, but you can call it as scom '###,####,#####' foo.plx to enable more levels as necessary.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        Adjusted for inferior shell:

        #!/usr/bin/sh perl -e'$l=shift if $ARGV[0] =~ /#{3,}/; exec $^X, "-MSmart::Comments=$l", @ARGV; die $! '
        I use a bunch of aliases:
        alias smart='perl -MSmart::Comments' alias smart3='perl -MSmart::Comments="###"' alias smart34='perl -MSmart::Comments="###","####"' alias smart4='perl -MSmart::Comments="####"' ...
Re: PL/1's PUT DATA in Perl - is this possible?
by ikegami (Patriarch) on Nov 07, 2008 at 09:19 UTC

      Data::Dump::Streamer looks promising, but I wonder whether there is also something I could use which is written in pure Perl. The README of Data::Dump::Streamer says

      The modules requires a functional C compiler, however PPM support for Win32 users will also be available sometime soon.
      My application should ideally be able to be "packaged in a box" (together with all the modules needed) and run on any platform having Perl 5.7 or higher. If I have modules with a compiled part, I would have to compile them for any possible target system in advance, which I would like to avoid.

      -- 
      Ronald Fischer <ynnor@mm.st>

        See the Useperl attribute in Data::Dumper - that makes it use pure perl version.

Re: PL/1's PUT DATA in Perl - is this possible?
by Anonymous Monk on Nov 07, 2008 at 09:21 UTC