Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
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 UTF-8 serving HTML5
2 direct replies — Read more / Contribute
by $h4X4_|=73}{
on May 29, 2016 at 12:22

    Update: I got it to work. Viewing the source problem was the header text/plain, I needed text/html. The "Content-Length" issues was

'g' flag w/'qr'
8 direct replies — Read more / Contribute
by perl-diddler
on May 28, 2016 at 22:25
    I was trying a few examples from the "Mastering Perl" (brian d foy) book relating to RE's. I noted w/interest, the 3 ways he used to match a "qr" expression on page 3.

    sub iregex { qr/ ... /ix } 1) my $regex = iregex() if ($isbn =~ m/$regex/ ) { print "Matched!\n } 2) my $regex = iregex() if ($isbn =~ $regex ) { print "Matched!\n } 3) if ($isbn =~ iregex() ) { print "Matched!\n }
    I especially liked methods 2 & 3 as they didn't need interpolation, with 3 being better as it didn't require an intermediate variable.

    Seeing that I wanted to try out capturing all matching sub expressions using 'g' added to my 'qr' expression, but I'm getting a deprecation error:

    > perl -we'use strict;use P; my $re = qr{ (\w+) }gx; my $dat = "Just another cats meow"; my @matches = $dat =~ $re; P "#matches=%s, matches=%s", scalar(@matches), \@matches; exit scalar(@matches);' output: Having no space between pattern and following word is deprecated at -e + line 2. Bareword found where operator expected at -e line 2, near "qr/ (\w+) / +gx" syntax error at -e line 2, near "qr{ (\w+) }gx" Execution of -e aborted due to compilation errors.

    If I move the 'g' option down to where the RE is used and use a *second* "RE" operator (i.e.: /.../g) and interpolate my expression into another RE (the slowest of the above 3 options), I get:

    > perl -we'use strict;use P; my $re = qr{ (\w+) }x; my $dat = "Just another cats meow"; my @matches = $dat =~ /$re/g; P "#matches=%s, matches=%s", scalar(@matches), \@matches; exit scalar(@matches);' output: #matches=4, matches=["Just", "another", "cats", "meow"]

    So why can't I add the global switch to the "qr" expression? FWIW, I also tried qr{ (?xg) }. Perl strips out the 'g' and tells me to add it to the end of the RE -- where it is deprecated:

    perl -we'use strict;use P; my $re = qr{ (?xg) (\w+) }; my $dat = "Just another cats meow"; my @matches = $dat =~ $re; P "#matches=%s, matches=%s", scalar(@matches), \@matches; exit scalar(@matches);' output: Useless (?g) - use /g modifier in regex; marked by <-- HERE in m/ (?xg + <-- HERE ) (\w+) / at -e line 2. #matches=1, matches=["another"]

    So how can I attach the "/g" modifier to my "qr" regex so I can use the direct match as in #2 or #3 above?

    Thanks...

    P.S. - I also just noticed that in addition to stripping out the 'g' option, the 'x' option doesn't seem to work in the regex's parens, i.e. - (?x).

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
3 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.
Perl6: unexpected exception when printing to a writable file handle
1 direct reply — Read more / Contribute
by stevieb
on May 28, 2016 at 10:42

    So, I've spent an hour or two every night this week reading through the perl6 intro, and several of the various official perl6 docs, but I've run into an issue. I don't know if this is my problem, or a problem with the docs.

    From the open() documentation, there is the following code example (spurt() simply prints something to the file):

    my $fh = open("test", :w); spurt $fh, "stuff\n";

    But when I test it (code is verbatim, the line num in the error is the spurt() line):

    my $wfh = open("in.txt", :w); spurt $wfh, "a\n"; $wfh.close;

    ... it results in:

    Type check failed in binding $path; expected Cool but got IO::Handle ( +IO::Handle.new(:path(...) in block <unit> at nl.pl6 line 2

    Can someone explain where I'm going wrong? If it's relevant, I just installed the most recent p6 bits yesterday afternoon.

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
2 direct replies — 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.
New Meditations
Behavior of Flip-Flop Operators
1 direct reply — Read more / Contribute
by haukex
on May 29, 2016 at 11:09

    Hello everyone,

    A recent thread reminded me of a script I wrote years ago while learning about the flip-flop operator (aka the range operator in scalar context), and I thought I'd share it in case it helps someone else.

    The script, which I've included below the output, runs through a sequence of true/false values, tests whether the right-hand-side and/or left-hand-side of the operator is evaluated (marked by an asterisk in the table), gets the return value of the operator, and outputs all that in the following handy table. I hope it illustrates the difference between the .. (two-dot) and ... (three-dot) versions of the operator: two dots will immediately evaluate the RHS if the LHS is true, three dots will wait until the next evaluation.

    *** Demonstration of the Flip-Flop Operators *** __A______B____X___ __A______B____X___ 0* .. 0 = 0* ... 0 = 0* .. 1 = 0* ... 1 = 0* .. 0 = 0* ... 0 = 1* .. 0* = 1 1* ... 0 = 1 0 .. 0* = 2 0 ... 0* = 2 0 .. 0* = 3 0 ... 0* = 3 0 .. 1* = 4E0 0 ... 1* = 4E0 0* .. 0 = 0* ... 0 = 0* .. 0 = 0* ... 0 = 1* .. 1* = 1E0 1* ... 1 = 1 0* .. 0 = 0 ... 0* = 2 0* .. 0 = 0 ... 0* = 3 1* .. 0* = 1 1 ... 0* = 4 0 .. 0* = 2 0 ... 0* = 5 0 .. 1* = 3E0 0 ... 1* = 6E0 0* .. 1 = 0* ... 1 = 1* .. 1* = 1E0 1* ... 1 = 1 1* .. 0* = 1 1 ... 0* = 2 0 .. 0* = 2 0 ... 0* = 3 0 .. 1* = 3E0 0 ... 1* = 4E0 0* .. 0 = 0* ... 0 = 1* .. 1* = 1E0 1* ... 1 = 1 1* .. 1* = 1E0 1 ... 1* = 2E0 1* .. 1* = 1E0 1* ... 1 = 1 0* .. 0 = 0 ... 0* = 2 0* .. 1 = 0 ... 1* = 3E0 0* .. 0 = 0* ... 0 = (* = Evaluated)

    Regards,
    -- Hauke D

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

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 perusing the Monastery: (8)
As of 2016-05-31 14:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?