Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Re: Avoiding user-input in sub calls.

by pekkhum (Sexton)
on Nov 01, 2003 at 13:34 UTC ( #303807=note: print w/ replies, xml ) Need Help??


in reply to Re: Avoiding user-input in sub calls.
in thread Avoiding user-input in sub calls.

This is the first mention I have heard of "dereferencing" a variable. I was looking for it in perldoc and found some references to it, but wish to double check what I have gathered. It seems to me:

&{ $functions{$doit} };

Would be seen by Perl as:

"Take the value of $functions{$doit} and treat it as a sub name"

Is that about right?
I also keep seeing the use of my $thing = $q->param('whatever'); but am having trouble searching for it as I do not know what name it goes by...

Thanks, for the help and better code.


Comment on Re: Re: Avoiding user-input in sub calls.
Select or Download Code
Re: Re: Re: Avoiding user-input in sub calls.
by barrd (Parson) on Nov 01, 2003 at 13:45 UTC
    ..."dereferencing" a variable...
    It isn't dereferencing a 'variable' but a subroutine.
    "Take the value of $functions{$doit} and treat it as a sub name" Is that about right?
    More or less, yeah. But I have to admit that my powers of syntax explaining are pretty poor, I understand in my head what is going on but I don't want to lead you down the garden path with my crap explanations. There are many people here who could explain far more eloquently what is going on and hopefully one of them will step in to help both of us ;)
Re: Re: Re: Avoiding user-input in sub calls.
by bobn (Chaplain) on Nov 01, 2003 at 14:23 UTC

    I also keep seeing the use of my $thing = $q->param('whatever'); but am having trouble searching for it as I do not know what name it goes by...

    This is the OO ('Object Oriented') interface to CGI.pm:

    use CGI; my $q = CGI->new; my $value = $q->param('name');
    Roughly equivalent to:
    use CGI; my $value = CGI::param('name');
    except the OO way rends to be more flexible and earsier to modify (eg, you could write/use a module that inherits from CGI, by modifying only the
    use
    and
    new
    statements. There's other reasons, but it's too early.

    --Bob Niederman, http://bob-n.com

    All code given here is UNTESTED unless otherwise stated.

Re: Re: Re: Avoiding user-input in sub calls.
by edan (Curate) on Nov 03, 2003 at 07:41 UTC

    &{ $functions{$doit} };

    Would be seen by Perl as:

    "Take the value of $functions{$doit} and treat it as a sub name"

    No, there's some confusion here about references. perlman:perlref is the definitive reference (no pun intended), but I'll try to give you the short answer.

    The syntax &{ SOMETHING } is, as you correctly said, a dereference. It says "Treat SOMETHING as a code reference", which means that the subroutine referred to by SOMETHING is executed. That means that SOMETHING is taken to be a reference, and in the example given, that's just what it is, as is evidenced by the code:

    my %functions = ('first' => \&one, 'second' => \&two );

    That \&one syntax is creating a hard reference to the subroutine one().

    If, however that SOMETHING is not actually a hard reference, but is just a plain old scalar, then the "value of the scalar is taken to be the name of a variable, rather than a direct link to a (possibly) anonymous value." (from perlman:perlfref). So if the code example would have looked like this:

    my %functions = ('first' => 'one', 'second' => 'two' );

    ... then you would be using a symbolic reference, which is what you mean by "Take the value of $functions{$doit} and treat it as a sub name". NOTE that this whole Symbolic reference business is disallowed by use strict 'refs', meaning that our friend SOMETHING must be an actual reference, and not just a name. This is probably a Good Thing.

    Hope this helps. And by the way, I think the syntax $coderef->() is preferable to the equivalent &{$coderef}, since I think it just looks clearer that you're calling a subroutine...

    --
    3dan

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://303807]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-12-20 20:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (98 votes), past polls