http://qs321.pair.com?node_id=479

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
Looping through an array
No replies — Read more | Post response
by Anonymous Monk
on Sep 27, 2021 at 15:49
    Hi Monks!

    I get this data where I need to get the values, when I loop through it, the values are coming duplicated, cant see where the issue is anymore:
    My test data:
    my $DATA = { "required": [ { "docs": [ { "sec": "123A", "number": "1" } ], "name": "Mary Lou" }, { "docs": [ { "sec": "4567", "number": "1" } ], "name": "John De" }, { "docs": [ { "sec": "8763", "number": "1" } ], "name": "Smith Doe" }, { "docs": [ { "sec": "123B", "number": "1" } ], "name": "Joe De" } ], }; ... for my $dta ( @{ $DATA->{ required } || [] } ){ # if the docs is true if( scalar @{ $dta->{ docs } } ){ for my $doc_data ( @{ $dta->{ docs } || [] } ){ if( $doc_data->{'sec'} ) { # Get sec - number and name } } }else { warn "NO: $doc_data->{'sec'} | $doc_data->{'number'} \n"; # Get sec - numnber and name } } ...

    I am trying to get this:
    "sec": "123A", "number": "1" "name": "Mary Lou" "sec": "4567", "number": "1" "name": "John De" "sec": "8763", "number": "1" "name": "Smith Doe" "sec": "123B", "number": "1" "name": "Joe De"


    Im getting data, but its duplicating:
    "sec": "123A", "number": "1" "name": "Mary Lou" "sec": "4567", "number": "1" "name": "John De" "sec": "8763", "number": "1" "name": "Smith Doe" "sec": "123B", "number": "1" "name": "Joe De" "sec": "123A", "number": "1" "name": "Mary Lou" "sec": "4567", "number": "1" "name": "John De" "sec": "8763", "number": "1" "name": "Smith Doe" "sec": "123B", "number": "1" "name": "Joe De"

    Thanks for looking!
loop iterator in a string
3 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 27, 2021 at 14:22
    Dear Monks,
    I would like to use the ".." as a loop operator but in a string. Is there any simple perl-ish way I can accomplish this? For some reason, I thought perl would support this, but it doesn't.
    my $arr=[1,2,3,4,5,6,7]; my $loop="2,4..5"; splice @$arr,$_,0,999 foreach($loop);

    I want $arr to be [1, 2, 999, 3, 999, 999, 4, 5, 6, 7]. This works fine if you pass in 2,4..5 instead of "2,4..5". I notice that '2', '4'..'5' will also work, but when the .. operator is within string, it does not.

    Thank you for your time!

    Michael

Parsing/regex help required
2 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 27, 2021 at 09:10
    Parsing a HTML page I have the contents of a set of paragraphs, the general form:
    Integer. Text - or more text e.g. 123. The quick brown fox - Jumps over the 123. The quick brown fox Jumps over the 456. The lazy dog - Barked & wagged results $num = 123; $text1 = 'The quick brown fox'; $text2 = 'Jumps over the';
    Right now I'm capturing the three variables I need using index & substr a bunch but was hoping for a cleaner, more perlish solution. TIA
Use of inherited AUTOLOAD for non-method is no longer allowed with Perl 5.34.
4 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 27, 2021 at 04:35

    Hi Monks,

    How to resolve below issue effectively which happens due to use of non-methods, please note that methods have been used at many places in the application

    "inherited AUTOLOAD for non-method is no longer allowed."

    Note that in package i already have it defined, below

    *AUTOLOAD = \&BASE_CLASS::AUTOLOAD;  #this doesn't seem to work

    Any help would be greatly appreciated.

Perl equivalent for Python's request module for REST APIs.
3 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 24, 2021 at 18:45

    Hi Monks,

    Is there an equivalent of the "request" Python module in Perl? The only similar module I see is Rest:: Client, but it hasn't been updated since 2015.

How and where to pass PERL_USE_UNSAFE_INC=1; to Perl while compilation.
3 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 24, 2021 at 12:19
    Hi Monks,

    Can you please tell me How and where should I add PERL_USE_UNSAFE_INC=1; while compiling Perl version 5.34?

    I am aware that the current directory (.) has been removed from @INC because of some security concerns but in my scenario I will have to change a lot of scripts in the application, hence I need to have a dot(.) back in the @INC.

    Any help would be appreacited!

    Thank you

backwards compatible 'die $obj'
2 direct replies — Read more / Contribute
by LanX
on Sep 24, 2021 at 11:52
    Hi

    we have a large infrastructure based on catching die "string" errors and catching them with eval I want to introduce die $err_obj with Try::Tiny for a cleaner handling.

    But for introduction we need to be backwards compatible, i.e. $err_obj should stringify via overload to a classical error string. Before reinventing the wheel I was skimming thru CPAN but couldn't quite find a ready to use solution.

    Any recommendations?

    update

    talking about Throw looks promising ... :)

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

Padding with sprintf changing number
5 direct replies — Read more / Contribute
by sachin raj aryan
on Sep 24, 2021 at 07:12

    Hello Monks Not getting why amount is changing after padding with sprintf.Can anyone tell why this kind of output this line print "$amount i m checking amount before padding,\n"; --> output is 488715 this line print "$padamnt i m checking amount after padding,\n"; ---> output is 0000000000488714

    sub amnt($amn) { my $amount=$_[0]; print "$amount i m checking amount before padding,\n"; my $padamnt = sprintf("%016d",$amount); print "$padamnt i m checking amount after padding,\n"; return $padamnt; }
How to use perl to capture its output while inputting data to an external command
3 direct replies — Read more / Contribute
by wangliang
on Sep 23, 2021 at 22:02

    I need to use perl script to capture output from two programs when inputing some thing to them.

    the first program called sac is used in shell like this:

    $sac SAC> I input some thing and enter SAC> output some content that I need to capture SAC> I type 'quit' and enter $

    the second program called gmt is used in shell like this:

    $gmt parameters I input some thing and enter I input some thing and enter Ctrl+D output some content that I need to capture $
    Thank you!
Perl terminal access to linux server
3 direct replies — Read more / Contribute
by Polyglot
on Sep 22, 2021 at 03:46
    Can Perl provide a direct link to terminal access on a linux webserver, and/or is there a module that would provide the tools for this in similar style to the DBI modules that give direct access to databases?

    Assume that the script were setuid root, so permissions were not an issue.

    I'm aware of the use of backticks, etc. to execute commands within a script, but am wondering if there is a more interactive approach instead of using only pre-scripted commands.

    For the purposes of this question, I'm interested in what is possible more than what might be considered standard practice and/or secure. However, feel free to wax eloquent on the security risks of letting someone use setuid scripts on the server.

    Blessings,

    ~Polyglot~


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":