Re: getting {string} from \${string}

From: Anno Siegel (anno4000_at_lublin.zrz.tu-berlin.de)
Date: 01/29/05


Date: 29 Jan 2005 01:54:03 GMT


 <bongo@frii.com> wrote in comp.lang.perl.misc:
> Hi folks. Perl newbie here. Having a heck of a
> time with something which probably isn't that hard...
>
> When debugging code in progress, I often find myself
> typing:
>
> print "\$var1=$var1, \$var2=$var2 \n";
>
> not a big deal, but when you do it all the time, it
> would be nice to save some typing by putting it in a
> subroutine, such as:
>
> dbg_pr($var1,$var2);
>
> Easy enough if I just wanted the values, but I want to
> print both the variable names and the values, and I'd
> like to get both from one string. If I pass $var1 or \$var1
> I can get the value but not the variable name, and if I
> pass "var1" I get the name but not the value
> (as ${"var1"} isn't defined in the subroutine name space.)
>
> It doesn't seem like I can pass a string and construct a
> reference from it. Can I somehow stringify \${var1} to
> get "var1" instead of SCALAR(Hhex address)?

No, not without Devel::Peek.

> Any help appreciated. It seems as if this has to be simple,
> but the best I can currently do is pass both the string and
> the reference, which is almost as redundant as just retyping
> the whole print statement every time.

It's not trivial. You can use "eval" to get the value of an expression
given as a string (including simple variables). Then you can print
the literal expression and the value. Define two routines:

    sub showval {
        my ( $name, $value) = @_;
        print defined $value ? "$name = $value\n" : "$name -undef-\n";
    }

    sub show {
        join '; ', map "showval( '$_', $_)", @_;
    }

(If you put them in a module, import both show() and showval().)

In a program, you may have:

    my $x = 3;
    my $y;
    my %z = ( aaa => 123, bbb => 456 );

To see some of the values, say

    eval show qw( $x $y $z{bbb} $z{gibsnich});

That prints

    $x = 3
    $y -undef-
    $z{bbb} = 456
    $z{gibsnich} -undef-

Close enough? Myself, I have it somewhere but never use it.

Anno



Relevant Pages

  • Re: Complex Specified Information - Pitman Formula
    ... Therefore a significant match between a reference and a test ... string is good evidence of non-random production. ... and there are no finite algorithms to compute their digits. ... probabilities of the different symbols the information source can produce. ...
    (talk.origins)
  • Re: String Reference Type
    ... All unary and binary operators have predefined implementations that are ... Therefore its always allocated in the heap and a variable of string ... As with all classes in this case y and x both reference the same String ... language depandant matter as below. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Abstract class variables question
    ... But as I think you've seen elsewhere in this thread, a value type can exist inside a class and in that case the value type is stored in the heap with the rest of the class instance. ... But as far as the "faster" goes, yes...to some extent value types have less overhead than reference types, and so can perform better in certain cases. ... Well, that would be true for a string object too, if there was any way to actually change a string. ... Seriously though, it is practically always the case that when you are writing an assignment to a reference, you're replacing the reference held by the variable. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Measurement of pitch
    ... as the method used by the Pythagoreans. ... of these reference units in the quantity to be measured. ... vibrating string seems as good as anything. ... The string or pendulum in question could no doubt be specified exactly, ...
    (sci.physics)
  • Re: Abstract class variables question
    ... I think I understand boxing a little better now. ... the object that is on the heap. ... value types are copied to the heap and made into an object and reference ... String types are already reference types and all we are doing when we do ...
    (microsoft.public.dotnet.languages.csharp)