Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Subroutine Reference with use strict

by chiller (Scribe)
on Apr 13, 2002 at 09:21 UTC ( [id://158750]=perlquestion: print w/replies, xml ) Need Help??

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

I am using Apache::Register to process stuff (I don't know how that works or really have a grip on how it affects the code, but):

Say I have this:

%states = ( 'Default' => '\&front_page(@)', 'Lost' => '\&lost(@)', );
This gives me Can't use string ("\&logoff(@)") as a subroutine ref while "strict refs" in use at (eval 69) line 113. Alternately:

%states = ( 'Default' => \&front_page(@), 'Lost' => \&lost(@), );
This gives me a syntax error (on the last line with the end-paren and semicolon).

What gives? What is the right way to do this? I was trying to just copy the text out of the Perl Cookbook but that didn't seem to work (with the syntax error and all). The syntax error IS in this somewhere--I commented it out, and perl -c said everything else was OK. Maybe I should just turn off 'strict refs' but I'd like to know which way I'm *supposed* to be doing things.

Thanks.

Replies are listed 'Best First'.
Re: Subroutine Reference with use strict
by rinceWind (Monsignor) on Apr 13, 2002 at 09:47 UTC
    chiller, what's the purpose of the (@)s in your code?

    Try:

    %states = ( 'Default' => \&front_page, 'Lost' => \&lost, );
    as you are generating coderefs for sub front_page and sub lost. See perldoc perlsub for more about coderefs.
      The purposes of the prototypes in the refs is to avoid this particular error, unless you can find another way around it:
      Prototype mismatch: sub Apache::ROOT::myPackage::::lost vs (@) at (eva +l 72) line 478 (#5)
      Obviously it isn't supposed to work that way, and putting the prototypes in there was just kind of a shot in the dark. But that particular error went away :)
Re: Subroutine Reference with use strict
by mirod (Canon) on Apr 13, 2002 at 11:52 UTC

    The problem is that you quote the values of the hash. As it is you just put the string \&front_page(@) in $states{Default}.

    What you want to do is put a code reference in $states{Default}. You do this, as Ricewind explained, by taking &front_page (the function, as a variable) and taking its reference: \&front_page. Then you can use that code reference, by doing my $current_state= 'Default'; $state{$current_state}->( $param1, $param2) (I suspect you tried to use eval to call the function, you don't need to, and in fact you should not).

Re: Subroutine Reference with use strict
by Juerd (Abbot) on Apr 13, 2002 at 17:39 UTC

    You can't give prototypes to sub references like that. Drop the (@), and drop the quotes. Don't worry about the prototypes as sub references are always called at run-time, and protypes aren't even checked at run time. Besides, no prototype equals (@).

    %states = ( Default => \&front_page, Lost => \&lost, );
    By the way, there is no Apache::Register (at least not in my CPAN mirrors). It's called Apache::Registry.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

      I gave the prototype to the subroutine reference because without it, I get the "Prototype Mismatch" error, IN ADDITION TO the subroutine error.

      There is not a (@) prototype?! Programming Perl 3rd ed. lists them on pg. 226. :)

      Yes, it's Apache::Registry, sorry.

        The prototype problem is probably because you didn't have just \&foo, rather something like \&foo(), in which case the subroutine foo would have been run, which would result in prototype mismatch. However, if you leave out the parentheses completely no subroutine call will be made and such no prototype check will occure there. If you do like Juerd and rinceWind said then you're home safe. You must realize that & is the sigil of subroutines, just like $ is the sigil of scalars. So you use & to address it, not call it. If you want to execute the subroutine then you use parentheses, but not otherwise.

        (Disclaimer to other monks: Yes, I know this is very simplified and I know about &foo and it's magical @_ treatment, but that's another issue and let's not confuse he who seeks help with almost irrelevant details.)
Re: Subroutine Reference with use strict
by chiller (Scribe) on Apr 14, 2002 at 01:59 UTC
    Hey Monks,

    Ugh. I seem to be good at answering my own ?'s. The problem is not with the code, the problem is with mod_perl. In particular, I simply needed to restart Apache. (sheepish grin)

    Anyone who is having weird errors and warnings writing CGI scripts under mod_perl should go here:

    http://www.perlreference.com/mod_perl/guide/warnings.html

    There are quite a few quirks out there...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 02:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found