Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
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
How does $. work in one liner?
4 direct replies — Read more / Contribute
by elevenfly
on May 28, 2016 at 18:20

    Hello! I found a code like this:

     perl -n -e 'print $_ if 1..2' test.txt

    This code will only print the first and second in test.txt and I was told that if will compare $. with the line number I provide. But what is the reason for that?

    Can anyone provide any help?

    Thanks!,

converting context free grammar to BNF
2 direct replies — Read more / Contribute
by nido203
on May 28, 2016 at 14:48

    Hello! I have a problem with converting this grammar to BNF version in Perl. I am using Parse RecDescent

    $grammar = q{ start: seq seq: '(' seqstr(s) ')' seqstr: seq | tagstr tagstr: OZN ( seq | rijec ) OZN: /[A-Z.,?'*:`*]+ / rijec: /[\w-?,:<*`*]+/ };

    i tried this but it is not working.

    use Grammar::BNF; my $g = Grammar::BNF.generate(Q:to<END> <start> => <seq> <seq> => '(' <seqstr(s)> ')' <seqstr> => <seq> | <tagstr> <tagstr> => <OZN> '(' <seq> | <rijec> ')' <OZN> => /[A-Z.,?'*:`*]+ / <rijec> => /[\w-?,:<*`*]+/ END );
Mojo::DOM find tag after another tag
2 direct replies — Read more / Contribute
by luxs
on May 28, 2016 at 13:23
    I've got simple HTML
    yada...yada...yada.. <img src="111"> yada...yada...yada.. <img src="222"> yada...yada...yada.. <h1>Some title</h1> yada...yada...yada.. <img src="333"> yada...yada...yada.. <img src="444">
    How can I find "333" and "444" (i.e. atributes of IMG tags after H1 tag) with mojo::dom functions? PS. With regex this is a trivial task.
Log::Log4perl::Layout::PatternLayout: setting combined length for two fields?
2 direct replies — Read more / Contribute
by toomas
on May 27, 2016 at 23:03

    Hello,

    Recently I discovered Log::Log4perl. This is such a great piece of work that after a week of using it I'm almost wondering how could I live without it before. But there are things about it I have not been able to figure out by studying the documentation.

    Here is my problem:

    I want to indent log messages by stack depth (for tracing program execution), and although Log::Log4perl itself does not directly support this, I have managed (with help from the Internet, including Perl Monks) to have it my way — almost. I added the following lines to log4perl.conf:

    log4perl.PatternLayout.cspec.S = sub { return ' ' x level_for_l4p() +; } log4perl.appender.std.layout.ConversionPattern = %-27F %3L %S%m%n

    and defined level_for_l4p() in my main program:

    my $zerolevel = 8; sub level_for_l4p { my $level = 0; 1 while caller( $level++ ); return ( $level - $zerolevel ); } Log::Log4perl::init( 'log4perl.conf' );

    (Appropriate numbers for $zerolevel and %F are easily found by trial and error.)

    Logs produced by this setup look basically like this:

    /My/Project/One/File.pm 12 sub0: calling sub1 /My/Project/Another/File.pm 96 sub1: entering /My/Project/Another/File.pm 105 sub1: calling sub2 /My/Project/Elsewhere.pm 72 sub2: entering /My/Project/Elsewhere.pm 84 sub2: leaving /My/Project/Another/File.pm 108 sub1: continuing after sub2 /My/Project/Another/File.pm 115 sub1: leaving /My/Project/One/File.pm 16 sub0: continuing after sub1 # etc.

    In principle, this is exactly what I want, but I don't like the appearance of the output: IMHO it fails miserably to please the eye.

    I'd much prefer being able to have the log output look like this:

    /My/Project/One/File.pm:12: sub0: calling sub1 /My/Project/Another/File.pm:96: sub1: entering /My/Project/Another/File.pm:105: sub1: calling sub2 /My/Project/Elsewhere.pm:72: sub2: entering /My/Project/Elsewhere.pm:84: sub2: leaving /My/Project/Another/File.pm:108: sub1: continuing after sub2 /My/Project/Another/File.pm:115: sub1: leaving /My/Project/One/File.pm:16: sub0: continuing after sub1

    which would also have the additional benefit of working right out of the box with Emacs' default compilation-error-regexp-alist. To achieve this, it should be possible to say something like %-32{%F:%L:}, i.e. to tell Log::Log4perl to set the total length for %F:%L: as a unit. But I have not been able to find a way to express my intention.

    Is it possible to do what I want? In case I'm missing something obvious, can anybody point me to an existing and/or simple solution?

    I am using Log::Log4perl 1.47 and Perl 5.20.0.

    Cheers,
    T.

DBD::CSV::st execute failed:
4 direct replies — Read more / Contribute
by Sandy_Bio_Perl
on May 27, 2016 at 17:49

    Dear Monks, I am runnng an sql query using DBI. The code works well on my laptop running Strawberry Perl but fails to run on our university linux server using Perl 5.10.1

    My perl progam is

    #!/usr/bin/perl use strict; use warnings; use Text::CSV; use DBI; sub RunSqlSearch; my $query = qq(SELECT sid,genotype FROM newCsv.csv WHERE genotype = 'a +'); my ($queryResult,@sidResult) = RunSqlSearch($query); print "You query $query returned the following result:$queryResult\n"; print "The sid list associated with this result are: @sidResult\n\n"; sub RunSqlSearch($){ my $query = $_[0]; # error check # if ($query eq ""){die "No value entered for $query $!\n";} # Connect to the database, (the directory containing our csv file +(s)) my $dbh = DBI->connect ("dbi:CSV:", undef, undef, { f_dir => ".", f_ext => ".csv/r", f_encoding => "utf-8", RaiseError => 1, }) or die $DBI::errstr; # Output using sql query # my $sth = $dbh->prepare($query); $sth->execute; my @row; my $queryResult=""; my @queryResult; my @sidResult; while (@row = $sth->fetchrow_array) { push @queryResult, @row; push @sidResult, $row[0].","; $queryResult .= join("\t",@row) . "\n"; } # output arguments # if ($queryResult eq ""){$queryResult = "No result found";} return ($queryResult, @sidResult); $sth->finish(); $dbh->disconnect(); }

    I get the following error message from our university server

    DBD::CSV::st execute failed: Execution ERROR: No filename given at /usr/lib64/perl5/DBD/File.pm lin +e 565. called from runSQLQuery.pl at 11. at /usr/lib64/perl5/DBD/File.pm line 565. [for Statement "SELECT sid,genotype FROM newCsv.csv WHERE genotype = +'a'"] at runSQLQuery.pl line 32. DBD::CSV::st execute failed: Execution ERROR: No filename given at /usr/lib64/perl5/DBD/File.pm lin +e 565. called from runSQLQuery.pl at 11. at /usr/lib64/perl5/DBD/File.pm line 565. [for Statement "SELECT sid,genotype FROM newCsv.csv WHERE genotype = +'a'"] at runSQLQuery.pl line 32.

    Could you please point out the error of my ways?

    !!Fixed!!. Thank you, bretheren, for your wise counsel. The clue came from an anonymous brother who pointed out that there was no need for me to both specify the file extension name *.csv and also use the command f_ext => ".csv/r", When I did this I no longer received an error message, but I also got a nil return, which I new to be wrong.

    Searching on stackoverflow, I found that in the act of cleaning up a and creating a csv file one can remove the DOS/Windows line ending which DBD::CSV assumes regardless of the platform the script is run on. Thanks to Grant McLean at http://stackoverflow.com/questions/2786741/dbdcsv-problem-with-file-name-extensions for this insight

    The solution is to add the term csv_eol => "\n", as $dbh parameter.

Recovery of a "multi-perl" installation
1 direct reply — Read more / Contribute
by haricot-48
on May 27, 2016 at 10:14

    Hi Monks,
    while doing the initial setup of a server (that will host a web application) I've found something unexpected and I need your help to proceed with my job.
    The installation of a new perl module (DBD::Oracle) gave me this output message:

    Multiple copies of Driver.xst found in: /usr/local/lib/x86_64-linux-gnu/perl/5.22.1/auto/DBI/ /usr/lib/x86_64-linux-gnu/perl5/5.22/auto/DBI/ at Makefile.PL line 39. Using DBI 1.636 (for perl 5.022001 on x86_64-linux-gnu-thread-multi) installed in /usr/local/lib/x86_64-linux-gnu/perl/5.22.1/auto/DBI/

    I've searched and I found that the server seems to have two different "Perl installations" (forgive me if I'm not using the proper term)

    Here follows an excerpt of the content of the two paths:

    root@xyz# ll /usr/lib/x86_64-linux-gnu/perl5/5.22/auto/ total 44 drwxr-xr-x 11 root root 4096 May 27 09:48 ./ drwxr-xr-x 13 root root 4096 May 27 09:48 ../ drwxr-xr-x 3 root root 4096 May 13 16:19 Algorithm/ drwxr-xr-x 2 root root 4096 May 27 09:46 DBI/ drwxr-xr-x 2 root root 4096 May 9 16:48 FCGI/ drwxr-xr-x 3 root root 4096 May 13 16:19 File/ drwxr-xr-x 3 root root 4096 May 9 16:48 HTML/ drwxr-xr-x 2 root root 4096 May 9 16:47 LibAppArmor/ drwxr-xr-x 3 root root 4096 May 9 16:43 Locale/ drwxr-xr-x 4 root root 4096 May 9 16:43 Text/ drwxr-xr-x 3 root root 4096 May 27 09:48 YAML/ root@xyz# ll /usr/local/lib/x86_64-linux-gnu/perl/5.22.1/auto/ total 16 drwxrwxr-x 4 root root 4096 May 27 11:28 ./ drwxrwxr-x 7 root root 4096 May 27 08:19 ../ drwxrwxr-x 3 root root 4096 May 27 11:28 DBD/ drwxrwxr-x 2 root root 4096 May 27 08:19 DBI/

    I don't want to have different installation on the server and, most of all, I don't want the new modules to be installed in the /usr/local/lib/ path, as it seems to be happening to the DBD::Oracle module.
    I've found many posts/answers that explain how different (and coexistent) perl installations can be created; none of them tells how they can be deleted.
    I would like to know if there is a procedure to recovery the main perl installation and roll back to a single "instance".
    Will there be side effects if I delete all the content of the /usr/local/lib/ installation?

    Thank you very much in advance.

X Error in Tk remote drag and drop
1 direct reply — Read more / Contribute
by prenaud
on May 27, 2016 at 08:10

    Hello,

    I use PERL on Linux Mint and I have an error when I drop a file from the Cinnamon desktop on a Tk widget :

    X Error of failed request: BadWindow (invalid Window parameter) Major opcode of failed request: 25 (X_SendEvent) Resource id in failed request: 0x2fd00fd Serial number of failed request: 164 Current serial number in output stream: 165

    My source code is:

    #!/usr/bin/perl use strict; use Tk; use Tk::DropSite; my $mw = new MainWindow(-width => 500, -height => 300); my $label = $mw->Label()->pack->place(-relwidth => 1.0, -relheight => +1.0); $label->DropSite(-droptypes => ['XDND'], -dropcommand => [\&dropFile, +$label]); MainLoop; sub dropFile { print "DROP\n"; }

    What is wrong in this code ?

    Perl version is 5.18.2 and Tk version is 804.033

    Thanks for the help.

Using .pod as a standalone file rather than in .pm and it showing up in MetaCPAN
2 direct replies — Read more / Contribute
by rockyb
on May 26, 2016 at 19:00

    I'd prefer to have some documentation associated with a Perl Module, but I would rather not put it inside the code or .pm file but rather its own .pod file.

    The reason I'd like to do this is so that it can be shared in say a github wiki where folks can interactively edit.

    Before a release though, I'd copy the wiki into the distribution.

    So what are practices or best practices for having documentation showing up on search.cpan.org or metacapan.org outside of the code?

hash question
4 direct replies — Read more / Contribute
by duyet
on May 26, 2016 at 06:46
    I've been working with hash for a while now, but there is something i really don't understand.
    my $hash = {}; do_something( $hash ); print Dumper( $hash ); sub do_something { my $hash = shift; $hash = { a => 'alpha', b => 'beta', }; print Dumper( $hash ); }
    when exectuted:
    { 'a' => 'alpha', 'b' => 'beta' } {}
    but if using:
    do_something_else( $hash ); print Dumper( $hash ); sub do_something_else { my $hash = shift; $hash->{g} = 'gamma'; $hash->{d} = 'delta'; print Dumper( $hash ); }
    it shows:
    { 'd' => 'delta', 'g' => 'gamma' } { 'd' => 'delta', 'g' => 'gamma' }
    To me the initialization of $hash is the same in both subs, but apparently it is not. It's probably something very trivial, but i just miss it. Would you please explain why? TIA.
Code coverage of forked code
1 direct reply — Read more / Contribute
by britterm
on May 25, 2016 at 11:46
    Hi monks,

    I have a module which forks several times to set up a few workers. I'm using Test::More and Devel::Cover to do unit testing of my code and generate code coverage. I have unit tests that stress the workers, but the coverage report shows that the forked code didn't execute even though it did.

    I considered putting the child code into separate functions that can be tested independently of forking, but the code relies on piped handles to communicate with the parent, which AFAIK do not function without forking (can't write/read to a pipe in the same process). I would have to do a significant amount of coding in my unit tests to attempt to work around this, and I'm looking for a more straightforward solution if there is one.

    Does anyone know how to configure Devel::Cover to detect coverage in the child process when the program forks? Is this even possible? (perhaps with a different coverage tool?)

    Thanks in advance,
    Brian
Finding file level lexical variables
1 direct reply — Read more / Contribute
by johndeighan
on May 25, 2016 at 11:35
    Our mod_perl application is sometimes running out of memory. So, I want to create a function to track down all variables outside of functions and check their memory usage using Devel::Size. I'm using Package::Stash to find all package variables (declared using 'our'), but now I want to also find all lexical variables (declared with 'my') which are at the file, i.e. top, level in all packages. I know about PadWalker but don't know how to use it - or anything else - to find and get the memory used by these top level lexical variables.
Collaborate on a module
4 direct replies — Read more / Contribute
by ASeekerOfWisdom
on May 24, 2016 at 10:17

    So sometime ago I adopted a module. The original author had not updated this module for a while and When I spoke to him he was glad I was taking this module over. So after some time I fixed it. Now that module is ready for a release to CPAN.

    So my question is how do I release this module on CAPN? Do I ask the author to release it or Do I release it under my pause ID? (I don't want to take the credit form the original author for doing a lot of the foundation work) This is my first time doing something like this so sorry if this sounds stupid.
New Cool Uses for Perl
i2c attached LCD Character Display for a Raspberry Pi
1 direct reply — Read more / Contribute
by anita2R
on May 24, 2016 at 12:28

    I was unable to find a Perl script to make my SainSmart LCD character display with i2c 'backpack' work from my Raspberry Pi.

    Code for using these devices is available mainly for the Arduino. There is also code in C and python, but I could not find a simple working example in Perl. There is a module for an HTBackpackV2 (HiPi::Interface::HTBackpackV2), but I wasn't able to get this working with the SainSmart LCD.

    I found a python script written by Matt Hawkins here: https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/lcd_i2c.py and I used his python script as the basis for my Perl Script - thanks Matt.

    This script only provides basic display functionality and is run by calling it with two parameters, -s a string of text to display and -l the line number to display the text on. Hopefully it will provide both a way for others to get one of these LCD's working as well as provide the basis for more comprehensive implementations. The subroutines writeByte and sendByte are the key ones, together with the initialization sequence in the init subroutine.

    The script must be run as root to set up the i2c object, but drops back to the regular user immediately after - enter your user name and user group in the two variables $user and $group which you will find at lines 39 & 40.

    When called with no text and no line parameter the LCD initialization code is run.

    After the code I have added some explanation about the way the data is transferred from the backpack to the LCD, including the transition from the power-up 8-bit mode to 4-bit mode. It took me some time to get my head round this!

    In addition to -l 1 & -l 2 for the two lines, -l 50 & -l 51 turn the display off and back on. When the display is turned off with -l 50, the data remains in the LCD's DDRAM memory and the previous text will be displayed by -l 51 (a simple flashing display can be implemented)

    If no line number is given, the text is printed on line 1 and overflows to line 2 if required. All existing text is cleared.

    Examples (run as root/sudo):


    Code for a Raspberry Pi with SainSmart 2 line, 16 character LCD display with i2c backpack at 0x3f, attached to the Pi's default i2c pins.

    Mode of operation & initialization

New Obfuscated Code
Having fun with ambiguity
No replies — Read more | Post response
by trizen
on May 23, 2016 at 14:30
    print $$ /0; # a legit division by zero ^....some.....^ ^....black....^ ^....magic....^ |(?{m{(?{(("\[\[ \)\.\\\|\`\]\[\[ \{\[\.\@\/\(\^\. \[\{\;\\\,\[\@\: \?\+\^\)\("=~s{[ \s]}<>rg)^("\+\) \@\@\(\^\*\(\(\/ \[\:\@\/\[\@\;\\ \{\+\^\.\@\{\(\[ \\\@\;\[\"\""=~s <\s>()gr))})},s[ ]<$^R>seexi})|&: ^....hugs....^ ^.....&......^ ^...kisses...^ // //xo//xo//xo//xo//xo//xo//xo// //
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2016-05-29 06:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?