Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

variable scope within eval{}

by Stegalex (Chaplain)
on Jan 29, 2010 at 22:29 UTC ( [id://820412]=perlquestion: print w/replies, xml ) Need Help??

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

I guess this must be a stupid question, but I am stumped. I am using eval{} blocks to implement database transactions (or I would like to). I am putting my important updates inside an eval{} block. The problem seems to be that I can't pass variables between subroutines within an eval block.
Example:
eval { $bl_id = insert_budget_line (); @bly_id = insert_budget_line_year ( bl_id => $bl_id, planning_horizon => @planning_horizon, ); };
In the above, $bl_id is visible within the eval block, but it is NOT visible inside of subroutine insert_budget_line_year().

What gives?

Replies are listed 'Best First'.
Re: variable scope within eval{}
by crashtest (Curate) on Jan 30, 2010 at 00:13 UTC

    I think the assembled monks are scratching their heads because there's nothing all that special about an eval{} block. It should be simple enough to test - just compare the results of running your code as is and with the eval { ... }; opening/close lines commented out.

    Something else that caught my eye is the way you're calling insert_budget_line_year. The => arrow notation implies you're building a hash from the arguments, but then I'd expect:

    @bly_id = insert_budget_line_year ( bl_id => $bl_id, planning_horizon => \@planning_horizon, # array ref...? );
    Depends on how you're handling the parameters in the subroutine.

    Perhaps if you showed the pertinent parts of insert_budget_line_year, it might shed more light on what's happening.

Re: variable scope within eval{}
by holli (Abbot) on Jan 29, 2010 at 22:48 UTC
    insert_budget_line does probably not return what you think it does. Did you check?


    holli

    You can lead your users to water, but alas, you cannot drown them.
      Yes I did and it works fine in the eval block but is invisible when passed to the sub

        Easy enough to prove it works:

        >perl -wMstrict -le "my $bl_id; my @bly_id; sub insert_budget_line { return 'foo'; } sub insert_budget_line_year { return @_[1,0]; } eval { $bl_id = insert_budget_line(); @bly_id = insert_budget_line_year(bl_id => $bl_id); }; print qq{@bly_id}; " foo bl_id

        How is your  insert_budget_line_year function defined?

        Can you post the relevant code of the sub you call?


        holli

        You can lead your users to water, but alas, you cannot drown them.
Re: variable scope within eval{}
by pileofrogs (Priest) on Jan 29, 2010 at 23:50 UTC

    Try to recreate the problem with the simplest script possible. Does it still do this if you assign $bl_id a quoted string? What if you replace insert_budget_line_year with lc()?

Re: variable scope within eval{}
by shmem (Chancellor) on Jan 30, 2010 at 17:18 UTC
    In the above, $bl_id is visible within the eval block, but it is NOT visible inside of subroutine insert_budget_line_year().

    You are retrieving the parameters from @_ inside insert_budget_line_year(), aren't you? Sounds like a stupid question, but then I've seen (and perpetrated) too many *facepalm* bugs.

    my $bl_id = "foo"; # code, code, code... if(1){ eval{ my $bl_id = "bar"; my $res = insert_budget_line_year ( bl_id => $bl_id, ); } } sub insert_budget_line_year { print $bl_id,"\n"; } __END__ foo

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-25 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found