Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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
scope of variables in a sub
5 direct replies — Read more / Contribute
by frontrange
on Sep 23, 2020 at 18:43

    I feel like this is a really stupid question, but I'm sort of at my wit's end and really hoping someone can point out what it is that I'm missing. I have a subroutine and have some variables that are not in scope as I expected and can't figure out why, any wisdom on what I'm missing would be greatly appreciated

    sub make_ap_stanza () { my $policy = $_[0]; my $vsname; my $vsowner; my $owner; my $group; my $regexp; my $volumeset; my $volumeset_owner; print "Policy:$policy\n"; my @attrs = @{ $Policy_HoA{$policy} }; foreach(@attrs){ if(($vsname) = ($_ =~ /vs_name =\s+(.*)/)){ print "VSNAME:$vsname\n"; } elsif(($vsowner) = ($_ =~ /vs_owner =\s+(.*)/)){ print "VSOWNER:$vsowner\n"; } elsif(($filter) = ($_ =~ /filter_name =\s+(.*)/)){ #print "FILTER:$filter\n"; my @attrs = @{ $RegexHoA{$filter} }; foreach(@attrs){ if(($regexp) = ($_ =~ /REGEXP =\s+(.*)/)){ print "REGEXP:$regexp\n"; } elsif(($owner) = ($_ =~ /OWNER =\s+(.*)/)){ print "OWNER:$owner\n"; } elsif(($group) = ($_ =~ /GROUP =\s+(.*)/)){ print "GROUP:$group\n"; } } } } print "foo $vsowner\n"; }

    So I declared a bunch of variables in the beginning of the code block, the nested routines populate them, but outside of the foreach loop, the variables are uninitialized???

    [test1] change_timeout = 10 comment = foobar logmask = 0xffffff parent = thanos policy = perms policy = foobar queue_depth = 32 supports_hsm = yes path = /test1 Policy:perms REGEXP:image.([0-9]+).([0-9]+) OWNER:ANY GROUP:ANY VSNAME:dyn{2}{1} VSOWNER:root Use of uninitialized value $vsowner in concatenation (.) or string at +./getconf line 395. foo

    It looks like no matter how long I stare at this, I just can't understand why my variables are not properly declared

RFC: How did I do writing my first test?
3 direct replies — Read more / Contribute
by Lady_Aleena
on Sep 23, 2020 at 16:40

    The test I needed help with in Can Test::MockObject mock a file? is finished. I would like to know how I did writing it.

    I got some advice on looping I did not quite get a handle on, but this is a short test for a little module.

    Here are links to the Fancy::Open module and Fancy::Open pod (unfinished).

    My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.16.3 or 5.30.0 on web host depending on the shebang.

    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena
wxPerl and MacOS
1 direct reply — Read more / Contribute
by sciurius
on Sep 23, 2020 at 04:38


    I have a neat perl application that uses Wx (wxPerl). It runs on Linux, Windows and MacOS.

    One of the things currently missing on MacOS is the ability to respond to Finder commands. For example, double-clicking on an associated document should start the application. The association is dealt with in the app's Info.plist and works -- double-clicking the document starts the application but the application always starts with its main window, as if the application was double-clicked instead of the document.

    The wxWidgets docs suggest that I should override Wx::App::MacOpenFiles but this function does not seem to be in wxPerl.

    Are there any PerlMonks using wxPerl on MacOS that could shed some light on this?

Unicode vulgar fraction composition
3 direct replies — Read more / Contribute
by raygun
on Sep 22, 2020 at 20:05
    Greetings, Monks.

    Unicode has precomposed fractions, such as VULGAR FRACTION THREE EIGHTHS (U+215C), that Unicode::Normalize's NFKC or NFKD function decomposes into the string "3\N{FRACTION SLASH}8".

    But I can't get any function in that module to go the other way, turning the decomposed form back into the vulgar fraction. Clearly the module is aware of the equivalency of these forms. And while it wouldn't be difficult to write my own function to handle these fractions (there are only a dozen or so), rolling my own code to do a translation that already lives inside Unicode::Normalize seems like a wrongheaded approach. Is there a standard mechanism or tool for composing arbitrary fractions into precomposed ones, where such precomposed ones are available?

Test::Deep::noclass propagation
1 direct reply — Read more / Contribute
by choroba
on Sep 22, 2020 at 18:57
    Let's pretend we have an object:
    my $o = bless { x => bless ['y'], 'My::Value' }, 'My::Class';

    I want to verify that the object when cursed recursively will consist of

    { x => [$v], # where $v is 'y' or 'z' a => 'b' } # if a exists at all

    I thought Test::Deep's noclass should be able to tell me.

    cmp_deeply $o, noclass(subhashof({ a => 'b', x => subbagof('y', 'z') } +));

    But it doesn't work:

    # Failed test at ... # Comparing $data->{"x"} as a SubBag # got : My::Value=ARRAY(0x561fbd1fe3a8) # expect : An array to use as a Bag

    Without an object, it works nicely:

    cmp_deeply { x => ['y'] }, subhashof({ a => 'b', x => subbagof('y', 'z') });

    I asked about it in #freenode and was pointed towards Test2::Tools::Compare. It was the first time I played with it, so maybe my code is a bit clumsy, but I was able to write a test that passes:

    use Test2::Tools::Compare qw{ like hash item field in_set DNE bag }; my $o = bless { x => bless ['y'], 'My::Value' }, 'My::Class'; like({ x => ['y'] }, hash { field a => in_set(DNE(), 'b'); field x => bag { item in_set('y', 'z') }; } ); like $o, hash { field a => in_set(DNE(), 'b'); field x => [ in_set('y', 'z') ]; };

    How to do the comparison with Test::Deep? I tried adding another noclass to the value, or even noclass(useclass(...)), but it always failed similarly.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
pseudocode for getting data from table
2 direct replies — Read more / Contribute
by shabird
on Sep 21, 2020 at 23:55

    Hello monks, do we evaluate pseudocode for certain problem here? if yes i have written a pseudocode and i will post it here so that you guys can evaluate if it is a right way for pseudocoding or not?

Open new window after form submit
4 direct replies — Read more / Contribute
by tel2
on Sep 21, 2020 at 23:10
    Hi Monks,

    I'm not sure how Perlish this will work out to be, but Ive got an existing Perl CGI application which contains a form, which when submitted with the appropriate values, redisplays that same form (as per the form action).  That part works fine, but Id like to make it so the submit opens a separate window (browser tab) at the same time, and displays a table in it.  The source of the HTML for that table is in a variable (say $table), not in a file.

    The user would then either copy data from that table on the new window to the clipboard, or print the webpage, and when finished the user would close the window leaving them with the original window (which contains the form) only.

    (One reason I want a new window is, it has to contain the table only, because I don't want any thing else like navigation controls being included in any printout, and I don't want the user to have to click the browser's back button to get back to the main window.)

    I know I could write the HTML in $table out to a file, then display a link to that file (e.g. <a href="table.txt" target="_blank">Click for printable table</a>) on the main window after the submit, which the user could then click, but thats an extra step for the user which I definitely want to avoid, which is why Im asking for help.

    Id also prefer not to write the table to a file, mainly because then I have to somehow delete it after its been used, which is just more work, but Im flexible on this.

    Any suggestions on how to achieve the above, please?

    I'm not wanting any jQuery solutions, but I might be willing to tolerate a bit (or even a byte) of plain JavaScript if required.  I'm using Perl's CGI module, so maybe it has something which could help.

    I've also posted this question here:

    Thanks. tel2

mixed case with match
6 direct replies — Read more / Contribute
by catfish1116
on Sep 21, 2020 at 15:03

    Quick question. How do I get the script below to match on mixed case:

    my $sum = 0; while ( <>) { $sum++; last if m/perl/; } print "There were $sum lines until I found perl\n";

    TIA The Catfish

strftime does not handle Unicode characters in format argument properly (at least, not consistently)
2 direct replies — Read more / Contribute
by Bruder Savigny
on Sep 21, 2020 at 14:36

    I tried to use a UTF-8 non-breaking space (between day and name of month) in the format argument of POSIX::strftime, and hit (with Perl v5.32.0 and a UTF-8-encoded script file, without any non-default encoding settings) upon the following two oddities:

    1. a non-breaking space alone comes out as something unprintable (according to Emacs, Unicode 65533 (decimal) REPLACEMENT CHARACTER, but when examined in a hex-mode, looks like hexadecimal EFBFBD)
    2. when other non-ASCII characters figure in the format, they come out correctly, and this seems "infectious": the non-breaking space then comes out correctly as well! However, in that case, a string that is concatenated to what strftime returns gets garbled (perhaps erroneously encoded from an assumed iso-latin-1 (but really already utf-8) to utf-8), which does not happen in case 1

    These behaviours can be demonstrated with the following script (The comments apply to the transparent space character in the format; the innocent-looking - inner, i.e. not syntactical - quotes in lines 3 and 4 are Unicode LEFT and RIGHT SINGLE QUOTATION MARK, the same as in the $string):

    use POSIX qw(strftime); $string = 'hailed an ber cab on '; @t = (0, 0, 0, 23, 5, 2020, 4); print $string . strftime( '%d/%b', @t), "\n"; print $string . strftime( '%d%b', @t), "\n"; # UTF-8 nbsp print $string . strftime('%d%b', @t), "\n"; # UTF-8 nbsp print $string . strftime('%d %b', @t), "\n"; # ASCII space

    This outputs (line numbers added):

    1 hailed an ber cab on 23/Jun 2 hailed an ber cab on 23&#65533;Jun 3 hailed an über cab on 23Jun 4 hailed an über cab on 23 Jun

    Note that

    • the #65533; entity in line 2 of the output is what the HTML renderer here makes of the character I described above (apparently agreeing with Emacs)
    • the space in line 3 between 23 and Jun is a proper UTF-8 non-breaking space (as in the code), and
    • the one in line 4, an ASCII space (also as in the code).

    (I have deleted complaints about the wide characters in print for line 3 and 4 for brevity.)

    I am guessing, rather vaguely, that this is down to strftime essentially being the C function and the latter not being Unicode-aware and maybe also the way that Perl identifies how strings are encoded and then "upgrades" some so as to harmonise their encodings (in this case under a wrong assumption), but ... :

    The behaviour with a non-breaking space alone vs. (also) other non-ASCII characters seems definitely inconsistent. Why is the behaviour different between the non-breaking space and typographical quotation marks, which are all outside the ASCII block?

    Also, can anything be done about it, i.e. is it possible to use non-breaking spaces in a format for strftime such that they come out correctly (and without having to resort to inserting extra - likely unwanted - non-ASCII characters), and is it possible to use any non-ASCII character in those format argument without confusing Perl? (Actually, I can think only of non-breaking spaces as useful, but other cultures may very plausibly have other use cases.)

Need advice in for perl use as awk replacement
6 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 21, 2020 at 11:58
    Hi Monks,

    programming since very long and knowing quite a number of languages (like C/C++, python, java, javascript, ...), I am however very new to perl. The need for perl came form a quite complex bash script which needs also some data mangling. I started with awk (as usually) but came to one point where AWK became very heavy to handle it. So I wanted to give perl a try.

    The script (in this part) needs to analyze SYSLOG logs. With the help of goole and some tutorials I came up with:

    perl -n -e " /^(([a-zA-Z]+\s+[0-9]+\s+[0-9]+[:][0-9]+[:][0-9]+)\s+([0-9a-zA-Z-]+)\s ++([^:[]+)(\[.*?\])?\s*[:]\s*(?:([^:]*)[:])?\s*(.*))\$/ && ( \$m=(\$1, +\$2,\$3,\$4,\$5,\$6,\$7) ) && print \$3 " "$SYSLOG"

    which works fine for testing and printing the system name of each syslog record. But the script is to be more complex (actually it is a bash library function), so I need to filter. Also as a test I did then:

    perl -n -e " /^(([a-zA-Z]+\s+[0-9]+\s+[0-9]+[:][0-9]+[:][0-9]+)\s+([0-9a-zA-Z-]+)\s ++([^:[]+)(\[.*?\])?\s*[:]\s*(?:([^:]*)[:])?\s*(.*))\$/ && ( $3 =~ /^s +ystemname$/) && print \$3 " "$SYSLOG"

    But this does not work. I understand that this is because $3 is overwritten by this new regex testing. So I tryed to assign to a variable like:

    perl -n -e " /^(([a-zA-Z]+\s+[0-9]+\s+[0-9]+[:][0-9]+[:][0-9]+)\s+([0-9a-zA-Z-]+)\s ++([^:[]+)(\[.*?\])?\s*[:]\s*(?:([^:]*)[:])?\s*(.*))\$/ && ( \$m=(\$1, +\$2,\$3,\$4,\$5,\$6,\$7) ) && ( $3 =~ /^systemname$/) && print \$m[3 +]" "$SYSLOG"

    but get only empty lines as well.


    1. am I understanding correctly that this scrip timplicitely doe s$_ =~ before the initial regex ?
    2. do I really need the first capturing group for the full line if I also need to acces the full line ($_ seems not to wrok) ?
    3. How do I access the groups of the first regex at the end print ?

    Even if you think there is a better way, I do also want to understand why this way does not work as expected. In the final script there will be dynamic testing (so there can be additonal && ( $x =~....) &&

    Many thanka,

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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (4)
    As of 2020-09-24 08:31 GMT
    Find Nodes?
      Voting Booth?
      If at first I dont succeed, I

      Results (132 votes). Check out past polls.