Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Use of 'our' considered harmful

by tantarbobus (Hermit)
on Sep 24, 2004 at 10:44 UTC ( [id://393442]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Use of 'our' considered harmful
in thread Use of 'our' considered harmful

sub whatever_its_called { our ($dbh) = DBI->connect(...); sub do_hit { $dbh->prepare("stuff"); } }

Replies are listed 'Best First'.
Re^4: Use of 'our' considered harmful
by grinder (Bishop) on Sep 24, 2004 at 10:55 UTC

    I don't use our, so I'm not sure if there's some subtlety going on here that I am missing. With that in mind, I fail to see how your sub differs from:

    sub whatever_its_called { my ($dbh) = DBI->connect(...); sub do_hit { $dbh->prepare("stuff"); } }

    Look ma! no our! That's the whole point.

    - another intruder with the mooring of the heat of the Perl

      Actually, your code closes the $dbh before you ever execute the query! Where as the our version doesn't. With the our version, the user can use $dbh in other functions, because $dbh is a global.

      I'm not saying our is required to do this -- it isn't -- I'm just saying you broke the program by blindly switching to my.

        closes $dbh??
      You don't want to try to define a named sub inside of another named sub! You can get away with it if you only use global variables, but you never want to do it with lexicals.

      You have just triggered the infamous puzzling, "variable will not stay shared" warning. See Re (tilly) 9: Why are closures cool? which tries to explain what Perl is trying to warn you about and why there is no good resolution to the problem.

Re^4: Use of 'our' considered harmful
by gellyfish (Monsignor) on Sep 24, 2004 at 11:06 UTC

    But surely my would be more appropriate here - $dbh is still a global variable, it is only the effect of our that is lexical.

    /J\

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-04-19 15:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found