Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Seekers of Perl Wisdom

( #479=superdoc: print w/replies, xml ) Need Help??

If you have a question on how to do something in Perl, or you need a Perl solution to an actual real-life problem, or you're unsure why something you've tried just isn't working... then this section is the place to ask. Post a new question!

However, you might consider asking in the chatterbox first (if you're a registered user). The response time tends to be quicker, and if it turns out that the problem/solutions are too much for the cb to handle, the kind monks will be sure to direct you here.

User Questions
Good REST API Tutorial for Perl.
1 direct reply — Read more / Contribute
by netizen
on Oct 29, 2020 at 16:36

    Exalted Monks,

    I was here last time asking about Perl and was both humbled and amazed by your answers. I am here again to ask you for another favour. I am looking for a good tutorial for REST API for Perl.

    What I understand as of now, is that REST API is something like a call I make to a website and give me the results in the json format, that's all.

    If you could direct me to a good tutorial that teaches REST API using Perl, I will be ever so thankful.

Perl DBI connect cached still active
1 direct reply — Read more / Contribute
by newperldeveloper
on Oct 28, 2020 at 20:43
    I am doing a perl query using
    my $prod_dbh = DBI->connect_cached('DBI:Sybase:database','web_','web') + || die "Couldn't connect to DB!\n" . DBI->errstr; my $parent_sth = $prod_dbh->prepare_cached('select * from table where +id = ?'); $parent_sth->bind_param(1,$id); $parent_sth->execute(); my @parent_data = $parent_sth->fetchrow_array();
    If I try to do another query it fails and give an error
    my $child_sth = $prod_dbh->prepare_cached('select * from table2 where +id = ?'); $child_sth->bind_param(1,$parent_data[0]); $child_sth->execute(); my @child_data = $child_sth->fetchrow_array(); print Dumper(@child_data);
    Fails with statement handle DBI::st=HASH(0x1ed1280) still Active at When I add the line $parent_sth->finish(); to the first block of code. I get no data from the @child_data If I just grab the query and run it data is returned so I know the result should contain data but nothing is being returned when I dump child_data. I know it is executing the code when I print after the dumper I get my printed content. I was reading I need to
Can't write values from sorted array of structs
2 direct replies — Read more / Contribute
by maschwarz
on Oct 28, 2020 at 12:51
how can I access podwebserver remotely?
3 direct replies — Read more / Contribute
by misterperl
on Oct 28, 2020 at 11:41
    I installed and started podwebserver as root. Oddly, the install did not put the executable into /usr/bin even though the install said OK. locate didn't find it anywhere!

    So I copied that from another node, placed it into /usr/bin, and chmod-ed it. Now it runs. It indexes modules, then tells me I can access the pod using localhost:8020 . Ducky!

    But that host is in another state- its NOT my localhost. So I figure no problem, I'll just use:

    http://www.myserver.com:8020
    But that times out.. Is this ONLY viewable as localhost?

    TYVM

Software error: DBD::mysql::st execute failed: Field 'name' doesn't have a default value
1 direct reply — Read more / Contribute
by jonnyfolk
on Oct 28, 2020 at 11:38

    I was recently forced into a server change and it has broken an old script that's in use. I'm getting the error: "Software error: DBD::mysql::st execute failed: Field 'name' doesn't have a default value at admin.cgi line 192."

    my $var; if ($new eq "charter") { $var = "c"; } elsif ($new eq "sales") { $va +r = "s"; } my $sth = $dbh->prepare("INSERT INTO $new (active) VALUES (0)"); #line 192 $sth->execute;

    Could any kind Monk please advise on the steps I should take to troubleshoot this

    NB. The script is connecting to the DB ok - other aspects like edit are working fine

Dereference a reference to a file handle created with IO::File
3 direct replies — Read more / Contribute
by dgdriscoll
on Oct 27, 2020 at 21:32

    Hi, everything else seems to work if I manually insert file names instead of using hash values but everything I have tried on lines 40,44 &47 gives me a string instead of a file handle. What am I doing wrong? thanks, Dave Driscoll

    #!/usr/bin/perl -w #ad_sort.pl #in ~/logs take paydirt.log as input #for each item in the ads hash, #search for a match for 'GET /bookstore/'+ads key #when a match is found, write that and all following lines to #ads key value . #until a string containing '####' is found then stop writing #keep reading until next match or EOF use IO::File; use strict; use diagnostics; my $do_write = 'no'; open(MYINPUTFILE, "<paydirt.log") || die("$!"); my $western_lore_ad1 = IO::File->new("> ad_logs/western_lore_ad1.log") +; my $western_lore_ad2 = IO::File->new("> ad_logs/western_lore_ad2.log") +; my $western_lore_ad3 = IO::File->new("> ad_logs/western_lore_ad3.log") +; my $native_ad1 = IO::File->new("> ad_logs/native_ad1.log"); my $native_ad2 = IO::File->new("> ad_logs/native_ad2.log"); my %ads = ('western_lore.php?ad=n1'=>$western_lore_ad1,'western_lore.p +hp?ad=n2'=>$western_lore_ad2,'$western_lore.php?ad=n3'=>$western_lore +_ad3,'native.php?ad=n1'=>$native_ad1,'native.php?ad=n2'=>$native_ad2) +; my @ad_strings = keys $ads; my(@lines) = <MYINPUTFILE>; foreach (@ad_strings){ my $this_ad = $_; my $clicks = 0; foreach my $line (@lines){ if ($line =~ m/GET \/bookstore\/\Q$this_ad/){ $do_write = 'yes'; $clicks++; } if ($do_write eq 'yes'and $line =~ m/###/){ print $ads{$this_ad} "$line\n";#this should print to one of the +files referenced by IO::File on lines 21-25 $do_write = 'no'; } if($do_write eq 'yes' ){ print $ads{$this_ad} $line;#ditto } } print $ads{$this_ad} "clicks = $clicks \n";#ditto } close (MYINPUTFILE); $western_lore_ad1->close; $western_lore_ad2->close; $western_lore_ad3->close; $native_ad1->close; $native_ad2->close; exit ();
Listing scripts in distribution metadata
1 direct reply — Read more / Contribute
by perlancar
on Oct 27, 2020 at 18:43
    I wonder why the CPAN distribution metadata does not define a more general 'provides' to let a distribution specifies the list of scripts or other documents that it contains, in addition to packages. It would make things easier for tools like MetaCPAN, or App::lcpan. Right now I believe (not confirmed yet by reading the source code) that MetaCPAN performs some static parsing of Makefile.PL to get EXE_FILES, but less-than-straightforward declaration of EXE_FILES will make it miss (example: Spreadsheet-Read's Makefile.PL).
Testing a Dancer2 app with locally installed modules
1 direct reply — Read more / Contribute
by GertMT
on Oct 27, 2020 at 17:44

    Just decided to get more into testing (i've read it is almost guaranteed my code will improve..). I created a new Dancer2 app. The modules I use are installed locally via a cpanfile and a `carton install` command. Everything works though I cannot get the testing to work as I get warnings like:

    Can't locate Dancer2/Plugin/Database.pm in @INC (you may need to insta +ll the Dancer2::Plugin::Database module)
    bin/app.psgi looks like:
    #!/usr/bin/env perl use strict; use warnings; use File::Basename; use File::Spec; use lib File::Spec->catdir( File::Basename::dirname( File::Spec->rel2a +bs($0) ), '..', 'local/lib/perl5' ); use lib File::Spec->catdir( File::Basename::dirname( File::Spec->rel2a +bs($0) ), '..', 'lib' ); use jwsDancer; jwsDancer->to_app;
    lib/jwsDancer.pm
    package jwsDancer; use Dancer2; use Dancer2::Plugin::Database; use Dancer2::FileUtils qw/path read_file_content/; use Cwd qw(abs_path); use lib dirname( dirname abs_path $0) . '/../../lib'; get '/' => sub { template 'index' => { 'title' => 'a web page' }; true;

    For now I'm just trying to use the test that is generated by scaffolding a Dancer2 app via

    prove -lv t/002_index_route.t

    Can anyone give a hint what I should do to have the locally installed modules (in local/lib/perl5) work as well in the testing environment?

a simple web front end
4 direct replies — Read more / Contribute
by lis128
on Oct 27, 2020 at 17:37

    hello fellow monks

    From long time i've been developing little Perl programs to do some stuff. As i do often (ab)use other REST apis to scrap data i need, i am familiar with Curl blessings, i know how to perform simple GET or trick certain endpoint to POST him given payload to receive wanted answer. Until now it almost always happened in console, where requested informations were thrown out to STDOUT. Simple stuff

    But my programs always lacked a crossplatform, human friendly web interface. Now i wanted to change this and write my own API (simple DB selects, some system() executions) and send AND receive response in browser
    So i started Apache, made it capable of running CGI scripts and wrote some simple "parse url arguments and return json" procedures. it works, cool.
    Problem arose when i tried to use same set of tools to run my "frontend". My "webui" consists of single form with textarea and submit button. Action is targetted against my api. So i fill textarea, press submit and... i get raw json response in my browser.
    Question is:

    How do i separate "api" from "frontend" and made them pass data between each other in order to
    - have nice form
    - have api to do actual job
    - receive data from api in json format, process it and return output to user in "web format", like a table?
    i deliberately am trying to avoid javascript and other node.js-junk, as i don't want to have anything in common with JS ;p

    my first idea was to include some sort of www-client (like LWP) in my "fronted" to do request on my behalf, capture "api's" output and process it, but it seems like big overkill for me to have Apache running CGI script which "embeds" www client which requests data from other apache instance.
    My other idea (utilizing forms) was to submit to the same page (my fronted) and check wheter request was caused by form submission (then do www-client magic) or just by entering site "for the first time to fill the form".
    Both solutions feels clunky to me, so here i am, seeking a wisdom ;)

    My frontend doesn't have to be pretty, "responsive" and "modern". I just want to be able to click my buttons using browser in phone, tablet and computer, simpler the better in my case
    Also separation is important as i want to develop only my "backend" features, while focusing as little as possible on frontend. Seriously, form with button and optional checkboxes is something which will satisfy me as this point

a perldbg question about breaks
4 direct replies — Read more / Contribute
by misterperl
on Oct 27, 2020 at 12:11
    I know I can set
    dbg> b myclass::mymethod
    anywhere. Which is very useful. And I can set
    dbg> b 123
    to break at line 123 in my current scope. But, can I set a break at a line in another class, like
    dbg> b myclass::123
    (I know THAT doesn't work) but is there a syntax to do that? TY

Add your question
Title:
Your question:
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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (3)
    As of 2020-10-31 11:14 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My favourite web site is:












      Results (288 votes). Check out past polls.

      Notices?