Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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.

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.

Post a new question!

User Questions
(SOLVED) RegEx: backreferences in character classes
No replies — Read more | Post response
by LanX
on Oct 24, 2021 at 06:50
    Hi

    I'm failing to use relative backreferences in \g-form in character classes.

    Please note how [^\1] still works, but [^\g-1] fails.

    Is there a better syntax within character classes?

    I'm afraid I need to switch to negated look-aheads or hardcode different quotes.

    Debugger demo with perl-de0

    The following regex tries to match quoted substrings in $1, the quote in $2

    DB<164> p $_ __'abc'__"def"__ DB<165> x / ( (['"]) [^\2]*? \2 ) /gx # OK 0 '\'abc\'' 1 '\'' 2 '"def"' 3 '"' DB<166> x / ( (['"]) [^\2]*? \g-1 ) /gx # OK 0 '\'abc\'' 1 '\'' 2 '"def"' 3 '"' DB<167> x / ( (['"]) [^\g-1]*? \g-1 ) /gx # OOPS Invalid [] range "\g-1" in regex; marked by <-- HERE in m/ ( (['"]) [^ +\g-1 <-- HERE ]*? \g-1 ) / at (eval 179)[c:/St\ rawberry/perl/lib/perl5db.pl:738] line 2. at (eval 179)[c:/Strawberry/perl/lib/perl5db.pl:738] line 2. eval 'no strict; ($@, $!, $^E, $,, $/, $\\, $^W) = @DB::saved; +package main; $^D = $^D | $DB::db_stop; / ( ([\'"]) [^\\g-1]*? \\g-1 ) /gx; ' called at c:/Strawberry/perl/lib/perl5db.pl line 738 DB::eval called at c:/Strawberry/perl/lib/perl5db.pl line 3138 DB::DB called at -e line 1 DB<168> p $] 5.032001 DB<169>

    FWIW: \g{-1} instead of \g-1 doesn't help either

    This question is a follow-up to solution wanted for break-on-spaces (w/specifics)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    update

    ) or using (??{..})

    SOLUTION

    escaping the dash inside character classes helps, otherwise it's seen as a range

    DB<170> x / ( (['"]) [^\g\-1]*? \g-1 ) /gx 0 '\'abc\'' 1 '\'' 2 '"def"' 3 '"' DB<171> x / ( (['"]) [^\g{\-1}]*? \g-1 ) /gx 0 '\'abc\'' 1 '\'' 2 '"def"' 3 '"' DB<172> x / ( (['"]) [^\g2]*? \g-1 ) /gx 0 '\'abc\'' 1 '\'' 2 '"def"' 3 '"' DB<173>
Can someone please write a *working* JSON module
3 direct replies — Read more / Contribute
by cnd
on Oct 24, 2021 at 02:16
    JSON::XS is not thread-safe (crashes mod_perl in production) and seems to have a memory leak (I see oob on apache after some time...)
    All the other JSON's are stupidly slow.
    JSON:XS (and all the others) are riddled with "croak" statements - making them unusable in production without wrapping them in "eval()".
    eval makes JSON::XS even slower than JSON::PP

    Philosophically - I strongly disagree with the concept of ever using "croak" on broken input - that's a fast-track way to derail production code... but that's just my opinion (based on a mere 37 years of programming every day).

    The spec looks *really* simple: https://datatracker.ietf.org/doc/html/rfc8259 - with the exception of the unicode mess (which is the prime candidate for many of the croak deaths I observe above, and possibly the memory-leak.)

    Is there any really-good C programmer here, who wants to engrave their name in history by writing a correct and stable JSON module, that can be safely used in scripts without any eval() needed ?

    Suggestion: code some flags so that we users can specify the behaviour (in case some masochist actually wants to croak themselves), and other useful features (e.g. python interoperability including NaN, -inf, and other non-standard stuff that despite-not-being-standard everyone still wants/uses..., stripping of javascript-unsafe or broken unicode chars, pretty-printing, canonical output, ...)
Variable with curly braces?
2 direct replies — Read more / Contribute
by wyt248er
on Oct 24, 2021 at 02:10

    The perl guide document "perlpacktut" contain the following command statements, which look like assignments.

    $UTF8{Euro} = pack( 'U', 0x20AC ); # Equivalent to: $UTF8{Euro} = "\x{20ac}"; $Unicode{Euro} = unpack( 'U', $UTF8{Euro} );

    The tokens `$UTF8{Euro}` and `$Unicode{Euro}` seem to be treated as variables. However, the typical perl variables I am familiar with are of the form $v or ${v} but not $v{u}. In fact, the following code ends up with an error.

    #!/usr/bin/perl my $v{u} = "hello"; print($v{u}, "\n");

    syntax error at ./test.pl line 3, near "$v{u"

    What are `$UTF8{Euro}` and `$Unicode{Euro}`? Which perl guide document explain this kind of token?

    Thank you in advance.

How to convert between Unicode codepoint and UTF8 character code on Perl?
2 direct replies — Read more / Contribute
by wyt248er
on Oct 24, 2021 at 01:32

    I would like to convert between Unicode code points and UTF-8 character codes.

    For example, the Unicode code point for the GREEK SMALL LETTER PI is U+03C0, and its UTF-8 character code is 0xCF80. So, if the string "U+03C0" (or "0x03C0") is entered, then I want the string "0xCF80" to be printed (without quotes). If the string "0xCF80" is entered, then I want the string "U+03C0" (or "0x03C0") to be printed (without quotes). Note that the desired output is NOT a character itself but a string showing the character code.

    By the way, if your terminal is configured to display Unicode wide characters, then the following commands will show you the GREEK SMALL LETTER PI.

    perl -l12e 'print(chr(0x03c0))' -C perl -l12e 'print(pack("U0W*", 0xCF, 0x80))' -C

    Thank you in advance.

STDIN usage in interactive debug
1 direct reply — Read more / Contribute
by abdan
on Oct 23, 2021 at 23:26
    Why in interactive debug cannot do input:
    DB<1> @ln=<STDIN>; <"FILE" DB<2> print "$ln[0] $ln[1]"
    How to have it working directly
solution wanted for break-on-spaces (w/specifics)
7 direct replies — Read more / Contribute
by perl-diddler
on Oct 23, 2021 at 16:53
    I wanted to break input on spaces in an RE, but according to additional rules:
    1) Spaces between single or double quotes are not broken in.
    2) A single BS ignores any special-ness of the next character.
    3) Any quoted string is considered terminated by an end-of-string.

    I think (hope) that's it. I've evolved an example I found on this board, but don't remember the original URL. It had some failing, where it didn't satisfy the above additional rules. That example is:

    qr{((?>\\[^\\])|"(?:\\\\"|[^\\"]*)"|(?:'[^\\']*'|\S+))+}

    My test inputs included "embedded case/line# answer,text" and which assumes testprog can remove '#comments' are:

    # ln#, ans, test text 1 3,This is simple. 2 3,This is "so very simple". 3 4,This "is so" very simple. 4 2,This 'isn\'t nice.' 5 2,This "isn\"t nice." 6 3,This 'isn\\'t nice.' 7 3,This "isn\\"t nice." 8 2,This "isnt wrong." 9 2,This 'isnt wrong.' 10 3,This 'isn\\'t wrong. 11 3,This "isn\\"t wrong. 12 3,This isn\'t horrible. 13 3,This isn\"t terrible. 14 3,This \"isnt unnice.\" 15 3,This \'isnt unnice.\' 16 2,This 'is not unnice.' 17 2,This "is not unnice." 18 3,a "bb cc" d

    So far, my sample test-runs show lines/cases 4+5 to be in error.

    That is the main problem, the rest is specific to my testing program, which I don't care about, but can be used to run the sample RE against the test cases (which are included in the source of the testing program). The test prog's output follows (Note -- like the testprog, don't care about the format, it was just to show me pass/fails"):

    ResByLn:{ln=>1, wanted=>3, got=>[3]},[" p "] ResByLn:{ln=>2, wanted=>3, got=>[3]},[" p "] ResByLn:{ln=>3, wanted=>4, got=>[4]},[" p "] ResByLn:{ln=>4, wanted=>2, got=>[3]},["FAIL:<This "isn\"t nice.">"] ResByLn:{ln=>5, wanted=>2, got=>[3]},["FAIL:<This 'isn\\'t nice.'>"] ResByLn:{ln=>6, wanted=>3, got=>[3]},[" p "] ResByLn:{ln=>7, wanted=>3, got=>[3]},[" p "] ResByLn:{ln=>8, wanted=>2, got=>[2]},[" p "] ResByLn:{ln=>9, wanted=>2, got=>[2]},[" p "] ResByLn:{ln=>10, wanted=>3, got=>[3]},[" p "] ResByLn:{ln=>11, wanted=>3, got=>[3]},[" p "] ResByLn:{ln=>12, wanted=>3, got=>[3]},[" p "] ResByLn:{ln=>13, wanted=>3, got=>[3]},[" p "] ResByLn:{ln=>14, wanted=>3, got=>[3]},[" p "] ResByLn:{ln=>15, wanted=>3, got=>[3]},[" p "] ResByLn:{ln=>16, wanted=>2, got=>[2]},[" p "] ResByLn:{ln=>17, wanted=>2, got=>[2]},[" p "] ResByLn:{ln=>18, wanted=>3, got=>[3]},[" p "]
    The test prog originally was written to handle multiple RE's, but I threw out all the ones that were less successful than the 1 remaining -- but that's why some things are bracketed like \got\, "array values" in the test-prog output.

    For FAIL cases, I had it print the failing text, just to be sure I was failing on the line I thought I was.

    The test prog takes an optional '-f' that filters output to only display the FAILing cases.

    I repeat -- my main problem is finding an RE that I can use to break the test-cases into the correct number of capture-args (as broken by unquoted spaces). Am including my test prog, below for those that want to use it or see what I did. But the help I need is in fixing the 'RE' so all the test cases work. Thanks for any help!

    #!/usr/bin/perl use P; # vim=:SetNumberAndWidth if ( -t 0) { # unlikely to provide reliable test case open(STDIN, "<&=", "main::DATA") || die "opening internal tests: $!" +; } my @lines = grep { defined $_ and ! /^\s*#/ } (<main::DATA>); my @regex = ( qr{((?>\\[^\\])|"(?:\\\\"|[^\\"]*)"|(?:'[^\\']*'|\S+))+} + ); my $ln=0; my $norm=0; my @ResByLn; sub lnout($$$) { my ($ans,$outp, $lnp) = @_; bless {wanted=>$ans, got=>$outp, ln=>$lnp}, q(ResByLn:); } sub txt($) { local $_=shift; my (undef, undef,$txt)=m{^\s*(\d+)\s+(\d+),(.*)}; $txt; } my $only_fails = @ARGV && $ARGV[0] eq '-f' ? 1 : 0; for (@lines) { ++$ln; my ($lnnum, $ans,$_)=m{^\s*(\d+)\s+(\d+),(.*)}; my @got; for (my $r=0; $r<@regex; ++$r) { my $reg = $regex[$r]; my @out = grep {$_ } m{$reg}g; my $cnt = 0+@out; push @got, $cnt; } $lnnum and push @outs, lnout($ans, \@got, $lnnum); } for my $o (@outs) { my $ans = $o->{wanted}; my @out = @{$o->{got}}; my $ln = $o->{ln}; my @rts; #results for (my $i=0;$i<@out;++$i) { $rts[$i] = do { if ($ans==$out[$i]) { " p " } else { "FAIL:" . do { my $txt = txt($lines[$ln]); chomp $txt; "<$txt>"; }; } }; } my @output_args=("%s,%s",$o,\@rts); # * Shows line-number, result-wanted, result-got + test-status # * if test-status is FAIL, shows corresponding test-txt between <> +(carats) # unless ($only_fails) { P @output_args; } else { my @fails = grep /FAIL/, P @output_args; foreach (@fails) { P "%s", $_; } } } # vim: ts=2 sw=2 ai number __DATA__ # ln#, ans, test text 1 3,This is simple. 2 3,This is "so very simple". 3 4,This "is so" very simple. 4 2,This 'isn\'t nice.' 5 2,This "isn\"t nice." 6 3,This 'isn\\'t nice.' 7 3,This "isn\\"t nice." 8 2,This "isnt wrong." 9 2,This 'isnt wrong.' 10 3,This 'isn\\'t wrong. 11 3,This "isn\\"t wrong. 12 3,This isn\'t horrible. 13 3,This isn\"t terrible. 14 3,This \"isnt unnice.\" 15 3,This \'isnt unnice.\' 16 2,This 'is not unnice.' 17 2,This "is not unnice." 18 3,a "bb cc" d __END__
    P.s. I could probably do this easily in a parser -- But I'm trying to fit it into an RE, as I think it should be possible, and/or I just maybe a self-masochist. :-0 P.P.s originally had 4,5,6,7 as wrong, but realize 6+7 were right, so corrected things (I hope).
About perl -d -e "1;"
1 direct reply — Read more / Contribute
by abdan
on Oct 23, 2021 at 14:28
    I'm noobie using perl -d -e "1;" I installed Term::readline and thought the same as using Bash on terminal the backspace is undo the last key input/pressed, but the perl debug behave undo entire current line i.e. delete the line How to have it the same as doing Bash on terminal, i.e. the backspace is undo the last character input ? Thanks much
DBI trace has limited output
3 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 23, 2021 at 11:43
    I've enabled DBI trace with level 15 so it shows the full trace but it has no effect.the output remains the same whether level 1 or 99. In particular I'd like to capture the SQL queries with the values interpolated.In other machines I do.In this case I have a CGI script called by Apache which accesses the database and does some queries. It's a bit of an old system with DBI version 1.64. I can't make any modifications to the system because it's production. Do you think it's because of the old DBI version?
Dump the key value pairs in a hash
4 direct replies — Read more / Contribute
by byrnejb
on Oct 22, 2021 at 16:12
    I am not a perl programmer. I do have to maintain a legacy perl application. Something has changed in the environment so that it no longer correctly reports the status of public key certificates. I need to discover what that change is and how to accommodate it. The critical piece of code appears to be:
    sub genHTMLTableRow { my $self = shift; my $args = shift; my $e = shift; my $ser = $e->{serial}; my $pem = $ser.'.html'; . . .
    What I wish to do for debugging purposes is to dump the actual contents of $self, $args, and $e. I tried the naive approach of using print "$self\n"; but that just gave me this:
    CSP=HASH(0x800b1e060)
    I tried to use the map function
    print map { "Key: $_ has Value:$self{$_}\n" } keys $self;
    I clearly do not understand the naming conventions of perl variables as this usage of map causes the error:
    Global symbol "%self" requires explicit package name (did you forget + to declare "my %self"?) at /root/bin/rcsp/blib/lib/CSP.pm line 861
    What is the perl way of displaying the contents of $self, $args, and $e?
Defining directory/Datas the perl script should work with
3 direct replies — Read more / Contribute
by perlnewbie
on Oct 21, 2021 at 09:20

    Hi all, I want to write a script which should use extern programms for MRI-preprocessing, doing several processing commands to a bunch of data. For example: running brain extraction from the command line in linux I would write bet <inputimage> <nameforoutputimage> -m Then the programm bet in fsl would be opened and skull stripping would be performed, the output image would be saved in the same folder of the inputimage. In my script I want the outputimage to be saved in an other folder. Could anyone help me with this? I think there a lot of syntax errors ...

    #!/usr/bin/perl use strict; use warnings; use diagnostics; my $dir = "usr/bin/Test_Protokoll" my $001= "prob_001"; my $002= "prob_002"; my $003= "prob_003"; @filter: ("001", "002", "003"); #Brainextraction foreach ("001", "002", "003") { system ("bet SYNT_MPRAGE_T1_PD_B0corr YNT_MPRAGE_T1_PD_B0corr_brain + -m"); }

Add your question
Title:
Your question:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":


  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2021-10-24 19:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (89 votes). Check out past polls.

    Notices?