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
Getting man-page info on metacpan and search.cpan.org
1 direct reply — Read more / Contribute
by rockyb
on May 30, 2016 at 20:34

    Suppose I have a module and a command-line interface to that.

    Right now using Module::Build, if I create a directory called /bin those scripts get installed. I recently learn that Metacpan.org will treat POD files (.pod) as documentation for corresponding Perl modules (.pm). But will it do the same for command-line scripts?

    And if I want to link to that command from a readme or the corresponding library, can I do that with L<> and have it link correctly on metacpan?

    I can and will make experiments, but it would save a lot of spurious releases and testing and time waiting for indexing if someone out there just knew what works. Thanks.

Regular Expression on special character and alphanumeric values
5 direct replies — Read more / Contribute
by perlmad
on May 30, 2016 at 12:34

    Hi Monks

    I have a doubt on regular expression below is my code

    my $string="{'totalResultsCount':71-24,'securityList':[{'cusip':'91%27 +9-6.H:Y8','issueDate':'2016-06-02T00:00:00','securityType':'Bill'}"; $string =~ /'cusip':'(\w+\-\$\.\%)'/; print " first is : $1 \n\n";

    output is

    Use of uninitialized value in concatenation (.) or string at sample.pl + line 7. first is :

    Because the "\w+" on supports alphanumeric values but my question is how to include alphanumeric and special character with in the parenthesis , I have tried the above code but i could not get an exact answer , Is there need any changes in Regular expression ???

    Expected output

    first is :912796HY8
dbd::oracle truncation error
3 direct replies — Read more / Contribute
by wal
on May 30, 2016 at 07:42
    hi there i get an truncation error on a field but don't know why
    can anybody help me? thanks
Perl includes to reuse code.
3 direct replies — Read more / Contribute
by Anonymous Monk
on May 30, 2016 at 01:28

    I have created a file called system_mod_includes.pm and the contents of the file are as below:

    # # System Modules # use CGI; use HTML::Template; use File::stat; use File::Type; use File::Temp; use JSON; use Time::Local; use POSIX 'strftime'; use XML::Simple; use XML::Parser; use IO::Socket::INET; use Digest::MD5 qw(md5 md5_hex md5_base64); use Data::Dumper; use Net::SMTP; use Net::SMTP::TLS; use MIME::Base64; use Compress::Zlib; use URI::Escape; use Locale::Country; use Socket; use HTML::Entities; use List::Util qw[min max]; use IO::Uncompress::Gunzip qw(gunzip $GunzipError); use IO::Compress::Gzip qw(gzip $GzipError); 1;

    Then I have another module called Utils.pm. In this module I am using a function of perl module Data::Dumper.

    For example as below:

    print Dumper(%HASH);


    print Data::Dumper(%HASH);

    But this gives me error saying the function Dumper is unknown.

    But use Data::Dumper is already defined in system_mod_includes.pm.

    The error however goes away if I defined use Data::Dumper in Utils.pm as well.

Perl UTF-8 serving HTML5
2 direct replies — Read more / Contribute
by $h4X4_&#124;=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?


    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?


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 );
Perl code help
2 direct replies — Read more / Contribute
by usertest
on May 28, 2016 at 14:34
    I have a requirement to delete certains lines between two patterns (START and END) which contain a string (def) - for example
    Source Data START abc def ghi END START xyz abc END Output should be START xyz abc 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.

Add your question
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!
  • 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.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.