Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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
How CGI works
2 direct replies — Read more / Contribute
by Sarat1729
on Feb 22, 2018 at 09:55
    Hi Monks, I am new to CGI scripting but need to understand the code in our project. I am unsure what is the starting point and where to start and how it works. Can someone please explain how it works.
Data to fetch from Database to display in graph
2 direct replies — Read more / Contribute
by Sarat1729
on Feb 22, 2018 at 09:52
    Hi Monks, I am trying to display certain data fetched from database (using Select from a SQL Server DB) in a graph. I tried to achieve the same using GD::Graph with normal sample data (not from database though), but not sure how I can make it using the data from Database. Can someone help? Also, I want to use 2 vertical axis on a single horizontal axis on either side with different values and if one uses histograms, the other should use lines (Seems messy, but that's ok for me).
Learning/Trying to use Net::Curl with Cookie Jar
2 direct replies — Read more / Contribute
by YenForYang
on Feb 22, 2018 at 08:37

    I've never used libcurl before, but I wanted to start using it in curl for improved HTTP performance. I've been looking through all the Net::Curl::Easy:: keys, browsing the contents and comparing with and noticed that Net::Curl doesn't have a curl_easy_cleanup function.

    I'm asking this because on the manual page for CURLOPT_COOKIEJAR, it says

    ...will make libcurl write all internally known cookies to the specified file when curl_easy_cleanup is called.

    So...How does one store cookies in a cookie jar (file) by specifying CURLOPT_COOKIEJAR?

    EDIT: To give an idea of what I'm trying to do for now (this case): I'm making a simple GET request pretty much equivalent to this: curl -qsL -b cookies.txt -c cookies.txt <URL> and parse the html response content (html of URL).

OT: Converting some js to Perl
4 direct replies — Read more / Contribute
by BrowserUk
on Feb 22, 2018 at 07:16

    I'm converting some js, and have come across numerous examples of this construct:

    if(function(){return a.indexOf(Lb)!=-1 }())return Mb;
    I can convert that to:
    if( sub{ return index( $a, $lb ) != -1 }->() ) return $Mb;

    But this achieves the same thing (in Perl):

    if( index( $a, $lb ) != -1 ) return $Mb;

    The question(aimed at js progs): Is there anything inherent in the js code that my second Perl versions misses?

    Or is this just a case of someone defining an inString() function, that then got inlined, but not unwrapped?

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
Need Convert::ASN1 library encoded text in uper format
1 direct reply — Read more / Contribute
by santosh_vjit
on Feb 22, 2018 at 07:03
    Hi Friends,

    I have requirement to encode the below sequence structure with values to uper encoded rule and pass the string to other class. But using Convert::ASN1 library im able to encode only in ber/der format. Can any one of you please guide how to encode below input in uper encoding format and capture that encoded string in a variable. P.S: If require i can provide ASN1 structure of input.

    #!/usr/bin/perl -w use Data::Dumper; use Getopt::Std; use Convert::ASN1 qw(:io :debug); getopts('s:D', \%opts); if (!defined($opts{'s'})) { print STDERR "Missing ASN.1 Specification file!\n"; HELP_MESSAGE(); exit 1; } $specfile = $opts{'s'}; $DEBUG = $opts{'D'}; $asn = Convert::ASN1->new(encoding=>'DER'); $r = $asn->prepare_file($specfile); #print Dumper($r); if (!defined($r)) { print "ERROR: " . $asn->error . "\n"; } $securityModeError = $asn->find("ATCmessageheader"); $pdu = $securityModeError->encode( msgidentificationnumber => 1, msgreferencenumber =>1, timestamp =>{timehours=>12, timeminutes=>07, timeseconds=>50} ); print "Encode : pdu ". Dumper($pdu), "\n"; print "Decode: pdu ". Dumper($securityModeError->decode($pdu)). "\n";
Size of the hash value
5 direct replies — Read more / Contribute
by harishnv
on Feb 22, 2018 at 01:44
    __DATA__ (key, value) format test1= 1 2 3 test2= 4 5 6 _output_ 3 3

    how to get the size of the value of each key? I have data in %hash

Why use warnings? -w
9 direct replies — Read more / Contribute
by harangzsolt33
on Feb 21, 2018 at 23:56
    What do the "use warnings;" and "use strict;" statement do exactly? So far I have noticed that when I try to use an uninitialized variable, it gives me a compilation error. That's NO BIG DEAL, I think. I mean, we're told in Perl tutorials, that we don't have to initialize variables in Perl. We can just start using them. Right? Just like in JavaScript or QBASIC.

    So, WHAT EXACTLY HAPPENS IF I START USING UNINITIALIZED VARIABLES IN PERL? And why are people saying that we shouldn't get in the habit of doing this? In JavaScript, an uninitialized variable automatically becomes a global variable when we use it. So, if I refer to A in my function, then when I type "A = 5" it becomes a global variable. But if I initialize it as "var A = 5" then it becomes a temporary variable that exists only within the scope of that function. Is Perl treating variables the same way?

    What else happens if I quit using the "warnings" and "strict"? Is there any other trouble that may arise? ARE MY PERL SCRIPTS GOING TO RUN SLIGHTLY FASTER IF I DON'T USE STRICT AND WARNINGS? I am just trying to think of all the pros and cons, but so far all I've heard is that you got to use these two. But I don't know why. I want to understand the whys.

variables passed from main to package
1 direct reply — Read more / Contribute
by FryingFinn
on Feb 21, 2018 at 20:38
    I trying to get an application to set a couple of Global (?) variable that will be used in package subroutines. I thought I could use  $main::DEBUG my test main is
    #!/usr/bin/perl ## this is perl5.16 my $DEBUG = 1233; my $VERBOSE=5; use strict; use warnings; use Getopt::Long; my $Usage = "this is Usage"; Getopt::Long::GetOptions( 'd=i' => \$DEBUG, 'v=i' => \$VERBOSE ) or die "Incorrect u +sage! $Usage\n"; print "in MAIN DEBUG is $DEBUG \n"; print "In Main VERBOSE is $VERBOSE \n"; use lib "../lib"; use myApp; myApp::testprint(); myApp::testprint2();
    The code for is
    package myApp; my $DEBUG = $main::DEBUG; my $VERBOSE = $main::VERBOSE; print " has DEBUG [$DEBUG]\n"; print " has VERBOSE [$VERBOSE]\n"; 1; sub testprint { print "in myApp::testprint DEBUG is [$DEBUG]\n"; print "in myApp::testprint VERBOSE is [$VERBOSE]\n"; } sub testprint2 { my $DEBUG = $main::DEBUG; my $VERBOSE = $main::VERBOSE; print "in myApp::testprint2 DEBUG is [$DEBUG]\n"; print "in myApp::testprint2 VERBOSE is [$VERBOSE]\n"; }
    The results.
    ./xVERBOSE has DEBUG [] has VERBOSE [] in MAIN DEBUG is 1233 In Main VERBOSE is 5 in myApp::testprint DEBUG is [] in myApp::testprint VERBOSE is [] in myApp::testprint2 DEBUG is [] in myApp::testprint2 VERBOSE is []
    any help would be appreciated tks gerry
Strings/Numbers aren't equating properly
2 direct replies — Read more / Contribute
by M4ver1k
on Feb 21, 2018 at 18:29

    Edit: This is fixed. It was my fault...Flawed logic, it took time to work out. Explanation at the bottom of this post.

    I'm trying to compare results from an LDAP server to what lives on a MySQL server. Overall it works, right now I'm trying to optimize a part of it. I took the results of an LDAP query returning a single string value to an array '@ldap' which I then sorted alphabetically.

    Now I'm doing a search in MySQL, and as I iterate through the results, I'm comparing one column to the '@ldap' array and then I remove any matches out of '@ldap'. The problem is that sometimes, even though strings appear identical, it doesn't count it as a match and so fails to remove the match out of '@ldap', which creates redundant work at the end of the script.

    Here's the problematic code:
    $sth = $dbh->prepare("select * from table order by mid"); # So +rting alphabetically $sth->execute(); while(my $info = $sth->fetchrow_hashref){ my $count = 0; my $mid = $$info{'mid'}; chomp($mid); my $num = 0; if ($mid =~ /^[0-9]+$/){ # Checks to see if string hap +pens to be only numbers print fh "$mid is a number\n"; $num = 1; # If so, change value } else { print fh "$mid is a string\n"; # Prints to a t +ext file for me to look at. } print "$mid - \$num = $num\n" if ($debug > 0); foreach my $id (@ldap){ chomp($id); # Check to see if we've found a correlating LD +AP entry from MySQL if ($num == 1){ print fh "$mid - $id - Equating as numbers.\n"; last if ($count > 15); if ($mid == $id){ # Check type to see if we have + data on it if ($$info{'type'} ne 'NA'){ # If we do, then remov +e this ID from the list to retry splice @ldap, $count, +1; last; } } } else { print fh "$mid - $id - Equating as strings.\n"; last if ($count > 15); # To drastically shorten during troubleshooting if ($mid eq $id){ # Check type to see if we have + data on it if ($$info{'type'} ne 'NA'){ # If we do, then remov +e this ID from the list to retry splice @ldap, $count, +1; last; } } } $count++; } } $sth->finish;

    At first I thought it was because sometimes the strings contain only numbers, so I decided to check if they do contain only numbers, and if so, compare using '==' instead of 'eq'. I also was expecting to be able to use just $$info{'mid'} directly for comparison, so I did my $mid = $$info{'mid'}; for the sake of troubleshooting.

    In the file output, regardless of if they equate as strings or numbers, sometimes it just doesn't match when it looks like it should. Now I'm stumped. I'd be grateful for help, thank you!

    Edit: It's working now. My flawed logic was here:

    if ($$info{'type'} ne 'NA'){ # If we do, then remove this ID from the list to retry splice @ldap, $count, 1; last; }

    I ended up having a lot more entries of type 'NA' than I was expecting, and due to work later in the script it ended up making more sense to put those entries into another array to separate out the work. So I changed the logic to this:

    if ($$info{'type'} eq 'NA'){ push(@file, $id); } # If we do, then remove this UID from the list to retry splice @ldap, $count, 1; last;
    I appreciate everyone's time, thank you.
How to obtain perl5/auto/IO/ with global symbols?
3 direct replies — Read more / Contribute
by ek15
on Feb 21, 2018 at 18:25

    Hi all,

    We have received two items from Company A relating to a piece of software: a source code package and an executable (created with PAR Packer). I have added to the source code and packaged everything with PAR Packer to create a new executable. Now, I am getting the error below. I have found that the in my PAR-Packed executable has no global symbols when I check it with 'nm -g'. On the other hand, the found in the executable from Company A does have global symbols (including the missing boot_IO symbol) when I check it with 'nm -g'.

    The perl-5.16.3 RPM which I have installed through yum on my dev machine will only install without the global symbols. How can I get with global symbols?

    Thanks, Erick

    [root@ektest1 DarkEther-v1.15.3]# ./switchscan local/mlnx cfgfile=192. +168.20.141.cfg Can't find 'boot_IO' symbol in /tmp/par-726f6f74/cache-b7a72e3a0413c42 +480d6dae1f533764a0bf93415/ at /usr/local/share/perl5/PAR/ line 78. Compilation failed in require at /usr/lib64/perl5/IO/ line 26 +6. BEGIN failed--compilation aborted at /usr/lib64/perl5/IO/ lin +e 266. Compilation failed in require at /usr/lib64/perl5/IO/ line +101. BEGIN failed--compilation aborted at /usr/lib64/perl5/IO/ l +ine 101. Compilation failed in require at /usr/lib64/perl5/IO/ line 133. BEGIN failed--compilation aborted at /usr/lib64/perl5/IO/ line +133. Compilation failed in require at -e line 138.

Add your question
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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (1)
    As of 2018-02-23 05:14 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (300 votes). Check out past polls.