Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
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
Equality of strings
4 direct replies — Read more / Contribute
by ovedpo15
on Oct 21, 2019 at 15:32
    Hello dear monks,
    I wrote the following code:
    do { $content = make_get_request($url_reports); $tries--; } while ($content == "[]" && $tries > 0); print $content."\n"; print "check:".($content == "[]")."\n"; if ("$content" == "[]") { print "Failed to report.\n"; } else { print "Successfully reported.\n"; } sub make_get_request { # Builds cmd for checking in API return `$cmd`; }
    When I run it (and the data got to the DB), I get:
    [{"_id":"123125125","files":6,"user":"abcasda","timestamp":1571685134, +"runtime":3,"run_name":"checking"}] check:1 Failed to report. Successfully reported.
    Why does it enter in the if-statement if $content != "[]?
Strange DBI/DBD::ODBC behaviour (right truncation of data on ODBC call)
2 direct replies — Read more / Contribute
by jahero
on Oct 21, 2019 at 09:01

    EDIT: Solved by poj (thank you!), node Re^3: Strange DBI/DBD::ODBC behaviour (right truncation of data on ODBC call)

    Strange DBI/DBD::ODBC behaviour (right truncation of data on ODBC call)

    Dear monks, I am seeking your wisdom in regards to a problem with DBD::ODBC.

    I am issuing the a statement into a database connected via ODBC, roughly in the following fashion:

    $sth = $dbh->prepare('show table"EP_TGT"."NA_DATA_CNTL";'); $sth->execute; while (my $text = $sth->fetchrow_array) { # a piece of code not relevant to the question }
    Resultset should be approximately ~30.000 bytes long.

    The query (fetchrow_array) fails with the error of DBD::ODBC::st fetchrow_array failed: st_fetch/SQLFetch (long truncated DBI attribute LongTruncOk not set and/or LongReadLen too small).

    I was tinkering with the problem a little bit, with the following result:

    • Setting LongReadLen to 65535 does not help.
    • Setting LongReadLen to 1 yields partial DDL statement for the table

    This is the result of DBI trace (the important part).

    Based on that, it seems that:
    • DBI is treating return value as VARCHAR, not as LOB, therefor rendering any manipulation of LongReadLen moot
    • Setting LongTruncOk lets the query run its course, however yields only partial results - which is bad

    I am on perl 5.28 64bit, DBI version 1.642, DBD::ODBC 1.60

    The question: can you suggest a way how to run the querry sucessfully, without truncating the data? Can I "persuade" DBI to use longer buffer for VARCHARs somehow?

    Thank you for your wisdom.

    Regards,
    Jan.

[OT] SQL "on duplicate key" custom action
4 direct replies — Read more / Contribute
by haukex
on Oct 21, 2019 at 03:18

    Hi all,

    My SQL-fu is too weak for this one and Google hasn't helped yet. I'm inserting data records into a table, and there will be lots of duplicates: if the primary key exists, it should silently ignore the record if all the other columns match, but throw an error if they don't. Basically, of the following, the second should be ignored and the third should throw an error. I feel like there should be an easy solution but I'm not seeing it yet.

    INSERT INTO records (PriKey, Foo, Bar) VALUES (1, 'Hello', World'); INSERT INTO records (PriKey, Foo, Bar) VALUES (1, 'Hello', World'); INSERT INTO records (PriKey, Foo, Bar) VALUES (1, 'Hello', Quz');

    Also, related: Since I can choose the DB, what is your opinion on MySQL vs. Postgres? I'm thinking I'll probably use the latter but would like to hear any opinions.

Issue while reading decimal number from a Spreadsheet using perl
5 direct replies — Read more / Contribute
by KishKishore
on Oct 21, 2019 at 01:31

    Hi Guys, I'm very new to perl. I was just doing some read/write operations in excel sheet using perl. My issue is: When i write a decimal number (fraction number. Ex : 2.5), and read it back, the output is some long number with extra decimals. I dont know why this is happening. Please help me

    use Spreadsheet::ParseExcel; use Spreadsheet::ParseExcel::SaveParser; ## Write to an existing excel sheet $parser = Spreadsheet::ParseExcel::SaveParser->new(); my $workbook = $parser->Parse('myxls.xls'); # Get the first worksheet. my $worksheet = $workbook->worksheet("myxls"); my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); $worksheet->AddCell( $row_max+1, 0, '10.1' ); $worksheet->AddCell( $row_max+1, 1, '10.2' ); $worksheet->AddCell( $row_max+1, 2, '10.3' ); $worksheet->AddCell( $row_max+1, 3, '10.4' ); $worksheet->AddCell( $row_max+1, 4, '10.5' ); $worksheet->AddCell( $row_max+1, 5, '10.6' ); $worksheet->AddCell( $row_max+1, 6, '10.7' ); $worksheet->AddCell( $row_max+1, 7, '10.8' ); $worksheet->AddCell( $row_max+1, 8, '10.9' ); $workbook->SaveAs('myxls.xls'); ## Excel Read $parser = Spreadsheet::ParseExcel::SaveParser->new(); my $workbook = $parser->Parse('myxls.xls'); # Get the first worksheet. my $worksheet = $workbook->worksheet("myxls"); my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); for($r=$row_min; $r<=$row_max; $r++){ for($c=$col_min; $c<=$col_max; $c++){ $d = $worksheet->get_cell( $r, $c); if(defined $d) { $d= $d +->unformatted(); print "-$d-\t";} } print "\n"; }

    Result : -10.0999999999999996- -10.1999999999999993- -10.3000000000000007- -10.4000000000000004- -10.5- -10.5999999999999996- -10.6999999999999993- -10.8000000000000007- -10.9000000000000004- I don't understand why this extra decimal numbers. When i open excel and check, this extra decimals are not there. Please help me

How to deliver a perl application to a client?
5 direct replies — Read more / Contribute
by xcodejoy
on Oct 20, 2019 at 12:55

    Hi there.

    Can someone explain, how to deliver some perl application to a client? For example, my client has own VPS. Should I write instruction about installing "perlbrew", "cpanm", etc...? How to create equal testing area on dev machine and on client's machine? Is there the best way to deliver an application to production?

why package definition order affect the available of package variable
2 direct replies — Read more / Contribute
by fanasy
on Oct 20, 2019 at 06:06

    I can get the $testBegin::global but I cannot get the $testEnd::global
    my question is that
    it seems the test package definition location is the key for this issue.
    how perl consider the different order ? why perl can call testEnd::show_me() in main but can't see the $testEnd::global in main ?
    there is the code :

    #!/usr/bin/perl use feature qw/say/; use Data::Dumper; package testBegin { $global = "I'm in the testBegin package global version"; sub show_me { print "$tag: testBegin package $global\n"; } } testBegin::show_me(); print "-"x60; print "\n"; testEnd::show_me(); package testEnd { $global = "I'm in the testEnd package global version"; sub show_me { print "$tag: testEnd package $global\n"; } }

    output:
    : testBegin package I'm in the testBegin package global version
    ------------------------------------------------------------
    : testEnd package

cpanm says OK but module not really installed
4 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 18, 2019 at 21:55
    I installed a cpan module with cpanm which reports: 1 distribution installed. All I can find is an install.json and an empty .packlist. Also "perllocal.pod" is appended! I browsed the module files on cpan but can't figure out why this happens with Astro::SpaceElevator (and its spiffy utf8 source: my $Δ = $P - $V).
Force Getopt::Long to Require ONLY Equals Sign Between Option Name and Value
3 direct replies — Read more / Contribute
by roho
on Oct 18, 2019 at 16:36
    Here is what I am trying to do. I have a command line option that is defined in Getopt::Long as having an optional string value as follows:

    use Getopt::Long; GetOptions( "ot:s" => \$ot ) or die("Error in command line arguments\n +");

    The problem occurs when -ot (without a value) is followed by either another option or a program argument. The -ot option will take the next word as its value.

    I know I can signal the end of options by using -- on the command line, but for backward compatibility before this program used Getopt::Long (the program previously used "/usr/bin/perl -s" to process command line options), I would like to tell Getopt::Long to always require an equals sign before a value so that -ot (without a value) can be followed by a program argument without the invervening -- in the command line.

    Here is what I am aiming for:

    Scenario #1:

    myprog.pl -ot=123 arg1 arg2 ...

    This works fine. Variable $ot='123'

    Scenario #2:

    myprog.pl -ot arg1 arg2 ...

    I need $ot to be set to the empty string '' instead of 'arg1', without the need of an intervening -- on the command line (hence the need to force Getopt::Long to require an equals sign between the option name and its value).

    Again, my goal is backward compatibility with the way this program was called before adding Getopt::Long, because this program is very central to the system and is literally called in thousands of places. TIA for any suggestions.

    "It's not how hard you work, it's how much you get done."

SOAP::Lite encoding mess?
2 direct replies — Read more / Contribute
by nikosv
on Oct 18, 2019 at 08:02
    I've got the same CGI script running on two separate servers.

    Server A specs are :
    Centos 6.5
    Apache/2.2.15 (Unix)
    perl5 (revision 5 version 10 subversion 1)
    SOAP::Lite 1.27

    Server B specs are :
    Centos 5.6
    Apache/2.2.3
    perl5 (revision 5 version 8 subversion 8)
    SOAP::Lite 0.714

    The CGI script is a very simple test.cgi :
    #!/usr/bin/perl
    
    use SOAP::Transport::HTTP;
    
    SOAP::Transport::HTTP::CGI->dispatch_to('test')->handle( );
    
    package test;
    
    sub select {
    
    
    my $class = shift;
    my $a1 	= shift;
    my $a2 	= shift;
    my $a3	= shift;
    my $a4  = shift;
    my $a5 	= shift;
    my $a6	= shift;
    my $a7	= shift;
    my $a8	= shift;
    my $a9  = shift;
    my $a10	= shift;
    my $a11	= shift;
    my $a12	= shift;
    my $a13 = shift;
    my $a14 = shift;
    my $a15	= shift;
    
     return [$a1.$a2];
    
    }
    
    

    the client calling the script is :
    use SOAP::Lite +trace;
    
    my $soap = SOAP::Lite->uri("http://192.168.1.262/test")->
    proxy("http://192.168.1.262/cgi-bin/test.cgi")->
    select("16/09/2019 17:04:00","16/09/2019","13/09/2019 13:44:09","Α","B",1,"s","e","g","x","x","748",3519999,4015,2)->result;
    
    print "\n";
    print "result is ", @{$soap};
    

    Note that parameter "A" is in Greek the rest are in English.Both servers have as locale LANG=el_GR.ISO8859-7

    When the client calls the script on Server A, all the parameters consumed by test.cgi have the UTF8 flag on,except Greek "A".(Dumped with Devel::Peek)
    SV = PV(0x1f243c8) at 0x1b37880
      REFCNT = 1
      FLAGS = (POK,pPOK,UTF8)
      PV = 0x1f3cef0 "16/09/2019 17:04:00"\0 UTF8 "16/09/2019 17:04:00"
      CUR = 19
      LEN = 24
    $VAR1 = \'16/09/2019 17:04:00';
    SV = PV(0x1f243c8) at 0x1b37880
      REFCNT = 1
      FLAGS = (POK,pPOK,UTF8)
      PV = 0x1f3cef0 "16/09/2019"\0 UTF8 "16/09/2019"
      CUR = 10
      LEN = 24
    $VAR1 = \'16/09/2019';
    SV = PV(0x1f243c8) at 0x1b37880
      REFCNT = 1
      FLAGS = (POK,pPOK,UTF8)
      PV = 0x1f3cef0 "13/09/2019 13:44:09"\0 UTF8 "13/09/2019 13:44:09"
      CUR = 19
      LEN = 24
    $VAR1 = \'13/09/2019 13:44:09';
    SV = PV(0x1f243c8) at 0x1b37880
      REFCNT = 1
      FLAGS = (POK,pPOK)
      PV = 0x1f3cef0 "\301"\0
      CUR = 1
      LEN = 24
    $VAR1 = \'Α';
    SV = PV(0x1f243c8) at 0x1b37880
      REFCNT = 1
      FLAGS = (POK,pPOK,UTF8)
      PV = 0x1f3cef0 "B"\0 UTF8 "B"
      CUR = 1
      LEN = 24
    $VAR1 = \'B';
    SV = PV(0x1f243c8) at 0x1b37880
      REFCNT = 1
      FLAGS = (POK,pPOK,UTF8)
      PV = 0x1f3cef0 "1"\0 UTF8 "1"
      CUR = 1
      LEN = 24
    $VAR1 = \'1';
    SV = PV(0x1f243c8) at 0x1b37880
      REFCNT = 1
      FLAGS = (POK,pPOK,UTF8)
      PV = 0x1f3cef0 "s"\0 UTF8 "s"
      CUR = 1
      LEN = 24
    $VAR1 = \'s';
    SV = PV(0x1f243c8) at 0x1b37880
      REFCNT = 1
      FLAGS = (POK,pPOK,UTF8)
      PV = 0x1f3cef0 "e"\0 UTF8 "e"
      CUR = 1
      LEN = 24
    $VAR1 = \'e';
    SV = PV(0x1f243c8) at 0x1b37880
      REFCNT = 1
      FLAGS = (POK,pPOK,UTF8)
      PV = 0x1f3cef0 "g"\0 UTF8 "g"
      CUR = 1
      LEN = 24
    $VAR1 = \'g';
    SV = PV(0x1f243c8) at 0x1b37880
      REFCNT = 1
      FLAGS = (POK,pPOK,UTF8)
      PV = 0x1f3cef0 "x"\0 UTF8 "x"
      CUR = 1
      LEN = 24
    $VAR1 = \'x';
    SV = PV(0x1f243c8) at 0x1b37880
      REFCNT = 1
      FLAGS = (POK,pPOK,UTF8)
      PV = 0x1f3cef0 "x"\0 UTF8 "x"
      CUR = 1
      LEN = 24
    $VAR1 = \'x';
    SV = PV(0x1f243c8) at 0x1b37880
      REFCNT = 1
      FLAGS = (POK,pPOK,UTF8)
      PV = 0x1f3cef0 "748"\0 UTF8 "748"
      CUR = 3
      LEN = 24
    $VAR1 = \'748';
    SV = PV(0x1f243c8) at 0x1b37880
      REFCNT = 1
      FLAGS = (POK,pPOK,UTF8)
      PV = 0x1f3cef0 "3519999"\0 UTF8 "3519999"
      CUR = 7
      LEN = 24
    $VAR1 = \'3519999';
    SV = PV(0x1f243c8) at 0x1b37880
      REFCNT = 1
      FLAGS = (POK,pPOK,UTF8)
      PV = 0x1f3cef0 "4015"\0 UTF8 "4015"
      CUR = 4
      LEN = 24
    $VAR1 = \'4015';
    SV = PV(0x1f243c8) at 0x1b37880
      REFCNT = 1
      FLAGS = (POK,pPOK,UTF8)
      PV = 0x1f3cef0 "2"\0 UTF8 "2"
      CUR = 1
      LEN = 24
    $VAR1 = \'2';
    
    On the contrary when the client calls the script on Server B, all the parameters consumed by test.cgi have the UTF8 flag OFF,including Greek "A"
    SV = PV(0x12399a50) at 0x12036080
      REFCNT = 1
      FLAGS = (POK,pPOK)
      PV = 0x123a07e0 "16/09/2019 17:04:00"\0
      CUR = 19
      LEN = 24
    $VAR1 = \'16/09/2019 17:04:00';
    SV = PV(0x12399a50) at 0x12036080
      REFCNT = 1
      FLAGS = (POK,pPOK)
      PV = 0x123a07e0 "16/09/2019"\0
      CUR = 10
      LEN = 24
    $VAR1 = \'16/09/2019';
    SV = PV(0x12399a50) at 0x12036080
      REFCNT = 1
      FLAGS = (POK,pPOK)
      PV = 0x123a07e0 "13/09/2019 13:44:09"\0
      CUR = 19
      LEN = 24
    $VAR1 = \'13/09/2019 13:44:09';
    SV = PV(0x12399a50) at 0x12036080
      REFCNT = 1
      FLAGS = (POK,pPOK)
      PV = 0x123a07e0 "\301"\0
      CUR = 1
      LEN = 24
    $VAR1 = \'Α';
    SV = PV(0x12399a50) at 0x12036080
      REFCNT = 1
      FLAGS = (POK,pPOK)
      PV = 0x123a07e0 "B"\0
      CUR = 1
      LEN = 24
    $VAR1 = \'B';
    SV = PV(0x12399a50) at 0x12036080
      REFCNT = 1
      FLAGS = (POK,pPOK)
      PV = 0x123a07e0 "1"\0
      CUR = 1
      LEN = 24
    $VAR1 = \'1';
    SV = PV(0x12399a50) at 0x12036080
      REFCNT = 1
      FLAGS = (POK,pPOK)
      PV = 0x123a07e0 "s"\0
      CUR = 1
      LEN = 24
    $VAR1 = \'s';
    SV = PV(0x12399a50) at 0x12036080
      REFCNT = 1
      FLAGS = (POK,pPOK)
      PV = 0x123a07e0 "e"\0
      CUR = 1
      LEN = 24
    $VAR1 = \'e';
    SV = PV(0x12399a50) at 0x12036080
      REFCNT = 1
      FLAGS = (POK,pPOK)
      PV = 0x123a07e0 "g"\0
      CUR = 1
      LEN = 24
    $VAR1 = \'g';
    SV = PV(0x12399a50) at 0x12036080
      REFCNT = 1
      FLAGS = (POK,pPOK)
      PV = 0x123a07e0 "x"\0
      CUR = 1
      LEN = 24
    $VAR1 = \'x';
    SV = PV(0x12399a50) at 0x12036080
      REFCNT = 1
      FLAGS = (POK,pPOK)
      PV = 0x123a07e0 "x"\0
      CUR = 1
      LEN = 24
    $VAR1 = \'x';
    SV = PV(0x12399a50) at 0x12036080
      REFCNT = 1
      FLAGS = (POK,pPOK)
      PV = 0x123a07e0 "748"\0
      CUR = 3
      LEN = 24
    $VAR1 = \'748';
    SV = PV(0x12399a50) at 0x12036080
      REFCNT = 1
      FLAGS = (POK,pPOK)
      PV = 0x123a07e0 "3519999"\0
      CUR = 7
      LEN = 24
    $VAR1 = \'3519999';
    SV = PV(0x12399a50) at 0x12036080
      REFCNT = 1
      FLAGS = (POK,pPOK)
      PV = 0x123a07e0 "4015"\0
      CUR = 4
      LEN = 24
    $VAR1 = \'4015';
    SV = PV(0x12399a50) at 0x12036080
      REFCNT = 1
      FLAGS = (POK,pPOK)
      PV = 0x123a07e0 "2"\0
      CUR = 1
      LEN = 24
    $VAR1 = \'2';
    

    I have tried several scenarios and tests and can't find out why this is happening. Maybe some change in Perl/SOAP::Lite ?

eval: Why use the 'Zombie error' idiom?
5 direct replies — Read more / Contribute
by DouglasDD
on Oct 18, 2019 at 05:38
    A common idiom for eval is
    eval { do_something_that_might_die(); 1; } or do { my $error = $@ || 'Zombie error'; deal_w($error) || die; }

    Is that because $@ might contain something "false-y" (like 0, '0', "", undef)?
    (If so, who cares? Why would I care if the "reason string" is false-y or not?)

    Or is it a hold-over from bugs in ancient versions of perl?
    If so, how old? if we already require versions greater than $v can we dispense with it?

    ((Sorry monks, I'm sure this has been asked before, but searches like "perl eval zombie error" and the like are mostly about zombie processes and code that already contains that boilerplate.))

    Edit: I Forgot to mention that our local coding standards discourage Try::Tiny apparently because it's edge cases have been tripped over in the past.


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 romping around the Monastery: (6)
    As of 2019-10-23 23:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?