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

Monks, A quicky syntax question:

my $CFref = $self->param('CFref'); my %CF = %$CFref;

works. But

my %CF = %$self->param('CFref');

Doesn't. Error:

Can't call method "param" without a package or object reference at Con +tact.pm line 25.

Can someone explain how to do this in one line not two?

-------------------------------------
Nothing is too wonderful to be true
-- Michael Faraday

Replies are listed 'Best First'.
Re: dereferencing syntax question
by BrowserUk (Patriarch) on Oct 23, 2003 at 22:36 UTC

    Try

    my %CF = %{ $self->param('CFref') };

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!

      This makes sure that the deref happens last, and that's what you want.

      This roots in the precedence of perl operators. % is kind of term operator, which takes the highest precedence. ->takes the second highest.

      That would work. I would probably prefer to modify the param method and have it return a list in list context so that
      my %CF = $self -> param ('CPref');
      just works. But then, I don't know how 'param' is used in list context already, so it might not be feasible.

      Abigail

        Thanks to BrowserUk and pg for the fix and the concise reason why.

        Abigal: param is from CGI::Application module. I'm planning to use as is and not override it.

        -------------------------------------
        Nothing is too wonderful to be true
        -- Michael Faraday

        Returning a hashref is a lot more efficient than a hash, because of all the flattening Perl does to the hash when passing one back from a subroutine. In a CGI program, a method like param() could be used a lot, so it's important to optimize it.

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        :(){ :|:&};:

        Note: All code is untested, unless otherwise stated