So, my thought. I think it might be nice if there was a way to make a subroutine behave, in regard to naming scopes, exactly the same as if, instead of calling the module, you had cut and pasted the lines of code into your script in place of the subroutine call.

I think there is, though it would involve some XS programming. If you can find the CV (internal data structure holding a subroutine) you want to duplicate, you could theoretically copy it and change its stash value (lexical scope, basically) and stick it in the appropriate GV (symbol table). Any XS hackers out there care to enlighten us?

Barring that, is there a reason you can't use a closure?

sub make_booking_SQL { my ($BookingID, $ref_booking, $ref_event, $ref_course) = @_; return sub { $sth = $dbh->prepare("SELECT * FROM booking WHERE BookingID = +$BookingID") or die $dbh->errstr; $sth->execute(); $ref_booking = $sth->fetchrow_hashref; $sth = $dbh->prepare("SELECT * FROM event WHERE EventID = $ref_ +booking->{'EventID'}") or die $dbh->errstr; $sth->execute(); $ref_event = $sth->fetchrow_hashref; $sth = $dbh->prepare("SELECT * FROM course WHERE CourseID = $re +f_booking->{'CourseID'}") or die $dbh->errstr; $sth->execute(); $ref_course = $sth->fetchrow_hashref; }; }
You could also use placeholders and output binding, as in my DBI article. ;)

In reply to Re: subroutines and namespace by chromatic
in thread subroutines and namespace by George_Sherston

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.