Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hello,
I have a problem, when I try to use a refernce to an hash in a subroutine.

That works:
sub test { my %q=@_; while ( my ($key, $value) = each(%q) ){ } }

But I need to modify the orginal values, not a copy through "my" .
But that:

sub test { while ( my ($key, $value) = each( {@_} ) ){ #or { $_[0] } with the +same result } }
doesn't work, because I have:
Type of arg 1 to each must be hash (not anonymous hash ({})) near "} +)

Any suggestion about I can solve that?
Thank you!

_____________ UPDATE:

On a suggestion below I write all my code:

I would like to creare a Simply SQL wrapper.
So:
my $dbh; my (%q,@select,@insert,@update,@delete); %q= ( select => \@select, insert =>\@insert, update =>\@update, delete=>\@delete ); $select[0]=['SELECT * FROM emails WHERE `email`=?']; $select[1]=['SELECT * FROM emails AS e RIGHT JOIN bounces AS b ON e.` +emails_id`=b.`emails_id` WHERE e.`email`=? AND b.`email_date`=FROM_UN +IXTIME(?) '];
And then:
sub elabora_st { # argument list: # action='prepare' or action='finish' # then the hash delle query my $action=shift @_; my %q=@_; for my $value (values %q ){ for my $sel ( @$value ) { # print "$sel->[0]\n"; if ( $action eq 'prepare') { # print "$sel->[0] \n"; my $sth=$dbh->prepare($sel->[0]); $sel->[1]=$sth; } elsif ($action eq 'finish') { # to do } } } }
Finally:
&connection; &elabora_st ('prepare',%q); # other stuff # I would like to have queries like that, or something similar: # %q->select[1][1]->execute($address,$mailTime); &end_connection;

But I need to modify the original %q, not a copy...
I think that in object orientend code, I will have a better result, but I'm not yet able to create OO Perl code.
Finally I don't want to modify the global %q, but only the %q passed through argument list.

In reply to deferencing an hash in a subroutine by saintex

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-03-29 00:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found