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
    possible
    if(modifiable($_[0])) {
    # is there any modifiable() function ?
    chomp($_[0]);
    return $_[0];
    } else {
    # we can't apply chomp() to the arg, it's constant
    my ($copy) = shift;
    chomp($copy);
    return $copy;
    }
    }
    $result = chompit(`date`); #arg is constant
    $result = chompit($_); # arg is not constant

    Thanks
    Yakov
     
    Yakov, Dec 6, 2006
    #1
    1. Advertising

  2. Yakov

    -berlin.de Guest

    Yakov <> wrote in comp.lang.perl.misc:
    > 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")) ?


    see readonly() in Scalar::Util

    [snip]

    Anno
     
    -berlin.de, Dec 6, 2006
    #2
    1. Advertising

  3. On Dec 6, 12:36 pm, "Yakov" <> wrote:

    > $result = chompit(`date`); #arg is constant


    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;
    $$x;
    }

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

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

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

    Yakov Guest

    Brian McCauley wrote:
    > On Dec 6, 12:36 pm, "Yakov" <> wrote:
    >
    > > $result = chompit(`date`); #arg is constant

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


    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

    >
    > 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;
    > $$x;
    > }
    >
    > print ' \foo is ',\foo,"\n";
    >
    > sub bar : lvalue {
    > my $x = \1; # $x is ref to constant
    > print 'In bar $x is ',$x;
    > $$x;
    > }
    >
    > print ' \bar is ',\bar,"\n";
     
    Yakov, Dec 13, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Biber

    Re: Why is this not a modifiable lvalue.

    Simon Biber, Jun 27, 2003, in forum: C Programming
    Replies:
    0
    Views:
    2,362
    Simon Biber
    Jun 27, 2003
  2. Dan Pop

    Re: Why is this not a modifiable lvalue.

    Dan Pop, Jun 27, 2003, in forum: C Programming
    Replies:
    6
    Views:
    4,926
    Dan Pop
    Jun 30, 2003
  3. Martin Ambuhl

    Re: Why is this not a modifiable lvalue.

    Martin Ambuhl, Jun 27, 2003, in forum: C Programming
    Replies:
    0
    Views:
    705
    Martin Ambuhl
    Jun 27, 2003
  4. Micah Cowan

    Re: Why is this not a modifiable lvalue.

    Micah Cowan, Jun 28, 2003, in forum: C Programming
    Replies:
    1
    Views:
    763
    CBFalconer
    Jun 28, 2003
  5. Kavya

    lvalue -modifiable and non-modifiable

    Kavya, Nov 6, 2006, in forum: C Programming
    Replies:
    3
    Views:
    438
    Keith Thompson
    Nov 6, 2006
Loading...

Share This Page