Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How do you tell if a sub argument is an lvalue?

by rsmah (Scribe)
on Feb 10, 2007 at 20:42 UTC ( #599392=perlquestion: print w/replies, xml ) Need Help??

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

I have a bit of a problem. I want to know if an argument to a sub is a variable or a constant, that is, is it an lvalue or not?

Let's say I call a function called foo thusly...

foo($a, "b");
Inside foo(), I'd like to be able to determine that $_[0] is a variable (and thus an lvalue) and that $_[1] is a constant (and thus not an lvalue). Any ideas on how?

Replies are listed 'Best First'.
Re: How do you tell if a sub argument is an lvalue?
by diotalevi (Canon) on Feb 10, 2007 at 21:24 UTC

    use Scalar::Util::readonly() on your variables.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: How do you tell if a sub argument is an lvalue?
by bart (Canon) on Feb 10, 2007 at 21:32 UTC
    I did a little inspired search on CPAN, and I found Scalar::Readonly. As you can guess, I haven't tried it, but at least, judging by the docs, it's looking good.
      Doh! Silly me.

      Thanks very much diotalevi and bart.

Re: How do you tell if a sub argument is an lvalue?
by liverpole (Monsignor) on Feb 10, 2007 at 20:57 UTC
    Hi rsmah++,

    That is a very interesting question!

    I don't know if this is the best answer, but my first thought was to try assigning to the variable and see what happens:

    use strict; use warnings; my $a = "123"; foo($a, "456"); sub foo { for (my $i = 0; $i < @_; $i++) { my $type = eval { $_[$i] = "variable" } || "constant"; printf "Arg #%d is $type\n", $i + 1; } }

    The output of this is:

    Arg #1 is variable Arg #2 is constant

    Since the write operation is wrapped in an eval, it returns undef if the assignment failed (constant), otherwise the result of the assignment.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: How do you tell if a sub argument is an lvalue?
by rinceWind (Monsignor) on Feb 11, 2007 at 00:14 UTC

    Also look at Want, which answers the general question, rather than just to test for readonliness.

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

      Maybe I missed something, but I don't see how Want can be used to check if an argument is read-only.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://599392]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2022-05-27 21:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (98 votes). Check out past polls.

    Notices?