Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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
Perl debugging initialization: did I just reinvent the wheel?
4 direct replies — Read more / Contribute
by mpersico
on Dec 01, 2016 at 14:53

    I have worked out a system that allows me to maintain breakpoints in the Perl debugger from session to session and load them up next time I run the debugger:

    First, I create a ~/.perldb file that contains:

    ## -*- cperl -*- sub afterinit { use Cwd; my $dbg = getcwd(). "/.perldb"; print "$dbg...\n"; if ( -e $dbg ) { no strict; do $dbg; } }

    Then I create a local .perldb file in the current working directory where I run  perl -d:

    ## -*- cperl -*- push @DB::typeahead, 'f Some.pm', 'b 75', 'f bin/mymainscript', 'b 180', 'b 192', 'L';

    So, my questions are:

    1) Is this technique widely enough known that I would look like an idiot for blogging about it?

    2) Is the usage of @DB::typeahead unsafe in that it's a direct tap into the DB module that may be changed and, therefore, should not be promoted?

    Thank you.
basic question: regular expression
4 direct replies — Read more / Contribute
by Anonymous Monk
on Dec 01, 2016 at 02:08

    Hi Monks,

    I played with regular expression and perl does something different as I would expect. Only the "+" operator worked as expected. When the "*" or "?" is at the end of a expression, is seems it is sometimes not greedy. Please see my example:

    use strict; use warnings; $_="kkkaaabc"; print "String = $_\n"; print "1 $& Expected: ka ?? /k?a?/\n" if /k?a?/; print "2 $& Expected: kkka OK\n" if /k*a?/; print "3 $& Expected: kaaa OK\n" if /k?a+/; print "4 $& Expected: kkkaaa OK\n" if /k+a+/; print "5 $& Expected: kaaa ?? /k?a*/\n" if /k?a*/; print "6 $& Expected: kkkaaa OK\n" if /k*a*/;

    The result is:

    String = kkkaaabc 1 k Expected: ka ?? /k?a?/ 2 kkka Expected: kkka OK 3 kaaa Expected: kaaa OK 4 kkkaaa Expected: kkkaaa OK 5 k Expected: kaaa ?? /k?a*/ 6 kkkaaa Expected: kkkaaa OK

    Why do I get in case 1 and 5 no "a" at the end of the matching circuit ??. Many Thanks for any help !

Formatting quotes in a String
4 direct replies — Read more / Contribute
by pavan474
on Nov 30, 2016 at 08:48
    Hi All,

    I am new working with PERL. I have a requirement where i have a string having double quotes where it will be open but not closed i.e.,

     $string = '"testing the data available';

    I need to format the string in such a way that it shoul look like

    $string = '"testing the data available"';

    can you please suggest me rather than looping through each word in the string and update the closing string.

    Thanks
Digest::CRC giving unexpected result
1 direct reply — Read more / Contribute
by NickGribble
on Nov 30, 2016 at 08:24
    Hi

    I'm sent data files that have CRC32Q validation on each line. I wrote a script to check each line and it used to give the same value as the checksum in the file but now it doesn't and the code hasn't been changed. Checking the data online shows that the CRC in the file is correct so it's clearly my code that's wrong, but where? In this snippet I've created a page to check that the basic code is correct for just one line. The checksum should be 0x19F112C6 but I'm getting 0x1D094D45. Many thanks.

    #!/usr/bin/perl -w use CGI ':standard'; use CGI::Carp qw(fatalsToBrowser); use Digest::CRC; use strict; my $dataToCheck = 'EGCJOBSTHANGAR534712.29N0011243.02W65.26214.10N4520 +18.84432555.4217.0255.84100503/03/2015'; my $crcOnFile = '19F112C6'; my $ctx = Digest::CRC -> new( width => 32, poly => 0x814141AB, refin = +> 0, refout => 0, xorout => 0, init => 0 ); # Convert $dataToCheck to hex. $dataToCheck =~ s/(.)/sprintf("%X",ord($1))/eg; $ctx -> add( $dataToCheck ); my $crc = uc( $ctx -> hexdigest ); print "Content-type: text/html \n\n"; print '<html><body>'; print "Calculated value = $crc, correct value = $crcOnFile"; print '</body></html>';
split problem
6 direct replies — Read more / Contribute
by rmarkman
on Nov 29, 2016 at 17:13

    Perl 5, version 22, subversion 0 (v5.22.0) built for MSWin32-x64-multi-thread gives this error

    Modification of a read-only value attempted at test.plx line 6.

    With this program:

    use strict; my $value = "A\tB\tC\tD\tE\tF\t\t\t\t"; my @flds; @flds = split(/\t/,$value); $flds[16] = undef; $flds[8] = 0;

    No error is generated if any of: eliminate the trailing tabs in $value; eliminate the call to split; call split with a -1 limit. I am mystified.

twig_print_outside_roots replaces " with &quote; and ' with &apos;
6 direct replies — Read more / Contribute
by dimitarsh1
on Nov 29, 2016 at 12:05
    Hello, When reading and HTML file with TWIG and the twig_print_outside_roots enabled, all " are replaced with &quote; and all ' are replaced with &apos;. Even if I enable the keep_encoding option nothing changes. Does anyone have any idea? Thanks in advance, Dimitar.
How to restart a while loop that is iterating through each line of a file?
7 direct replies — Read more / Contribute
by cookersjs
on Nov 28, 2016 at 16:51

    Greetings monks,
    I am currently trying to solve a loop riddle that has bested me.
    The problem: I am trying to take the first 16 lines of a single file, and populate those same 16 lines in 34 different (but incrementally named) files. My current code:

    my $filename = "../../Annotation_output"; for (my $i=0; $i<=33; $i++) { my $file_count = $i; open(my $new_fh, '>>', ("$filename" . "$file_count" . ".vcf")); my $count = 0; while (<$fh>) { print $new_fh "$_"; $count++; if ($count == 16) { last; } } }
    $fh is the file im trying to take the 16 lines from, FYI

    I have had the first file (Annotation_output0.vcf) gain the 16 lines needed, but never any files afterward.

    Common outputs have included having each of the 34 files gain 16 lines with the while loop never reseting (So Annotation_output0.vcf = lines 0-15, Annotation_output1.vcf = lines 16-31, Annotation_output2.vcf = lines 32-47, and so on), as well as seeing just output0.vcf gain 16 lines while output1.vcf gains the entirety of the while loop, never advancing past $i = 2

    Any help is appreciated!

Win32/DBD::ODBC: works from cmd.exe, not works from apache (xampp)
1 direct reply — Read more / Contribute
by alexander_lunev
on Nov 28, 2016 at 01:14

    Hello, monks!

    I'm not sure it it's a Perl issue, but it is somehow related to it, so i'll give it a try.

    I have a Windows 2003 server, and an old database in DBF files, and a need to work with this DB. Numerous scripts are works fine when started from command line, doesn't matter through Win32::ODBC or DBI::ODBC, but when they started from local apache web server (XAMPP, running under the same user), the shows me an error:

    ERROR ODBC: [-1022] [] "[Microsoft][ODBC dBase Driver]  Disk or network error."

    or, if i use DBD::ODBC:

    ERROR ODBC: [Microsoft][ODBC dBase Driver] Disk or network error. (SQL-HY000)

    But when i run the same script from command line, everything works fine:

    w:\xampp\htdocs>errors.pl 2009-02-27 2005-01-01 2010-01-20 2008-10-01 2012-09-28 2016-08-30 2010-06-18 2010-05-18 2010-03-01 2009-12-23 2010-02-14

    the code itself:

    my $DSN = "EXDSN"; my $Dir = "e:/db/"; my $Driver = "Microsoft dBase Driver (*.dbf)"; if (!Win32::ODBC::ConfigDSN(ODBC_ADD_SYS_DSN, $Driver, "DSN=$DSN", "Description=DSN", "DBQ=$Dir", "DEFAULTDIR=$Dir", "UID=" )) { die "ERROR ODBC: Can't config Win32::ODBC : ".Win32::ODBC::Error() +; } if ($odbctype eq "DBI") { my $EXDSN = "driver=$Driver;dbq=$Dir;defaultdir=$Dir"; $db = DBI->connect("dbi:ODBC:$EXDSN","","" ); die "ERROR ODBC: ". wenc(scalar $DBI::errstr) ."\n" if not $db; } if ($odbctype eq "WIN32") { $db = new Win32::ODBC( $DSN ); die "ERROR ODBC: ". wenc(scalar Win32::ODBC::Error()) ."\n" if not + $db; } if ($odbctype eq "WIN32") { if ( $db->Sql( "select table.date from table group by table.date o +rder by table.date desc" )) { my $err = $db->Error(); print "Error DBF SQL: " . &enc($err) . "\n"; } else { while($db->FetchRow() ) { my(%Data) = $db->DataHash(); print Dumper(%Data); } } } if ($odbctype eq "DBI") { my $ref_astore = $db->selectall_hashref( "select table.date from t +able group by table.date order by table.date desc",'date' ); if (!defined($ref_astore)) { print "Error DBF: " . $db->errstr . "\n"; exit; } else { print join("\n",keys %{ $ref_astore } ); } }

    Although i can have access to *.dbf files and DSN list of ODBC driver from apache. The code:

    my $dir = "e:/db/"; if (-d $dir) { print "OK $dir\n"; opendir(my $dh, $dir) || die "Can't opendir $dir: $!"; print join("\n",readdir($dh)); closedir $dh; } my %dsn = Win32::ODBC::DataSources(); foreach my $key (sort keys %dsn) { print "<br>$key $dsn{$key}"; }

    gives me the output:

    OK e:/db/ . .. TABLE.DBF TABLE2.DBF TABLE3.DBF ... DB Microsoft dBase Driver (*.dbf) DSN Microsoft dBase Driver (*.dbf) EXDSN Microsoft dBase Driver (*.dbf)

    What's the problem here?

    UPDATE:

    SOLVED thanks to Corion. Seems that for ODBC to work it needs TMP environment variable. Setting it in code before ODBC stuff did the trick.

     $ENV{'TMP'} = 'C:\\DOCUME~1\\lan\\LOCALS~1\\Temp\\2';
SOLVED: Looking for suitable spells to get open to return a filehandle from a module
2 direct replies — Read more / Contribute
by talexb
on Nov 27, 2016 at 23:20

    I'm working on what I imagined would be a simple module to provide versioned access to files (this is something that VMS provides), but it seems I've run up against a challenging (to me) situation with opening files in a module, and passing the file handle back to the caller as open does.

    Here's some code that demonstrates the problem:

    #!perl use strict; use warnings; use Extra; { { open ( my $fh, '>', 'normal-open.txt' ) or die "1. Open failed: $!"; defined $fh or die "1. Filehandle not defined"; print $fh "Normal open works fine.\n"; close ( $fh ); } { Extra::open ( my $fh, '>', 'extra-open.txt' ) or die "2. Open failed: $!"; defined $fh or die "2. Filehandle not defined"; print $fh "Extra open works fine.\n"; close ( $fh ); } }
    And the Extra module is here:
    package Extra; use Symbol; sub open { my ( $fh, $direction, $filename ) = @_; $fh = Symbol::gensym; open ( $fh, $direction, $filename ); } 1;

    The first block works fine, and the second block returns from, but the filehandle is undefined.

    I had a good look at the code for File::Temp, and saw where it uses the call to Symbol::gensym for Perls before 5.6, so I tried adding that in. It makes no difference -- I'm using Perl 5.22.1 anyway.

    My plan is to hook the call to open and seamlessly handle open a versioned file using the requested file name as a basis -- I've also tried using sysopen, without any success. There's some magic going on that I don't understand.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Mojo: non-blocking API calls
2 direct replies — Read more / Contribute
by Beatnik
on Nov 27, 2016 at 16:30
    Hey All, I'm continuing my toy project and I'm looking at the responsiveness. Part of the functionality involves external API calls. I've developed the Perl modules that do the external calls (through LWP at this point). My application loads data locally from a database, allows the user to modify the database entry but also should fire off a 'save' call to the APIs. There is some querying from the API to the local database. Obviously, this is expensive so I'm trying to use a non-blocking call. In addition, since users can use the applications, some kind of checks will have to be done to reload application data locally as clean as possible. I've been looking at Mojo::IOLoop::Delay to implement this.

    My question is: do I rewrite / optimize the API library to add non-blocking support (swapping out LWP::UserAgent)? Is Mojo::IOLoop::Delay the right approach for this purpose? Are there any recommended approaches for the synchronizing from the API to the database?


    Greetz
    Beatnik
    ... I'm belgian but I don't play one on TV.
New Meditations
(Placeholder) Imagine!
5 direct replies — Read more / Contribute
by BrowserUk
on Nov 29, 2016 at 22:24

    This place excels in starting with a single, clearly defined, abstracted problem, and refining the set of proposed solutions down to a clear, concise, efficient solution. Many hands make light work!

    Now, imagine if each of the Perl(5) opcodes was proffered here -- say: one per weekend -- on a Friday night - say: US West Coast time -- in the form of a hack-a-thon task for that weekend.

    What might result from that exposure of a clearly defined task to the assembled (and usually quite bored on weekends), diverse populous?

    If those more enlightened tolorant members of p5p were enthused to cast a critical eye over the proceedings -- to catch the less obvious pitfalls in the evolution of solutions -- then the results might be usefully fed back into the p5p process and result in benefits for all.

    Thoughts on: Is the idea viable?

    Thoughts on: Are any members of p5p willing to participate?

    Caveats?


    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.
Propose addition to use warnings?
6 direct replies — Read more / Contribute
by perldigious
on Nov 28, 2016 at 09:30

    I'm not sure what the official mechanism for making such a suggestion would be, but I recently had to go on a bug hunt in my code that left me surprised use warnings didn't give me some sort of, "are you SURE you aren't doing something silly here at line x?" message. Maybe it isn't practical to expect use warnings to save me from myself in this case, but I figured I'd throw it out here as a meditation for discussion and to get opinions from Monks far wiser than I am.

    I'm of course simplifying and paraphrasing, but essentially I had some code that did the following:

    open(my $first_fh, "<", "file1.txt") or die "Cannot open \"file1.txt\" +: $!."; open(my $second_fh, "<", "file2.txt") or die "Cannot open \"file2.txt\ +": $!."; my @file1_lines = <$first_fh>; close $first_fh; while (my $line = <$second_fh>) { my @data = my_sub($first_fh, $line); # copy/paste error, should ha +ve passed $second_fh here # and of course a bunch of non-relevant stuff here }

    So I accidentally passed the $first_fh that I had previously closed to my_sub, but as it turns out use warnings didn't make a peep about this. I did of course get warnings from inside my_sub when it attempted to make use of the filehandle, but the kicker was that any such use was wrapped inside of a conditional that actually made it very rare it would ever get used (maybe once every 100,000 lines or so of typical data).

    DISCLAIMER: The fact that I didn't have something in my standard test data that ensured the conditional that used the filehandle was exercised is entirely on me (forgive me, for I have sinned). I accept full responsibility for that mistake and the initial copy/paste laziness error that forced me to engage in my bug hunt. I'm only pointing out that usually use strict, use warnings, or perl itself is pretty good about preemptively saving me from myself, and in this case it didn't.

    Any thoughts from the Monastery?

    Just another Perl hooker - will code for food
New Cool Uses for Perl
picwoodpecker -- a Tk program to choose and modify your photos
3 direct replies — Read more / Contribute
by Discipulus
on Nov 30, 2016 at 03:37
    Hello monks,

    after months of sparetime works I'm proud to present you picwoodpecker my last Tk effort (now also on github).

    It is application to review and copy pictures with many features.

    Suggestions are welcome.Tests with exif tags from different cameras are even more welcome.

    The post is splitted in 3 parts: here you have the rendered documentation, below there is the code and finally the POD that must be pasted after the __DATA__ token of the program.

                   This software is dedicated to my parents, who need to see printed pictures -- 6 July 1966 

    NAME

    PicWoodPecker

    SYNOPSIS

    perl picwoodpecker.pl [-s -d -debug -pr -wr -p -x -y -nothumbs -e -quality -pngcompression -df]

    OPTIONS

         -s|src|source        path
                        the path where search for jpg files to be loaded
                        Can be modified in the Tk interface

    -d|dest|destination path path used to save files Can be modified in the Tk interface

    -debug print more information on the screen Can be modified in the Tk interface

    -pr|phratio floating point the ratio used to display the current photo Can be modified in the Tk interface

    -wr|winratio floating point the ratio to size the window where the photo is displayed Can be modified in the Tk interface

    -p|preload integer how many photos load in memory after and before the current one. Can increase drawing speed time

    -x|gridx integer how many columns in the thumbnail grid

    -y|gridy integer how many rows in the thumbnail grid

    -nothumbs does not load thumbnails at all

    -e|extension jpg|gif|png|gd|gd2 the exstension of saved files Can be modified in the Tk interface

    -quality|jpegquality 0-100 the quality of the file used by GD when saving the current photo in jpeg format An empty value let GD to choose a good default Can be modified in the Tk interface

    -pngcompression 0-9 the compression factor used by GD when saving the current photo in png format -dateformat|df string the format used for dates. It defaults to %Y_%m_%d_%H_%M_%S in such way resulting pics can be ordered correctly. See C<strftime> in L<POSIX> to more hints about formatting.

    DESCRIPTION

    The program is aimed to let you to easely choose among photos and save one (or more) copy in the preferred format (jpg as default; gif png gd and gd2 are also available). The name of the copy is crafted using the timestamp when the photo was taken.

    Basically the program will load all jpg files found globbing the path given with command line parameter -source or entered in the graphical interface, and foreach file will examine some exif tags to get sizes, timestamps and the thumbnail (if not present showing a black empty one).

    Orientation of the image is handled automatically for thumbnails and main photo.

    Advanced options are available to manipulate how many photos are copied, in which format and let you to postprocess via exiftool each created image.

    The program uses GD for image manipulation and Image::ExifTool to load infos from photos and in the postprocess of them.

    THE GRAPHICAL INTERFACE

    A main control window and a display one are created. Optionally a third window is created to access the advanced copy options. The display window tends to take the focus being the program focused on displaying photos.

    control window

    The control window contains:

    • all controls to manipulate the photo list (&#39;browse&#39; &#39;new list&#39; &#39;view list&#39; &#39;add to list&#39; and &#39;clear list&#39;) Note the that the &#39;browse&#39; does not fill the list; you need to use &#39;new list&#39; or &#39;add to list&#39; after using it.
    • an entry to choose the destination folder (that will be checked for existence)
    • the photo ratio and the window ratio controls and the debug switch
    • an informative text about the current displayed photo or about the grid of thumbnails
    • the editable name of the current photo (and an eventual suffix) used to save it
    • an information text about the status of the main program (with only relevant information about copying and loading operations as eventual errors)
    • the save button and the advanced options one.
    • controls to navigate the photo list

    display window

    The display window will starts showing a grid of thumbnails. The first one is selected. You can navigate the grid using right arrow and left arrow of the keyboard to move the current selection on or back.

    up arrow and down arrow let you load previous or next grids of thumbanails.

    Enter key will open the currently selected thumbanil in a bigger resolution ( determinted by photo ratio parameter) photo filling the whole window.

    When just one photo is displayed right arrow and left arrow of the keyboard can be used to show next and previous photo while up arrow and down arrow bring you back to the thumbnail view.

    In both control and display window space bar can be used to save the current photo and p key can be used to toggle autoplay. If autoplay is active the time between photos can be set in the control window. Please note that specifying a short amount of time (shorter than the time needed to load photos data) can produce weird showing behaviours.

    advanced copy options

    This menu lets you to have a granular control about how original photo will be copied.

    The allow overwrite if checked silently overwrite a photo wich has the same name of what is composed for the current one.

    bypass original file elaboration (simple copy) make a copy of the original file using the new name but without processing it with GD

    output file type lets you to choose among different file fomrmats (jpeg, gif, png, gd and gd2) and to set the quality (0-100) for jpeg ones. For png files the compression factor (0-9) can be specified only via the command line parameter -pngcompression

    enable multiple copies is trickier. If enabled lets you to choose to copy the file many times, each one with a different resolution. In the multi copies pattern you can specify different resolutions in the format widthxheigth as in 800x600 or 1024x768 and for each format a copy will be created.

    enable post processing can be used to postprocess every image with exiftool program that ships with Image::ExifTool module. You can use alternative program to postprocess your image inserting the full path in the program field. Arguments to such program can be also specified where $ can be used to refer to the full path of the original image. In the exiftool command line you can also specify @ to refer to the current file. So you can postprocess every copy using the following arguments:

    -overwrite_original -all= -tagsFromFile $ -ImageSize -ImageWidth -ImageHeight -ThumbnailImage -Orientation -DateTimeOriginal

    This exiftool command will be issued for every copy made, overwriting each exif tags in the copy, removing them all but taking some tag frome the original file and applying them to the copy. See the exiftool page for a lot of options this wonderful program lets you to use.

    LIMITATIONS

    The program was tested against few different camera formats; i dont know if exif tags extracted are widely valid.

    The autoplay feature does not plays well with little time intervals: infact when the interval is smaller than the overall time taken to load the the photo and to redesign the display i'll notice photos and information skipped and the timer going on. I tried fixing this using waitVisibility Tk method, with no luck.

    COPYRIGHT

    This software and icons are copyright of Discipulus as found on www.perlmonks.org You may redistribute or modify it under the same term of Perl itself.

    L*

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2016-12-04 09:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (63 votes). Check out past polls.