checking if value is lvalue(modifiable) or not

Discussion in 'Perl Misc' started by Yakov, Dec 6, 2006.

  1. Yakov

    Yakov Guest

    Is there a function that tests whether $_[0] is bound to modifiable
    variable (like in foo($var)) or to non-modifiable variable (like in
    foo("str")) ?

    The context of my question is like this:
    sub chompit { # chomp + returns the result + modify the arg if
    if(modifiable($_[0])) {
    # is there any modifiable() function ?
    return $_[0];
    } else {
    # we can't apply chomp() to the arg, it's constant
    my ($copy) = shift;
    return $copy;
    $result = chompit(`date`); #arg is constant
    $result = chompit($_); # arg is not constant

    Yakov, Dec 6, 2006
    1. Advertisements

  2. Yakov

    anno4000 Guest

    see readonly() in Scalar::Util


    anno4000, Dec 6, 2006
    1. Advertisements

  3. Actually, qx// (like most Perl builtin functions and normal
    (non-lvalue) subroutines) doesn't return a readonly values.

    You'd think that you could force a subroutine to return a readonly
    value by using an lvalue subroutine but actually Perl will, in effect,
    dynamically undo the lvalue-ness of the subroutine if you try.

    sub foo : lvalue {
    my $x = \my $y; # $x is ref to variable
    print 'In foo $x is ',$x;

    print ' \foo is ',\foo,"\n";

    sub bar : lvalue {
    my $x = \1; # $x is ref to constant
    print 'In bar $x is ',$x;

    print ' \bar is ',\bar,"\n";
    Brian McCauley, Dec 6, 2006
  4. Yakov

    Yakov Guest

    But then, why do I get following error trying to modify `xxx`:

    perl -e 'chomp(`date`)'
    Can't modify quoted execution (``, qx) in chomp at -e line 1, at end
    of line
    Execution of -e aborted due to compilation errors.

    Yakov, Dec 13, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.