Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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
Passing perl variable as input name?
1 direct reply — Read more / Contribute
by RobRobson
on Jul 21, 2016 at 11:26

    Hi Guys, I have been searching google for hours and can't find a solution to this problem so any help will be fantastic.

    I'm creating image buttons via a while loop as I need these buttons to be created dynamically because it pulls data from a mysql database. I need these buttons to work independently. So for example, pressing 'button1' will make 'action1' happen.

    I have currently set the input name of the image buttons to the names I am pulling from the database using the Perl variable "$unit_name". Inspecting the buttons on the website itself I can see the values are being set to the names I am expecting, and when setting the form method to GET I can see the names in the URL bar.

    When you press a button I intend for the form to submit so the name of whichever button has been pressed can be sent through as a parameter. However at the moment the variable "selectedName" receives no data.

    Here is where I pull the parameter:
    my $selectedName = param('$unit_name');
    Here is the while loop I am using to create the buttons:
    my $statement2 = "select unit_name from unitNamesTable"; my $sth = $dbh->prepare($statement2); $sth->execute(); while (my @data = $sth->fetchrow_array()) { $unit_name = $data[0]; push(@unitNames, $unit_name); print "<td>$unit_name<br>"; print "<input type=image name='$unit_name' id=$unit_name value='$u +nit_name' alt=airConButton src=../../images/unit.jpg height=100px wid +th=100px onclick=this.form.submit()>"; }
    If you need anything else or have any ideas how I can either fix this or go about it in a better way please let me know. Thanks in advance!
Average start time handling midnight
8 direct replies — Read more / Contribute
by chrisjej
on Jul 21, 2016 at 08:16

    I'm wondering if anyone has a good algorithm to work out average start time that handles times over midnight. Assume the job runs once every 24 hours and will usually start within a 6 hour window.

    Obviously you could just sum seconds since midnight / number entries, which would work well if your times were:

    11:00 and 13:00 where it would give the plausible 12:00


    23:00 and 01:00 would also give the answer 12:00 whereas 00:00 is desirable.

    If, instead, you calculated this second example on seconds since 12:00 - you would get the desired answer of 00:00. But then the first example would also give you 00:00.

    I'm thinking you could do it by doing a first pass to generate a histogram and then derive a good base time from that.

    But I was hoping someone might already have implemented or know of a solution.

Perl API version v5.18.0 of ... does not match v5.16.0 ...
3 direct replies — Read more / Contribute
by Nightgem
on Jul 21, 2016 at 05:18


    I'm having trouble getting a Perl module (File::Sync) to work:
    perl -e "use File::Sync" Perl API version v5.18.0 of File::Sync does not match v5.16.0 at /usr/ +lib64/perl5/ line 213. Compilation failed in require at -e line 1. BEGIN failed--compilation aborted at -e line 1.

    The Perl version installed on the system is v5.16.3, built for x86_64-linux-thread-multi. I was wondering if anyone here could point me in the right direction to get this fixed and working properly. The File::Sync module was installed using cpanm.

Variables are automatically rounded off in perl
5 direct replies — Read more / Contribute
by Anonymous Monk
on Jul 21, 2016 at 02:37
    $hash->{TGT}=3335.9999999999995; print $hash->{TGT}; Actual Result:- 3336 Excepted Result:- 3335.9999999999995
Data Structure
3 direct replies — Read more / Contribute
by efficasy
on Jul 20, 2016 at 17:06
    Hi, If Gurus can help me a small problem I am facing.. I have file, with a structure like below, Student Id Class John 23 2 Mark 33 3 Jerry 44 2 Sam 55 2 Tom 65 3 I want to print out a output like below, can you please help me how? Class 2 = John,Jerry, Sam Class 3 = Mark, Tom
Install and then load missing modules with in a script
1 direct reply — Read more / Contribute
by edimusrex
on Jul 20, 2016 at 14:16

    I am not sure if what I am attempting is possible but I'll ask any ways. I have a script which performs various checks. This script will be deployed on at least 6 servers. Currently I only require it to use 2 modules, DateTime and DBI; The idea is that it will first check to see if those modules are indeed installed and if they are not, install and load them. I am looking into Module::Load but not sure if it can handle what I am trying to accomplish. I'd prefer not to use a module to do this. What I currently have is the following

    #!/usr/bin/perl use warnings; use strict; my $version = `cat /etc/*-release | head -n 1`; chomp $version; my ($os) = $version =~ /(Ubuntu|CentOS)/; my %rhel = ( "DBI" => "perl-Class-DBI-mysql.noarch", "DateTime" => "perl-DateTime.x86_64", ); if ($os eq 'CentOS'){ foreach my $mod (keys %rhel){ eval { require $mod; $mod->import(); 1; } or do { system("yum install $rhel{$mod} -y"); } } }

    2 issues with this, 1 it does not actually seem to load the module and 2 I don't want the text in STDOUT from the eval.

    If any of this makes any sense and you can help I'd love the advice. Thanks in advance

    ------ UPDATE ------

    Seems like I have solved my issue. Here is what I ended up doing

    #!/usr/bin/perl use warnings; use strict; my $version = `cat /etc/*-release | head -n 1`; chomp $version; my ($os) = $version =~ /(Ubuntu|CentOS)/; my %rhel = ( "DBI" => "perl-Class-DBI-mysql.noarch", "DateTime" => "perl-DateTime.x86_64", ); if ($os eq 'CentOS'){ foreach my $mod (keys %rhel){ eval "use $mod"; if($@){ system("yum install $rhel{$mod} -y"); eval "use $mod"; } } }

    Thanks for the help

perlcritic compliant way to eval?
3 direct replies — Read more / Contribute
by KeighleHawk
on Jul 20, 2016 at 13:28

    So I've done a bad thing (TM) I am certain. I wanted to easiliy create a slew of "data files" from exisiting code that could be read back in and executed and later allow us to add many, many more test files. So I opted to use Data::Dumper to create the initial files, fixed them up a bit, then used the following to set the needed values for each test:

    { no strict 'vars'; local $/; my $in; open $in, '<', $test_env or die "..."; my $rc = eval <$in>; close $in; # set various values from that data structure. };

    The above is essentially in a loop, reading in each test data file.

    Besides the obvious of redesigning the data files as XML or some other, is there anyway to accomplish the above in a way that perlcritic is happy with? I have a bazillion files I'd rather not recreate at the moment.

solution for the problem Illegal division by zero at line 36, <DATA> line 4. at line 36
1 direct reply — Read more / Contribute
by Anonymous Monk
on Jul 20, 2016 at 13:13
    # type: perl <trace file> <required node> <granlarity> + > output file use diagnostics; $infile=$ARGV[0]; $tonode=$ARGV[1]; $granularity=$ARGV[2]; #we compute how many bytes were transmitted during time interval speci +fied #by granularity parameter in seconds $sum=0; $clock=0; open (DATA,"<$infile") || die "Can't open $infile $!"; while (<DATA>) { @x = split(' '); #column 1 is time if ($x[1]-$clock <= $granularity) { #checking if the event corresponds to a reception if ($x[0] eq 'r') { #checking if the destination corresponds to arg 1 if ($x[3] eq $tonode) { #checking if the packet type is TCP if ($x[4] eq 'tcp') { $sum=$sum+$x[5]; } } } } else { $throughput=$sum/$granularity; print STDOUT "$x[1] $throughput\n"; $clock=$clock+$granularity; $sum=0; } } $throughput=$sum/$granularity; print STDOUT "$x[1] $throughput\n"; $clock=$clock+$granularity; $sum=0; close DATA; exit(0);
How to handle modules across different versions
2 direct replies — Read more / Contribute
by ksublondie
on Jul 20, 2016 at 10:50
    I have a couple windows servers running scheduled tasks in perl. Once upon a time, they were all in perl v 5.6. Over the years, we've been slowly trying to upgrade the old scripts to and creating the new scripts in the latest and greatest version at the time. So now we're running multiple versions.

    Of course, all the common functionality (email sending, network variables, etc) are kept in common modules. Currently, we have a different for each perl version. Is there a good way to simply have one for all versions? Or is it inevitable with running different versions?

Nested div tag
2 direct replies — Read more / Contribute
by perlmad
on Jul 20, 2016 at 09:38

    Hi Monks

    I have problem in nested div tgs

    my code

    #!/usr/bin/perl use strict; use HTML::TokeParser::Simple; my $parser = HTML::TokeParser::Simple->new(handle => \*DATA); my @dnldLinks; my @month_year; my @date; my @data; my $index; my $contract; while ( my $div = $parser->get_tag('div') ) { if($div->is_start_tag('div')){ if($div->[1]{class} =~ 'Cell month-year') { my $time = $parser->get_trimmed_text; push(@month_year,$time); } if($div->[1]{class} =~ 'Cell Release Date') { my $time = $parser->get_trimmed_text; push(@date,$time); $index=$time; } if($div->[1]{class} =~ 'Mortgage Contract Rate') { my $time = $parser->get_trimmed_text; push(@date,$time); $contract=$time; } print "date : $index, data : $contract\n"; } } #use Data::Dumper; #print Dumper \@dnldLinks; __DATA__ <div class='historicalChartTable'> <div class="Row Jun-2015"> <div class="Cell month-year "> Jun-2015 </div> <div class="Cell Release D +ate "> 2015-07-30 </div> <div class="Cell Natio +nal Mortgage Contract Rate "> 3.850 </div> </div> class="Row May-2015"> <div class="Cell month-year even"> May-2015 </div> <div class="Cell Release Date even"> 2015-06-25 </div> <div class="Cell National Mortgage Contract Rate even" +> 3.750 </div> </div> </div>
    Getting Output: date : , data : date : , data : date : , data : date : 2015-07-30, data : date : 2015-07-30, data : 3.850 date : 2015-07-30, data : 3.850 date : 2015-06-25, data : 3.850 date : 2015-06-25, data : 3.750

    my code is parsing entire div tag one by one , not recursively,Kindly help me to find out the data recursively in the div tag

    Expected output: date : 2015-07-30, data : 3.850 date : 2015-06-25, data : 3.750

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 the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (8)
    As of 2016-07-25 16:19 GMT
    Find Nodes?
      Voting Booth?
      What is your favorite alternate name for a (specific) keyboard key?

      Results (224 votes). Check out past polls.