Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Creating a library

by dracos (Sexton)
on Mar 13, 2006 at 19:07 UTC ( [id://536340]=perlquestion: print w/replies, xml ) Need Help??

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

Is there a way to create a file that can be used by multiple scripts that contains only variables/constants other than by making them global and using "require"?
I have a set of SQL statments that I would like to bo able to put in a seprate file. Currently I have it embeded in my single script but a will be writing more scrpts hence the question.

Example of one of the SQL statments:
#! /usr/bin/perl -w use Date::Calc qw{Today Add_Delta_Days}; #needed to create date range in sql statments my $interval = -1; my ( $year, $month, $day ) = Today(); my ( $y_year, $y_month, $y_day ) = Add_Delta_Days( Today(), $interval ); my $tday = sprintf qq{'%s-%02d-%02d'}, $year, $month, $day; my $yday = sprintf qq{'%s-%02d-%02d'}, $y_year, $y_month, $y_day; # New Tickets by Date Range my $REPORT_BY_DATE = q{ SELECT p.value, id AS ticket, summary, status, component, version, milestone, t.type AS type, (CASE status WHEN 'assigned' THEN owner||' *' ELSE owner END) AS owner, strftime("%m/%d/%Y - %H:%M:%S", t.time, 'unixepoch') AS CreatedTime, c.value as Verified FROM ticket t, enum p, ticket_custom tc, ticket_custom c WHERE p.name = t.priority AND p.type = 'priority' AND t.type = 'defect' AND DATETIME(t.time, 'unixepoch')>= } . $yday . q{ AND DATETIME(t.time, 'unixepoch')<= } . $tday . q{ AND tc.ticket = t.id AND tc.name = 'story_card' AND t.status = 'new' AND c.ticket = t.id AND c.name = 'verified' ORDER BY id; };

Replies are listed 'Best First'.
Re: Creating a library
by rafl (Friar) on Mar 13, 2006 at 19:43 UTC

    You don't need to mark those variables as global. You can simply put everything in a package and use some accessors

    How about that?

    package SqlStatements; sub report_by_date { return q{ SELECT ... FROM ... WHERE ... }; }
    In the code that uses that you can then simply do
    use SqlStatements; #or require or do or whatever you like my $sql = SqlStatements->report_by_date(); my $sth = $dbh->prepare($sql); ...

    Flo

      <Insert Head smaking sound here>
      Aparently I was stuck in the mindset of thinking of variables as soon as I read your reply I had one of thoes DUH moments.
      Thanks
      ron...
Re: Creating a library
by izut (Chaplain) on Mar 13, 2006 at 20:09 UTC

    Check Exporter. This module is used to export symbols to caller's package. I think it is what you're looking for.

    Igor 'izut' Sutton
    your code, your rules.

Log In?
Username:
Password:

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

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

    No recent polls found