We don't bite newbies here... much 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
 Imager::File::TIFF Dependencies 3 direct replies — Read more / Contribute by BrentD on May 22, 2017 at 17:31 I have written a program that allows me to input some info, then calls Imager to output the info as Group4 Fax encoded TIF files. I am running this under Windows 10 64-Bit. The program runs fine if I use the .pl file, but if I use pp to build an .exe file and copy to another system, the file will not run. This is my current pp command call: pp -M Imager::Font::W32 -M Imager::File::TIFF -M -a cbxarrow.xbm -a ar +rowdownwin.xbm -u -o Program.exe Program.pl [download] This is what happens when I run the .exe file: E:\>Program.exe Can't load 'C:\Users\generic\AppData\Local\Temp\par-67656e65726963\cac +he-1fbb3c46710e1a62d9823bf27f4a9fe7a961c4bb\c6dc6748.xs.dll' for modu +le Imager::File::TIFF: load_file:The specified module could not be fo +und at C:/Strawberry/perl/lib/DynaLoader.pm line 193. at C:/Strawberry/perl/lib/DynaLoader.pm line 95. DynaLoader::croak("Can't load 'C:\\Users\\generic\\AppData\\Lo +cal\\Temp\\par-67656e657"...) called at C:/Strawberry/perl/lib/DynaLo +ader.pm line 193 DynaLoader::bootstrap("Imager::File::TIFF", 0.89) called at C: +/Strawberry/perl/vendor/lib/PAR/Heavy.pm line 120 PAR::Heavy::_bootstrap("Imager::File::TIFF", 0.89) called at C +:/Strawberry/perl/lib/DynaLoader.pm line 102 DynaLoader::bootstrap_inherit("Imager::File::TIFF", 0.89) call +ed at Imager/File/TIFF.pm line 10 Imager::File::TIFF::BEGIN() called at Imager/File/TIFF.pm line + 11 eval {...} called at Imager/File/TIFF.pm line 11 require Imager/File/TIFF.pm called at TCTC/Dockets.pm line 6 require TCTC/Dockets.pm called at script/Program.pl line 32 main::BEGIN() called at Imager/File/TIFF.pm line 11 eval {...} called at Imager/File/TIFF.pm line 11 require main called at C:/Strawberry/perl/vendor/lib/PAR.pm li +ne 636 PAR::_run_member(Archive::Zip::ZipFileMember=HASH(0x37e33b0), +1) called at script/main.pl line 26 require main called at C:/Strawberry/perl/vendor/lib/PAR.pm li +ne 636 PAR::_run_member(Archive::Zip::ZipFileMember=HASH(0x37e3698)) +called at C:/Strawberry/perl/vendor/lib/PAR.pm line 428 PAR::import("PAR") called at -e line 631 eval {...} called at -e line 41 __par_pl::BEGIN() called at Imager/File/TIFF.pm line 11 eval {...} called at Imager/File/TIFF.pm line 11 BEGIN failed--compilation aborted at Imager/File/TIFF.pm line 11. Compilation failed in require at TCTC/Dockets.pm line 6. Compilation failed in require at script/Program.pl line 32. BEGIN failed--compilation aborted at script/Program.pl line 32. E:\> [download] Seems obvious that I have some sort of missing dependency, but I can't seem to find what that might be. Any ideas? Trouble with fork() 2 direct replies — Read more / Contribute by phonybone_monk on May 22, 2017 at 16:55 Hello, perlmonks, I am seeking advice on diagnosing a problem related to a Perl program (which I inherited) that makes use of fork(). The basic problem seems to be that some of the forked sub-processes immediately exit without doing any work. When those processes are reaped (with wait()), $? is set to 11, which I believe is the error code for "resource temporarily unavailable, try again". I am trying to determine which resource is unavailable. The environment is Linux/Centos 7 The basic structure of this program is as follows: - Initialization - Read in a large file (~10Gb) - Do some processing of the input file using other open-source software (bowtie and samtools), via calls to system(). This results in the writing of some intermediate files. - Process the intermediate files in parallel. For each processor core unit (as reported by Sys::Info::Device::CPU, or by reading /proc/cpuinfo), my program calls fork(). But instead of then calling exec(), the child continues processing on its own. - The parent process, after spawning all the children, enters a loop around a call to wait(). If wait() returns -1, the parent prints a message that includes the value of$?, and then exits (via Carp::confess). Here's a pseudo code outline (note: this won't actually re-create my problem, of course): # Initialization # Read input file, calls to system('bowtie2', ...) and system('samtool +s', ...) use strict; use warnings; use Carp qw(confess); use Sys::Info::Device::CPU; use Sys::Info::Constants qw( :device_cpu ); my $info = Sys::Info->new; my$cpu = $info->device( CPU => {} ); my$nthreads = $cpu->count; print "$nthreads threads\n"; while ($nthreads--) { my$pid = fork; confess ("Can't fork: $!") unless defined$pid; if (! $pid) { print "child process:$$\n"; # this line never gets executed +by some of the children # Do a bunch of processing using intermediate files as input, usin +g Bio::DB::Sam->get_features_by_location() to access intermediate fil +es exit 0; } } # parent continues here: my$pid = 0; my $ok = 1; while ($pid != -1) { $pid = wait; last if$pid == -1; my $err=$?; if ($err) { print("one of the children died: pid=$pid, err=$err\n");$ok = 0; } } if (! $ok) { confess "some child processes died."; } print "all done, yay\n"; [download] In all of the processes, the line directly after the fork confess("Can't fork:$!") unless defined $fork; never gets executed. I infer that the fork itself did happen. But some of the processes, depending on the input file, are reaped immediately, with$err=11 (EAGAIN). I surmise I am running short of some resource, but which one? I have increased the limits for the following resources to values (by editing /etc/security/limits.conf, and then rebooting) that I believe should be more than sufficient, to no avail: - number of open file handles - number of allowed processes per user - number of file locks allowed - amount of lockable memory I've also increased swap space via swap files. Short or restructuring the code to use exec(), which I've considered, do any wise monks have suggestions as to: - how to determine which missing resource is causing fork() to exit with $?=EAGAIN? - any other useful areas to examine? My thanks for all answers, insights, and useful comments What is correct way to reference? 6 direct replies — Read more / Contribute by BradV on May 22, 2017 at 13:13 I'm writing some code to parse some xml files. One element could either have a single value or an array of values. I have: if ( ref($xmlcollection->{phone}->{callTo}) eq "ARRAY" ) { printf( "Call To:" ); foreach ($xmlcollection->{phone}->{callTo}) { print "$_\n"; } } else { printf( "Call To: %s\n", $xmlcollection->{phone}->{callTo}); } [download] It just prints the array reference. If I add: printf( "%s\n",$xmlcollection->{phone}->{callTo}[0]); printf( "%s\n", $xmlcollection->{phone}->{callTo}[1]); [download] it prints the correct values. The problem is I don't always know how many elements will be in the array. How do I correctly reference the array element of the hash in order to print out all of the values? \b in Unicode regex 3 direct replies — Read more / Contribute by Arik123 on May 22, 2017 at 02:45 Hi Monks! I do$string =~ /$_/ and it matches. I do$string =~ /\b$_\b/ and it doesn't match, for the same values of$string and $_. I think it should match, since there's a hyphen or a dot after$_ in $string, which I think should match \b. Both$string and $_ are Unicode. Could it be that \b doesn't function for Unicode strings? why is the size of notebooks area smaller than main window 2 direct replies — Read more / Contribute by madhusudhan_s on May 22, 2017 at 00:49 Hi, I am trying to write a program which has many notebooks and i found a reference code from the web as below which is exactly what i want. When i make the main window size 870x1000 i notice that the area where the notebooks are present is smaller than the main window. How should i modify this code such that the notebooks are as big as the main window? #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::NoteBook; our$mw = MainWindow->new; $mw->geometry("870x1000"); my$canvas = $mw->Scrolled('Canvas',-bg=>'red')->pack; our$nb = $canvas->NoteBook; ### add 20 tabs to notebook for ( 1 .. 20 ){ my$title = "Untitled ($_)";$nb->add( $_, -label =>$title ); } ### embed the notebook widget into the canvas my $nb_in_canvas =$canvas->createWindow( 0,0,-window => $nb,-anchor = +> 'nw' );$canvas->update; ### the whole notebook is larger than what can be initially displayed ### so we'll have to sroll it later on in the program $canvas->configure(-scrollregion => [0,0,$nb->reqwidth,$nb->reqheight] +); MainLoop; [download] scrape a webpage 2 direct replies — Read more / Contribute by backyardbill on May 21, 2017 at 23:26  # ============ this is the HTML snipit from Firebug ================== +======== 4WD/AWD # ====== this is the perl code snipit trying to access/extract the dat +a print "\n$node\n"; my $B =$tree->findvalue( $node );$B = trim($B); print "\n$B \n"; # ============ this is what prints ================ /html/body/div/div[4]/div[2]/div[2]/form/div[3]/div/table[2]/tbody/t +r/td[3]/div/table/tbody/tr[1] 4WD/AWD # ============ this is my question ================ # # how do I access/extract the 'value="72"' data? [download] syntax error 3 direct replies — Read more / Contribute by keiththirgood on May 21, 2017 at 11:39 I am new to all coding and I have no idea what is wrong here. I have a simple form I'm trying to send cleaned up results from to an email address. This is the error message I received: syntax error at feedback.cgi line 33, near "print MESSAGE "From" syntax error at feedback.cgi line 35, near "n\" syntax error at feedback.cgi line 79, near "}" Execution of feedback.cgi aborted due to compilation errors. This is the code near each of those lines: 32 print MESSAGE "To: keith\@wilsonstreetstudio.ca\n”; # Don't forget to escape this @ symbol! 33 print MESSAGE "From: " . $FORM{name} . ", reader\n"; 34 print MESSAGE "Reply-to: " .$FORM{email} . "(" . $FORM{name} . ")\n"; 35 print MESSAGE "Subject: Feedback from$FORM{name} \n\n"; 36 print MESSAGE "$FORM{name} wrote:\n\n"; 76 EndHTML 77 78 exit(0); 79 } Experimental push on scalar now forbidden 3 direct replies — Read more / Contribute by gsd4me on May 21, 2017 at 04:35 Sorry to interrupt your tonsure creating dear monks but some advice please. It appears that Perl has adapted itself and has thrown our system's operation, established during a small routine change to our code. The code creates several data types from definitions in a package, and stores them as a series of joined records - so one record holds the data pointer of another etc. (I have simplified the code to show you below) (It was so much easier to use address manipulations in 'C' but Perl's list manipulation was one of the original reasons why we went with Perl over 'C'). The trouble is that the code that was working before has now seemingly been banned as being anti- the Perl Spanish Inquisition's latest thinking on purity. Obviously a code rewrite/redesign is *not* desired at this stage so is there a better/newer/easier way of simply pushing the element onto the list held by the 'root' structure? The error being given is: Experimental push on scalar is now forbidden at C:\crap\adbtest.pl line 40, near "$rohan1;" Experimental push on scalar is now forbidden at C:\Users\Alan\Documents\crap\adbtest.pl line 46, near "$rohan4;" Many thanks - ADB  Package/data sources package rohanType1; sub new { my$self = {}; # Discard the class name shift; $self->{'field11'} = undef; bless ($self); return $self; } return 1; package rohanType2; sub new { my$self = {}; shift; $self->{'field21'} = 0; bless ($self); return $self; } return 1; package rohanType3; sub new { my$self = {}; shift; # Pointer to rohanType1 $self->{'field31'} = undef; # Pointer to rohanType2$self->{'field32'} = undef; bless ($self); return$self; } return 1; # Now the data structures used globally to store information used by t +he whole analysis, which holds # data types as defined above package rohanData; sub new { my $self = (); shift;$self->{'rohan1'} = undef; $self->{'rohan2'} = (); bless ($self); return $self; } return 1; ======================= Code my$rohanRoot; my $rohan1; my$rohan2; my $rohan3; my$rohan4; $rohanRoot = rohanData->new();$rohan1 = rohanType1->new(); $rohan2 = rohanType2->new();$rohan1->{'field11'} = $rohan2;$rohanRoot->{'rohan1'} = $rohan1; # my debug helper to see if add +resses are correct - not part of code push$rohanRoot->{'rohan2'}, $rohan1; # Interpreter/debugger objec +ts to this ...$rohan3 = rohanType1->new(); $rohan4 = rohanType2->new();$rohan3->{'field11'} = $rohan4; push$rohanRoot->{'rohan2'}, $rohan3; # ... and this [download] Random F%^k up strings! 5 direct replies — Read more / Contribute by TroyH on May 20, 2017 at 18:59 I have problem with comparing two strings, one is generated from a random “if else” it's called$answer, the other is input from a form, it called $useranswe. If I test to see if there the same it fails but if I say$answer = “HsRtm” then compare it to $useranswer it works. 1.$randomNumber = int(rand(100)) +1; if ($randomNumber < 100 and$randomNumber > 50) { $answer = "kbNtA"; } else {$answer = "WiaNq"; } 2. $answer = “qUfuT”; [download] 1. doesn't work but 2. does. Can anyone explain to me why does the random statement corrupt the$answer John Guttag's book - 2nd exercise. My attempt in Perl. 11 direct replies — Read more / Contribute by pritesh_ugrankar on May 20, 2017 at 16:22 Hi Monks, I came across John Guttag's book on computation - "Introduction to Computation and Programming Using Python". Note that I love Perl, but found this book to be very interesting. I tried the second finger exercise and though, initially it appeared trivial to solve, it took me a real long time to figure it out. Here's the exercise: Write a program that examines three variables— x , y , and z — and prints the largest odd number among them. If none of them are odd, it should print a message to that effect. Points to note:- This is to be tackled using only if/else loop and comparisons, because that is what the author has covered till that part. Here's my attempt in Perl. I tried other ways, but most of them would not print the expected results if either of the numbers were negative, or two of the three numbers were even. use strict; use warnings; my ($x,$y, $z) = (-11,-13,4); if ($x % 2 == 0 and $y % 2 == 0 and$z % 2 == 0) { print "All are even numbers\n"; } elsif ($x % 2 == 0 &&$y % 2 == 0) { print "$z is the biggest odd number\n"; } elsif ($y % 2 == 0 && $z % 2 == 0) { print "$x is the biggest odd number\n"; } elsif ($x % 2 == 0 &&$z % 2 == 0) { print "$y is the biggest odd number\n"; } elsif ($x % 2 == 0 && $y % 2 != 0 &&$z % 2 != 0) { if ($y >$z) { print "$y is the greatest odd number\n"; } else { print "$z is the biggest odd number\n"; } } elsif ($y % 2 == 0 &&$x % 2 != 0 && $z % 2 != 0) { if ($x > $z) { print "$x is the greatest odd number\n"; } else { print "$z is the biggest odd number\n"; } } elsif ($z % 2 == 0 && $x % 2 != 0 &&$z % 2 != 0) { if ($x >$y) { print "$x is the greatest odd number\n"; } else { print "$y is the biggest odd number\n"; } } else { if ($x >$y && $x >$z) { print "$x is the biggest odd number\n"; } elsif ($y > $z) { print "$y is the biggest odd number\n"; } else { print "$z is the biggest odd number\n"; } } [download] Update: As pointed out by Choroba, I did a typo at line 26. Sorry for the typo. I've fixed it below. use strict; use warnings; my ($x, $y,$z) = (-11,-13,-17); if ($x % 2 == 0 and$y % 2 == 0 and $z % 2 == 0) { print "All are even numbers\n"; } elsif ($x % 2 == 0 && $y % 2 == 0) { print "$z is the biggest odd number\n"; } elsif ($y % 2 == 0 &&$z % 2 == 0) { print "$x is the biggest odd number\n"; } elsif ($x % 2 == 0 && $z % 2 == 0) { print "$y is the biggest odd number\n"; } elsif ($x % 2 == 0 &&$y % 2 != 0 && $z % 2 != 0) { if ($y > $z) { print "$y is the greatest odd number\n"; } else { print "$z is the biggest odd number\n"; } } elsif ($y % 2 == 0 && $x % 2 != 0 &&$z % 2 != 0) { if ($x >$z) { print "$x is the greatest odd number\n"; } else { print "$z is the biggest odd number\n"; } } elsif ($z % 2 == 0 &&$x % 2 != 0 && $z % 2 != 0) { if ($x > $z) { print "$x is the greatest odd number\n"; } else { print "$z is the biggest odd number\n"; } } else { if ($x > $y &&$x > $z) { print "$x is the biggest odd number\n"; } elsif ($y >$z) { print "$y is the biggest odd number\n"; } else { print "$z is the biggest odd number\n"; } } [download] While this works fine for all the test cases I tried, but I will be thankful if you could please let me know a better way of writing this. Point to note is, it should only use if/else/elsif loops and / or comparison operators.

text here (a paragraph)

and:  code here 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.
• 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
 erix . o O( "what fools the french are, Jeeves" ) [choroba]: Also some Достое́вс кий [Discipulus]: mmh windows understand something like: cd c:\\\\path\\\\to weird.. [Discipulus]: even with odd number of \

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2017-05-24 08:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (183 votes). Check out past polls.