Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: Multiple file handles

by stevieb (Canon)
on Feb 03, 2023 at 17:09 UTC ( [id://11150139]=note: print w/replies, xml ) Need Help??


in reply to Re: Multiple file handles
in thread Multiple file handles

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

Replies are listed 'Best First'.
Re^3: Multiple file handles
by MoodyDreams999 (Beadle) on Apr 07, 2023 at 21:44 UTC
    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.
        Yeah, that helped alot, I was trying to find an example where they use the dsn in the connect statement, wasn't sure I was doing it right since it kept failing, probably just need to make sure its got the right credentials since we have like 5 different servers for similar things.

Log In?
Username:
Password:

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

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

    No recent polls found