Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

The Monastery Gates

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

Donations gladly accepted

If you're new here please read PerlMonks FAQ
and Create a new user.

New 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).

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.
Undefined value error
1 direct reply — Read more / Contribute
by clarkmurph
on Feb 21, 2018 at 12:54

    Hello, I get the following error when I execute the below script "Can't call method "mail" on an undefined value at ./recalert.plx line 24, <RECCHK> line 9." I did not write the original perl script, I just modified the email addresses to work in my environment, and the email addresses in the below script are sanitized. The script has worked for about three years without issue, I really have no perl experience, and not sure what the error means, or how to fix it/ Please help

    #!/usr/bin/perl -w $from = ''; $to = ''; $servername = ''; # $user ='username'; # $pwd ='password'; open RECCHK, "/tmp/statusmon.eml"; @recchkvar = <RECCHK>; use Sys::Hostname; my $host = hostname(); my $host1 = uc($host); open STDERR, '>&STDOUT'; #redirect STDERR output to STDOUT use Net::SMTP; $smtp = Net::SMTP->new("$servername", Debug => 1); ### Enable below if authentication required and set parameters above # $smtp->datasend("AUTH LOGIN\n"); # $smtp->datasend("$user\n"); # $smtp->datasend("$pwd\n"); sleep(2); $smtp->mail("$from"); $smtp->to("$to"); $smtp->data; $smtp->datasend("Subject: 79XX/69XXC/WB3 InfiniStream $host1 has an is +sue, and requires investigation \n"); foreach $line (@recchkvar) { $smtp->datasend("$line\n"); } close RECCHK; $smtp->dataend; $smtp->quit
Make a CPAN module
4 direct replies — Read more / Contribute
by mpersico
on Feb 21, 2018 at 09:42
Counting PDL vectors in a PDL matrix
3 direct replies — Read more / Contribute
by mxb
on Feb 20, 2018 at 04:19

    Hi all, I am currently learning PDL and I've come up against a situation for which I cannot figure out how to proceed. I have a 2D array, comprised of multiple 1D byte vectors which may or may not be unique. I wish to count the number of unique vectors within the array - essentially a histogram of vector counts. My data is too big for here, but the following example demonstrates the issue I am having.

    pdl> p $x [ [0 1 2] [3 4 5] [6 7 8] [0 1 2] [0 1 2] [6 7 8] ]

    I know I can retrieve a list of unique elements:

    pdl> p uniq $x [0 1 2 3 4 5 6 7 8]

    I know I can retrieve a histogram of all elements:

    pdl> p scalar hist $x,0,256,1 [3 3 3 1 1 1 2 2 2 0 0 0 0 0 ....]

    What I would like is something like the following output:

    [0 1 2] 3 [3 4 5] 1 [6 7 8] 2
    I think my issue stems from the fact that PDL is designed for operations on PDL elements. I am currently contemplating if the best solution to my problem is to have a lookup table of the data I am putting in vectors to an index, for example:
    my %lookup = ( 0 => "0 1 2", 1 => "3 4 5", 2 => "6 7 8", ); # Then $x reduces down to $x = [ 0 1 2 0 0 2 ];

    Any advice would be welcome, as both PDL and numerical computing are new to me, thanks!

Date::Parse - how to correctly parse dates between 1901 and 1969
6 direct replies — Read more / Contribute
by eniad
on Feb 19, 2018 at 14:29

    I am parsing dates and datetimes input by users who aren't too careful with their formatting. Date::Parse seems great because it handles most cases I need to handle.

    Except datetimes between 1901-01-01 00:00:00 and 1968-12-31 23:59:59, as I found out today. For those datetimes, Date::Parse str2time adds an extra 100 years when it parses the datetime to epoch time.

    Here is the code I am using to parse the datetimes:

    #!/usr/bin/perl #--------------------------------------------------------------------- # # # format variable date inputs #--------------------------------------------------------------------- use strict; use warnings; use Date::Parse; use DateTime; my $DEFAULT_TIME_ZONE = "GMT"; my @dates = ( "1899-06-24 09:44:00", "1900-12-31 23:59:59", "1901-01-01 00:00:00", "1960-12-31 23:59:59", "1966-06-24 09:44:00", "1968-12-31 23:59:59", "1969-01-01 00:00:00", "1969-12-31 23:59:59", "1970-01-01 00:00:01", "2000-01-01 00:00:00", "2017-06-24 23:59:59", "2018-06-24 09:44:00", "2238-06-24 09:44:00" ); foreach my $string (@dates) { # format datetime field from any valid datetime input # default time zone is used if timezone is not included in string my $epoch = str2time( $string, $DEFAULT_TIME_ZONE ); # error if date is not correctly parsed if ( !$epoch ) { die("ERROR ====> invalid datetime ($string), " . "datetime format should be YYYY-MM-DD HH:MM:SS"); } my $date = DateTime->from_epoch( epoch => $epoch ); printf( "formatting datetime: value = %20s, epoch = %20u, " . "date = %20s\n", $string, $epoch, $date ); } exit 0;

    Side note: I need to improve my error handling because the valid date 1970-01-01 00:00:00 will throw an error.

    The additional 100 years for dates between 1901 and 1969 can be seen in the output:

    formatting datetime: value = 1899-06-24 09:44:00, epoch = 18446744071 +484095456, date = 1899-06-24T09:44:00 formatting datetime: value = 1900-12-31 23:59:59, epoch = 18446744071 +532098815, date = 1900-12-31T23:59:59 formatting datetime: value = 1901-01-01 00:00:00, epoch = +978307200, date = 2001-01-01T00:00:00 formatting datetime: value = 1960-12-31 23:59:59, epoch = 2 +871763199, date = 2060-12-31T23:59:59 formatting datetime: value = 1966-06-24 09:44:00, epoch = 3 +044598240, date = 2066-06-24T09:44:00 formatting datetime: value = 1968-12-31 23:59:59, epoch = 3 +124223999, date = 2068-12-31T23:59:59 formatting datetime: value = 1969-01-01 00:00:00, epoch = 18446744073 +678015616, date = 1969-01-01T00:00:00 formatting datetime: value = 1969-12-31 23:59:59, epoch = 18446744073 +709551615, date = 1969-12-31T23:59:59 formatting datetime: value = 1970-01-01 00:00:01, epoch = + 1, date = 1970-01-01T00:00:01 formatting datetime: value = 2000-01-01 00:00:00, epoch = +946684800, date = 2000-01-01T00:00:00 formatting datetime: value = 2017-06-24 23:59:59, epoch = 1 +498348799, date = 2017-06-24T23:59:59 formatting datetime: value = 2018-06-24 09:44:00, epoch = 1 +529833440, date = 2018-06-24T09:44:00 formatting datetime: value = 2238-06-24 09:44:00, epoch = 8 +472332640, date = 2238-06-24T09:44:00

    The Date::Parse documentation suggests it can handle dates at least as old at 1901-01-01. The Time::Local documentation suggest it should be able handle dates even older.

    How should I handle this oddity? Is there a better way to parse variable input formats?

Failed to find share dir for dist 'Dancer2'
2 direct replies — Read more / Contribute
by markong
on Feb 19, 2018 at 07:13
    Hello, I've cloned the Dancer2 official git repo and now I'm trying to setup a dev environment (inside a perlbrew local lib) and I'm stuck at this command, receiving back:
    Failed to find share dir for dist 'Dancer2' at lib/Dancer2/CLI/Command/ line 62.
    which I guess is to be expected seen that I haven't installed the 'Dancer2' dist from CPAN and File::ShareDir::dist_dir('Dancer2') will fail.

    I'm trying to execute the dancer script from the cloned checkout code base, in order to setup a test dancer2 project, but it seems that it is not something supported?!

    I'm wondering if anybody has experience with the code base and can suggest something. Is the Dancer2 distro installation from CPAN mandatory to execute the 'dancer2' script or else how do you call that 'script/dancer2' bin around that share dir check while developing?

creating and managing many hashes
7 direct replies — Read more / Contribute
by Gtforce
on Feb 18, 2018 at 02:18

    My data is as follows:

    2018-01-01 apple 200 50000 2018-01-02 apple 201 60000 2018-01-03 apple 202 70000 2018-01-04 apple 198 80000 2018-01-01 orange 400 30000 2018-01-02 orange 401 35000 2018-01-03 orange 402 36000 2018-01-04 orange 405 28000

    where apple and orange are my products, and I have over 2,000 distinct products. The values 201,202, etc are the prices of those products on the respective dates represented in the first column (and the quantities aka inventories are in the last column). My data series is for the past 3 years for each of these products.

    I need to pass the data series (i.e., price and inventory) for one product at a time to a subroutine that calculates the mean,, etc.

    I also need to pass the data series for a combination of two products at a time (a pair) to a subroutine that calculates the correlation between the two data series.

    I have currently done this using arrays over the 2 million pairs that arise from 2,000 products and it ran for 4 days before my patience ran out and I terminated the process. I've only just started reading up on hashes and I think I can speed up things if I get the data series for one product into one hash, and the data series for another product into another hash (likewise for 2,000 products or hashes).

    The reading I've done so far warns me against using variables for hashnames. Any advice you can offer would be greatly appreciated, thanks.

New Meditations
Revitalising your own old posts
5 direct replies — Read more / Contribute
by stevieb
on Feb 21, 2018 at 19:32

    Periodically, I'll go through my posts via the normal mechanism and sort by lowest-highest-this-that-other, and sometimes, like today, I have found one that I'd like to reply to.

    Now, this post has a (relatively) high XP count, and the responses have 15-25% higher than that.

    I want to reply to a poster on such thread legitimately (orig post count was ~45, replies were 60+), but I don't want it realized that I'm doing it in order to get exposure on the overall hierarchical post.

    How do Monks handle these situations? Shatter humility or what?


    ps. I have been accused of raising posts for XP+ before, but those who raised that are irrelevant to me.

Sum to 100 at Rosetta Code
2 direct replies — Read more / Contribute
by choroba
on Feb 17, 2018 at 12:52
    After a long time, I checked the list of tasks not implemented in Perl on RosettaCode. One of them was "Sum to 100", kind of similar to mjd's Simple but difficult arithmetic puzzle:

    In the string 123456789, you can prepend + or - before any digit to form an expression. You should

    • list all the possible expressions that evaluate to 100
    • show the number that is a result of the maximal number of expressions
    • show the lowest positive number that can't be expressed
    • show the ten highest numbers that can be expressed

    Here's my solution:

    I tried to avoid eval to evaluate the expressions, at the same time, I didn't want to implement the traditional full math expression parser as there were only two operations of the same precedence in use.

    $sum += $_ for $expression =~ /([-+]?[0-9]+)/g;

    Feel free to comment on perlishness, effectiveness, golfness, or beauty of the solution, or propose your own.

    Note: Those interested in Perl 6 can read the solution just below mine.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
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 avoiding work at the Monastery: (2)
As of 2018-02-23 06:43 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (300 votes). Check out past polls.