Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Multiple file handles

by exilepanda (Friar)
on Feb 03, 2023 at 06:02 UTC ( [id://11150116]=note: print w/replies, xml ) Need Help??


in reply to Multiple file handles

using one file to reference zip codes
Instead of Spreadsheet, INI file maybe a lite weight alternative. The content may be look like:
code1 = state 1 code2 = state 2 ...
Then your Perl script:
use strict; use Config::Tiny; my $Config = Config::Tiny->read( 'ZipCodes.ini' ); sub check { my $code = shift; return $Config->{_}{$code} or die "No such code '$code' "; } my $st_name = check ( $code ) ;
Code not tested, but the idea is like that. CPAN Config::Tiny

Replies are listed 'Best First'.
Re^2: Multiple file handles
by stevieb (Canon) on Feb 03, 2023 at 17:09 UTC

    If I were to use a file as opposed to a DB, I'd most definitely use JSON. Cross-platform, cross-language, and web API ready.

    codes.json

    { "91280": "CA", "61682": "NY", "23823": "WA" }

    Perl script:

    use warnings; use strict; use JSON; my $file = 'codes.json'; my $data; { local $/; open my $fh, '<', $file or die $!; my $json = <$fh>; $data = decode_json $json; } print "$_: $data->{$_}\n" for sort keys %$data;

    Output:

    23823: WA 61682: NY 91280: CA
      Thats actually a really good idea, We do use alot json stuff here, this might be a simpler way for me to do this, because I'd like to create 2 versions where I can run one on my machine, then use another to reference the database, but still not sure if I could reference mysql database on my local machine. i'll post more data once I get that connection working. This is what I have so far though.
      my $database = "asterisk"; my $user = "root"; my $host = 192.168.8.50; my $password = 123; my $dbhA = DBI->connect("DBI:mysql:$database,$host","$password") o +r die "Couldn't connect to database: ".DBI->errstr;
      using DBI What would you recommed DBI vs DBD::mysql?

        DBI is the plumbing; DBD::mysql is the fitting. You need both.

        From the doc for DBD::mysql (plus your error handling):

        my $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port"; my $dbh = DBI->connect($dsn, $user, $password) or die "Couldn't connec +t to database: ".DBI->errstr;
        Set up all those variables then use that exact code snippet and it should work for you.

        Hope this helps!


        The way forward always starts with a minimal test.
Re^2: Multiple file handles
by MoodyDreams999 (Beadle) on Apr 07, 2023 at 21:37 UTC
    oo thats a nice module, I'm going to try playing around with that idea maybe theres a tiny module I can use to help reference or make a log file too. I've decided to take another crack at connecting to mysql database this time though, but ill definitely use that idea. Thank you

Log In?
Username:
Password:

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

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

    No recent polls found