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
Using 64 Bit Perl for Production Scripts.
9 direct replies — Read more / Contribute
by pmu
on Apr 18, 2014 at 08:31

    Respected Monks,

    We're going get a new set of SAN Management Servers and I am planning to install Strawberry Perl Version 5.18.2.2 (64 Bit) on it. I googled about it and found that its often recommended to stick to 32 Bit Perl eventhough the OS is 64 Bit because 64 bit versions of some Perl Modules have issues. Is that still the case or can I go ahead using 64 Bit Perl?

    Please let me know.

    -------------------------------------------------------------- Perspectum cognitio aeterna --------------------------------------------------------------
DBD::ODBC - Why do i get an "out of memory" exception?
2 direct replies — Read more / Contribute
by karlgoethebier
on Apr 18, 2014 at 07:44

    Hi all,

    in some older code i found something like this:

    do { my @row; while (@row = $sth->fetchrow_array()) { # do stuff here } } while ( defined( $sth->{odbc_more_results}) );

    This one throws an "out of memory" exception. I don"t know who wrote it ;-)

    According to the manual of DBD::ODBC the code should be:

    do { my @row; while (@row = $sth->fetchrow_array()) { # do stuff here } } while ( $sth->{odbc_more_results} );

    This one works as expected. No need to worry - i use this code.

    But i wonder why the first example fails.

    Update: defined for ever?

    #!/usr/bin/env perl + use feature qw (say); use strict; use warnings; my $foo; ( defined $foo ) ? say qq(D'oh!) : say qq(No!); $foo = 0; ( defined $foo ) ? say qq(D'oh!) : say qq(No!);

    Thank you very much for any hint and best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Different result for 'foreach' vs 'while shift' arrayref
6 direct replies — Read more / Contribute
by gvandeweyer
on Apr 18, 2014 at 04:52
    Dear Monks,
    I having problems understand why the following two snippets give different result (the second one being correct):
    Situation: Arrayref with database rows, returned from subroutine, in my test-case 927306 rows:
    my $rowcache = runQuery($query,"Table",\@ids); print "RunQuery results: ".scalar(@$rowcache)." rows\n";

    Snippet one: Using while shift to return rows yields only 788425 resulting rows
    while (my $result = shift(@$rowcache) ) { print OUT $result->{'id'}."\t".$result->{'Name'}."\t".$result->{'Co +de'}."\t".$result->{'Date'}."\t".$result->{'Comment'}."\n"; }

    Snippet two : Using foreach returns all rows:
    foreach my $result (@$rowcache) { print OUT $result->{'id'}."\t".$result->{'Name'}."\t".$result->{'C +ode'}."\t".$result->{'Date'}."\t".$result->{'Comment'}."\n"; }

    I thought that these should be equivalent?
    Best,
    Geert
Tk::tablematrix update leaks memory
1 direct reply — Read more / Contribute
by abonvici
on Apr 18, 2014 at 03:54

    Hi all, i have a great trouble with perl 5.16.3 activestate and tk 8.4 32. I need to update a tablegrid content every one second to display a changing matrix of data. I noticed that the call to refresh the view leaks memory, ie the memory uccupied by the script grows forever. Anyway I do not destroy or delete any row or widget and reuse always the same data container structure. The script is

    use Tk; use Tk::TableMatrix; my $i=0; my $macchina_='38'; my $dbh; $mw = MainWindow->new; $mw->geometry("1000x600+0+0"); my $gridContainer={}; my ($rows,$cols) = (100, 16); foreach my $row (0..($rows-1)){ foreach my $col (0..($cols-1)){ $gridContainer->{"$row,$col"} = " "; } } my $frameFermiDelTurno= $mw->Frame()->pack(-expand=>1,-fill=>'both' +); -orient=>"hori +zontal"); my $t = $frameFermiDelTurno->Scrolled('TableMatrix', -rows => $rows, -cols => $cols, -width =>500, -height => 14, -titlerows => 1, -titlecols => 1, -variable => $gridContainer, -selectmode => 'extended', -state => 'disabled', -bg => 'white', -font=> '{arial} 16 bold', -padx=>0, -ipadx=>0, -sparsearray=>0, -scrollbars=>'se' ); $t->tagConfigure('active',-bg => 'gray90', -relief => 'sunken' +); $t->tagConfigure('title', -bg => 'gray85', -fg => 'black', -re +lief => 'sunken'); $t->colWidth(2,20); $t->colWidth(3,20); # force scrollbar refresh: $t->pack( -expand=>1,-fill => 'x' , -side=>'left', -anchor=>'w +'); my $row=0; # $gridContainer->{"$row,0"} = "ID"; # $gridContainer->{"$row,1"} = "-"; # $gridContainer->{"$row,2"} = 'STATO'; # $gridContainer->{"$row,3"} = 'TURNO INIZIO'; # $gridContainer->{"$row,4"} = 'TURNO FINE'; # $gridContainer->{"$row,5"} = 'ORA INIZIO'; # $gridContainer->{"$row,6"} = 'ORA_FINE'; # $gridContainer->{"$row,7"} = 'ODL'; # $gridContainer->{"$row,8"} = 'LOTTO'; # $gridContainer->{"$row,9"} = 'NOTE'; # $gridContainer->{"$row,10"} = 'OPERATORE'; # $gridContainer->{"$row,11"} = 'CODICE PRODOTTO'; # $gridContainer->{"$row,12"} = 'CODICE_FERMO'; # $gridContainer->{"$row,13"} = 'DATA FINE'; # $gridContainer->{"$row,14"} = 'DATA INIZIO'; # $gridContainer->{"$row,15"} = 'DESCRIZIONE'; # $mw->repeat (1000,[ \&leak ]); MainLoop; sub leak{ TMRefresh(); } ###################################################################### sub TMRefresh{ ###################################################################### # this leaks $t->see("end"); # this leaks $->see("1,1"); $t->configure(-padx =>($t->cget(-padx))); #this leaks memory too my ($rows,$cols) = (100, 16); foreach my $row (0..($rows-1)){ foreach my $col (0..($cols-1)){ $gridContainer->{"$row,$col"} = int(rand(10)); } } $mw->update; }

    Please , may you give me some advice if i am doing something wrong? I noticed that if I comment out the $t->see or $t->configure the table does not diplay anything other that column label but the memory allocated is costant. SO I think that the process of updating display has something that i don't understand ....Please help me. Thnak you a lot.

Update an XML node value based on one of the attribute values.
5 direct replies — Read more / Contribute
by amitp2011
on Apr 18, 2014 at 02:07

    I have to update the value of a node by based on the value of one of its attributes.

    <ReferenceID referenceIDType="FXDD">FXDDILN233</ReferenceID> For the above Tag. i want to edit the value FXDDILN233 based on the attribute value referenceIDType="FXDD"

    I tried the below code: <Code>

    my $nodeset = $doc->findnodes("//*name()='ReferenceID'");

    foreach my $node ($nodeset->get_nodelist) {

    if ($node->hasAttribute("referenceIDType") == XML::LibXML::Boolean->True) {

    $RetVal = $node->getAttributeNode("referenceIDType")->getValue();

    if ($RetVal eq "FDD") {

    print "Attribute VAL=@@@@@@@@@@@@"."$RetVal \n";

    $node->setData("$NodeVal");

    } } } </Code > But getting error on the setData line.. Whats is it that could be wrong ? Thanks in advance.
What are the advantages or disadvantages of "require" over "use"
8 direct replies — Read more / Contribute
by ghosh123
on Apr 18, 2014 at 01:57

    Hi ,
    I just wanted to know about the advantages and disadvantages of "require" over "use" in Perl.

    I know that :
    1. "use" is evaluated at compile time and "require" at run-time.
    2. If the module I am using with "use" has syntax error they will be thrown while compiling, on the contrary if the module is "require" ed then they will come during run-time and no errors will be reported in compile time.
    3. When we do "use", the control is passed to that file at run-time and whereas in case of "require" the code from that module/file is included to my code which is "require"ing it at run-time.
    4. If I have a module which does a lot in the BEGIN block and I don't really need it in all cases , then it is clever to "require" it so that I won't have to wait for all the initializations of that module in case I don't need it.

    My question is :
    is there any gain in terms of memory consumption when I use "require" or any other advantage ? Please let me know.
    Thanks.

Read MP3 tags
2 direct replies — Read more / Contribute
by rgammon51
on Apr 17, 2014 at 17:16

    I want to read tags in MP3 files to reorganize my music files. The general order to do this is

    If MP3Tag(album) is "Top 1000" and current directory is NOT /home/rgammon51/Temp/Music/Various THEN move file to /home/rgammon51/Temp/Music/Various/Top1000.

    Its the MP3Tag thing that I search for. General web searches do not turn up anything useful.

    I am fairly certain that I can do the rest of the file processing. I want to make this general so that "Top 1000" is passed as a parameter to the program.

    The issue is that most music managers INSIST that a single artist appears on an Album. Movie Soundtracks are just one example of where that is simply NOT true.

    Google Music ruined the organization that I want for my music files, on Google and on my computer due to this INSISTENCE on a single artist per album.
flock and read-ahead buffering on input
4 direct replies — Read more / Contribute
by dbooth
on Apr 17, 2014 at 15:54

    I am trying to figure out how flock interacts with read-ahead buffering on input, and I have not been able to find any documentation about it. Suppose I open and lock a file using flock for exclusive access, like this as apparently advised in the Perl Cookbook:

    sysopen(FH, "numfile", O_RDWR|O_CREAT) or die "can't open numfile: $!"; flock(FH, LOCK_EX) or die "can't write-lock numfile: $!"; $num = <FH> || 0;

    (I am using an exclusive lock because after reading the file, I intend to re-write it.) Between the calls of sysopen and flock, the file has been opened but has not yet been locked. Therefore, if a read-ahead buffer were filled when the file was sysopen'ed, and another process wrote to the file after that but before flock was called, then it would seem to me that the example above would be unsafe, because reading (from the read-ahead buffer) immediately after the flock could return the old file contents instead of the current contents . . . unless flock automatically invalidates the read-ahead buffer, or the read-ahead buffer is not filled until the file is actually read. But the perl documentation on sysopen, read and flock say nothing about read-ahead buffering, so I am left to guess.

    Can anyone point to conclusive documentation that indicates whether the above code is actually safe (i.e., that the read operation is guaranteed to return the current file content)? Or documentation about when read-ahead buffering is done? Or how it interacts (or not) with flock? Or explain why the above code is correct, in light of this analysis?

How to bind single click to get an path of an entry in Tk::HList
2 direct replies — Read more / Contribute
by rjohn1
on Apr 17, 2014 at 13:41

    Hi Monks,

    In the below code, if the user clicks on APPLE(which will be displayed) under ONE, i need to know the path 'one.apple' This is currently possible but only with double-click(using -command). How can i change this binding to single click?

    Quoting from HList in CPAN-> Switch: -command Specifies the perl/Tk callback to be executed when the user invokes a list entry in the HList widget. Normally the user invokes a list entry by double-clicking it or pressing the Return key.

    use Tk; use Tk::HList; my $mw = MainWindow->new(-title=>'HList'); my $hlist = $mw->HList->pack; foreach(qw/one two three one.apple two.grapes three.apple/){ my $path = $_; (my $display_entry = $_) =~ s/.*\.//; my $uc_entry = uc($display_entry); $hlist->add($_, -text => $uc_entry); } MainLoop;
    Thanks RJ
Sockets, eval , and DIE
1 direct reply — Read more / Contribute
by Wiggins
on Apr 17, 2014 at 12:12
    I am looking for edge condition precedences. Syswrite to a closed socket(closed at remote end) results in Perl termination, with or without eval. Are there edge conditions that allow this?

    This code (without the eval and test blocks) can be seen on the botton of page 209 of Advance Perl Programming, #1. The MSG module.

    while (@$rq) { my $msg = $rq->[0]; my $bytes_to_write = length($msg) - $offset; my $bytes_written = 0; while ($bytes_to_write) { #write the prefixed length print "_send: syswrite to sock prefix length\n"; eval { $bytes_written = syswrite ($sock, $msg, $bytes_to_write, $offset); }; if ($@){ print "_send: syswrite to sock error <$@>\n"; } print "_send: syswrite to sock returned <$bytes_written>\n"; if (!defined($bytes_written)) { if (_err_will_block($!)) { # Should happen only in deferred mode. Record how # much we have already sent. $conn->{send_offset} = $offset; # Event handler should already be set, so we will # be called back eventually, and will resume sending return 1; ...
    This is Perl 5.14.
    The consistant result, running in foreground, when the server is shutdown, and a client send is attempted is:
    checkRealTTY:: starting UID=<UID> rIP=<> Acct=<root> _send entered <Msg=HASH(0x29987c8)> flush=<1> _send: syswrite to sock prefix length root@kali:/usr/share/msg/bin#

    It is always better to have seen your target for yourself, rather than depend upon someone else's description.


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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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.