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

Passing hash references into subs

by Indomitus (Scribe)
on Jul 18, 2002 at 03:19 UTC ( [id://182688]=perlquestion: print w/replies, xml ) Need Help??

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

Sorry if this has been asked before but I can't seem to make SuperSearch do anything except give me errors and the google search is somewhat hard to use on this site. I'm trying to pass a hash reference from fetchrow_hashref into a function and it's not working.
my $book_hashref = $sel_cursor->fetchrow_hashref; display_basic($book_hashref);
then
sub display_basic { my $book_ref = shift; print "$book_ref->{'name'}<br>\n"; }
When I run this I get this error:
"my" variable $book_ref masks earlier declaration in same scope at Dis +playStuff.pm line 100. syntax error at DisplayStuff.pm line 96, near "sub display_basic" syntax error at DisplayStuff.pm line 102, near "}"
I'm somewhat new to hash references so if somebody could enlighten me as to what I need to do to make this work (or at least point me at a site I missed during my searching) I would greatly appreciate it. Thanks in advance.

Replies are listed 'Best First'.
Re: Passing hash references into subs
by Abstraction (Friar) on Jul 18, 2002 at 03:29 UTC
    You are passing/accessing the hash reference correctly. My guess is there's something else wrong that's causing this error. Post more code and maybe something else will surface.
Re: Passing hash references into subs
by thelenm (Vicar) on Jul 18, 2002 at 03:33 UTC
    The code that you posted looks fine to me, but from the error messages you're getting, it looks like there is a syntax error near line 96 in DisplayStuff.pm. You should fix the syntax error (possibly a missing or extra curly brace?) before doing anything else.

    -- Mike

    --
    just,my${.02}

Re: Passing hash references into subs
by graff (Chancellor) on Jul 18, 2002 at 04:49 UTC
    Based on this part of the error diagnostics:

    "my" variable $book_ref masks earlier declaration in same scope at DisplayStuff.pm line 100

    the first thing I'd do is search backward in the module from line 100, looking for "$book_ref", to see if it's declared with "my" at some point outside of any other subroutine -- i.e. lexically "global" within the module file (excuse me if those terms are not exactly correct, but I hope you get the idea).

    The other possibility, given the nature of the syntax error reports, is that a previous sub or other block is lacking a closing curly brace, causing the "sub display_basic" to be considered as part of some other block of code (which also happens to declare its own "my $boof_ref"); if you use a programmer's text editor that supports automatic indentation (like (x)emacs) -- in the sense that the editor can parse the code well enough to indent a line appropriately, based on the current bracketing depth -- this can help a lot to watch for cases where you've forgotten a close-bracket somewhere.

Re: Passing hash references into subs
by Indomitus (Scribe) on Jul 18, 2002 at 03:40 UTC
    The other thing I forgot to mention is that this is in a module. I don't know if that makes a difference.
      Possibly. But because the problem doesn't appear to be in the code you posted we'll need to see more code to help.
Re: Passing hash references into subs
by cfreak (Chaplain) on Jul 18, 2002 at 04:02 UTC

    At least in my experieance the -> operator doesn't interpolate. You can do one of two things:

    print $book_ref->{'name'},"<br>\n";
    Or:
    print "$$book_ref{'name'}<br>\n";

    To me the first method seems cleaner but its just personal preference.

    Hope that helps
    Chris

    UPDATE: I stand totally correctly and apologize for my ignorance. Thanks thelenm :)

    Lobster Aliens Are attacking the world!
      The original code should work fine. Try this example:
      my $hashref = {'name' => 'Homer'}; print "$hashref->{'name'}\n";

      -- Mike

      --
      just,my${.02}

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-09-10 04:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.