Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
go ahead... be a heretic
 
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. 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
variables /interpolations inside PPI::Token::Quote::Double quoted string
1 direct reply — Read more / Contribute
by Anonymous Monk
on May 19, 2013 at 14:07

    How do I get $_ out of PPI::Token::Quote::Double using some PPI/PPIx module/method?

    Say I have

    #!/usr/bin/perl -- use strict; use warnings; use PPI; use PPI::Dumper; # Load a document my $Module = PPI::Document->new( \q{ print "hi $_\n"; }); print join "\n", PPI::Dumper->new( $Module )->list , "\n"; __END__ PPI::Document PPI::Token::Whitespace ' ' PPI::Statement PPI::Token::Word 'print' PPI::Token::Whitespace ' ' PPI::Token::Quote::Double '"hi $_\n"' PPI::Token::Structure ';' PPI::Token::Whitespace ' '

    How do I get $_ out of PPI::Token::Quote::Double using some PPI/PPIx module/method ?

simple multithreading with curl
3 direct replies — Read more / Contribute
by Anonymous Monk
on May 19, 2013 at 13:48

    hi monks, i seek your wisdom,i am pretty new to perl

    im using strawberryperl on windows xp to download multiple html pages,i want each in a variable

    right now im doing this but as i see it, it gets one page at a time, and doesent go to the next until the current is downloaded

    my $page = `curl -s http://mysite.com/page -m 2`; my $page2 = `curl -s http://myothersite.com/page -m 2`;

    there are about 4 links in total, so i wanted to keep it as simple as possible,

    looked into parallel::forkmanager, but couldnt get it to work also tried to use the windows command start before curl but that doesent get the page is there a more simple way to do this?

    thank you in advance
Checking number in file name
3 direct replies — Read more / Contribute
by Anonymous Monk
on May 19, 2013 at 10:52
    Hi Monks!
    I have to check if an account number is part of a file name and then do some processing if they are a match, but I can't find the best way of doing this, here is a sample code to simulate what I am trying to do:
    #!/usr/bin/perl use strict; use warnings; my $filename = "000231263444_01_XY_20130110_061717.txt"; #my $filename = "17034513_01_WQ_20130511_053551.txt"; $filename =~/(^\w+)_(\w{1,2})_(\w{1,2})_(\w+)_(\w+)\.txt$/i; my $accountnumber = $1; #test condition #my $accountnumber = "0"; print "\n *$accountnumber* \n"; #if($filename=~/$accountnumber/gi) { if($accountnumber=~/$filename/gi) { print "\n Found - *$accountnumber* - *$filename*\n"; }else{ print "\n Not Found - *$accountnumber* - *$filename*\n"; }
    Thanks for looking!
Scalar followed by parenthetical...
3 direct replies — Read more / Contribute
by Anonymous Monk
on May 18, 2013 at 22:15
    Why doesn't the following produce an error??
    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @h = ( { A => 1, B => undef }, { A => 2, B => 2 }, { A => 2, B => undef }, ); $_->{B} ||= $_{A} foreach @h; # Bug! print Dumper(\@h);
What's missing here when I use JIRA::Client to update a custom field in an issue?
No replies — Read more | Post response
by sailortailorson
on May 18, 2013 at 20:06

    Greetings thou Nacreous Natives

    I am trying to use JIRA::Client to update a Jira issue, with partial success.

    I have an issue of type "test subcase", which represents the use of a particular test case in a test plan. I am trying to change the custom field there called 'State', between these values: "Passed", "Failed", "In Progress", "Can't Test".

    So, I managed to do the update with Perl: and looks (nominally) the same as when I manually do the change through a dialog called "Change Test Case State". However, some things that happened when I made the change manually are missing when I do it with Perl. It must be because there is something I do not know to do either with a different field, or with some aspect of the custom field (or both).

    When I do the update manually, via the web client, the change propagates through the display of the state of the test subcase everywhere else, namely the test plan and the parent test case. When I do the update with my Perl script, the change shows up reliably in "Change Test Case State" dialog of the target test subcase, as well as in the history of that subcase, but there is no propagation of the new state to the display of the test subcase's state in other views. In those other views, the old value of 'state' persists (and disagrees with the value in the "Change Test Case State" dialog of the target test subcase.)

    I also notice that the history entry is different between a manual change and a change using Perl. In a manual change from "In Progress" to "Can't Test", the History entry looks like this:

    Tester, Intrepid made changes - Today 2:22 PM Status Open [ 1 ] Open [ 1 ] Test Case State In Progress { "state":"Can't Test","tp":"","tp +Start":"false"}

    In a change from "In Progress" to "Can't Test", made by my Perl, the History entry looks like this:

    Tester, Intrepid made changes - Today 2:38 PM Status Open [ 1 ] Open [ 1 ] Test Case State In Progress Can't Test

    Here is the significant part of the code, that does the update:

    $jira->progress_workflow_action_safely( $s_vetted_key, 'Change Test Ca +se State', {custom_fields => { 'customfield_10213' => { '0' => $s_tar +get_state}}}) ;

    I tried to diagnose the problem by comparing a DataDumper of the issue retrieved by JIRA::Client::get_issue both after a manual update and one after a perl update, but there is no significant difference between them.

    How can I do this correctly with PERL, so that it behaves more like the manual update, and lets my boss and teammates see that I have tested and recorded my results?

    Thank you.

    -sailortailorson

    Here is all the code:

    #!/usr/bin/perl -w # Sample Perl client accessing JIRA via SOAP using the CPAN # JIRA::Client module, to update a test case # use strict; use warnings; use Data::Dumper; use DateTime; use JIRA::Client; use Term::ReadKey; $|++; my $ra_states = { a => 'Passed', b =>'Failed', c =>'In Progress', d => + q{Can't Test}, q => 'quit'}; my $s_jira_url = 'https://jira.thankless_employer.com'; my $s_jirauser; my $s_passwd; print "\nCorp. login?:\t"; $s_jirauser = ReadLine(0); chomp $s_jirauser; ReadMode(2); print "\nCorp. passwd?:\t"; $s_passwd = ReadLine(0); chomp $s_passwd; ReadMode(0); print "\nThanks...\nNow connecting to $s_jira_url.\n"; my $jira = eval{JIRA::Client->new($s_jira_url, $s_jirauser, $s_passwd) +} or die("Could not log into $s_jira_url. Here's the problem: $@"); print "Connected.\n"; my $s_target_state = &choose_state($ra_states); my $command = "."; if ($s_target_state ne "quit") { print "\nEnter a liat of test subcase id's (or \"help\", if you ne +ed help, or \"state\" if you want to change the target state):\n\n"; } else { exit; } my $rh_test_subcases_to_update = {}; while( $command && $command !~ /^N(o|eg|yet|ein|icht)?|Q(uit)?/i ) { print "\n$s_target_state >> "; $command = <STDIN>; chop $command; if ( $command =~ /^help/i) { help(); } elsif ( $command =~ /\bstate\b/i) { $s_target_state = &choose_state($ra_states); } elsif ( $command && $command !~ /^N(o|eg|yet|ein|icht)?|Q(uit)?/i) { my $s_raw_id; my @a_raw_ids = split /(?:\s+|\s*?,\s*|\s*?;\s*)/, $command; foreach $s_raw_id (@a_raw_ids) { my $issue; my @a_errors; if ($s_raw_id !~ s/^((DS)?-)?(\d+)$/DS-$3/i) { push @a_errors, "$s_raw_id does not look like an issue + Key." } else { unless ($issue = eval{ $jira->getIssue($s_raw_id)}) { push @a_errors, "issue $s_raw_id could not be foun +d in $s_jira_url: $?"; } else { #unless ( $issue->{type} eq 'Test Subcase' ) #{ # push @a_errors, "issue $s_raw_id is not a 'Te +st Subcase', but a '" . $issue->{type} . "'."; #} unless ( $issue->{type} == 14 ) { push @a_errors, "issue $s_raw_id is not a '14' +, but a '" . $issue->{type} . "'."; } unless ( $issue->{assignee} eq $s_jirauser ) { push @a_errors, "issue $s_raw_id is not assign +ed to '$s_jirauser', but to '" . $issue->{assignee} . "'."; } } } if ( scalar @a_errors == 0 ) { if (not exists $rh_test_subcases_to_update->{$s_target +_state}) { $rh_test_subcases_to_update->{$s_target_state} = [ +]; } push @{$rh_test_subcases_to_update->{$s_target_state}} +, $s_raw_id; } else { print "\nI cannot set the state of $s_raw_id to $s_tar +get_state for the following " . (scalar @a_errors > 1 ? "reasons" : " +reason") . ":\n"; print "\n" . join "\n", @a_errors; print "\n"; } } print "Alright. Any more?\n"; } } print "\n$s_jirauser has ended the list.\n"; foreach $s_target_state (keys %{$rh_test_subcases_to_update} ) { foreach my $s_vetted_key ( @{$rh_test_subcases_to_update->{$s_targ +et_state}} ) { $jira->progress_workflow_action_safely( $s_vetted_key, 'Change + Test Case State', {custom_fields => { 'customfield_10213' => { '0' = +> $s_target_state}}}) ; print "\nChanged test subcase '$s_vetted_key' to '$s_target_st +ate'."; } } exit; sub choose_state { my $ra_states = shift; my ($raw_entry, $s_key, $s_state_choice); $s_key = ''; while( not (exists $ra_states->{$s_key}) ) { print "\nPlease choose a target state (by letter) from the followi +ng list:\n\n"; foreach $s_key (sort keys %{$ra_states}) { print sprintf "%s.\t%s\n", $s_key, $ra_states->{$s_key}; } print "\n"; $raw_entry = <STDIN>; chop $raw_entry; $raw_entry = lc($raw_entry); if ( $raw_entry =~ /^help/i) { help(); } elsif ( exists $ra_states->{$raw_entry} ) { $s_key = $raw_entry; $s_state_choice = $ra_states->{$s_key}; print "\n$s_jirauser chose $s_key: $s_state_choice...\n\n" +; } else { print "\nSorry, I did not understand your choice: $raw +_entry\n"; } } exit if $s_state_choice eq 'quit'; return $s_state_choice; } sub help { print "\nThis is a utility to do small bulk updates from the c +ommand line on jira test subcases.\n"; print "\nFirst, choose the state you want to change the curren +t group of test subcases to.\n"; print "\nThen, enter the test subcase IDs, either in one long +list, or pressing \"Enter\" after each one.\n"; print "\nYou can change state for assignment at any time, and +subsequently entered test subcase id's will be set to that state.\n"; print "\nTo prevent errors, this utility checks that the reque +sted issue exists, is a test subcase, and is assigned to you before"; print "\nit actually makes the change. If for some reason it f +inds a problem, it alerts you to the problem so you can correct the"; print "\nchoice."; print "\npress [Enter] to continue...\n"; my $nothing = <STDIN>; }
Anti-aliasing Alpha Channel in GD
No replies — Read more | Post response
by shawnhcorey
on May 18, 2013 at 19:27

    I'm using GD to create some images that will blend with a transparent background but things aren't working out right. I'm hoping someone can point out what is wrong.

    Here is the code; as you can see there's no anti-aliasing at all.

Yet more Try::Tiny problelms
1 direct reply — Read more / Contribute
by dd-b
on May 18, 2013 at 17:39

    This may be an interaction with Log::Log4perl

    $logger->trace("Point 9.85"); # create an API notice try {$self->publish_notice( $subject->upload->mapping->map_id, $csv_row->row_id, q(InvalidValue), $csv_row->grain, q(Domain::Products), $item_name // '' );} catch { $logger->trace("point 9.9"); $logger->logdie("Notice failed: ",Dumper($_)); $logger->trace("point 9.92"); die ("Notice failed ".Dumper($_)); }; # stuff something into the log $logger->trace("Point 9.9");

    is producing the following log output:

    20130518 16:19:53.264 TRACE 29309 PreparePayment.pm(175) _run: Point 9.85 
    20130518 16:19:53.268 TRACE 29309 PreparePayment.pm(184) __ANON__: point 9.9 
    20130518 16:19:53.269 FATAL 29309 PreparePayment.pm(185) __ANON__: Notice failed: $VAR1 = 'sale-item'; 
    20130518 16:19:53.272 ERROR 29309 PreparePayment.pm(78) __ANON__: run(OT::DB::Result::Csv=HASH(0xa47dd00)): $VAR1 = '';
    

    That last log line, from PreparePayment line 78, comes from the $logger call in this:

    try { $self->_run($row_id, $subject) } catch { $logger->error("run($row_id): ".Dumper($_)) };

    So, you see what's happening there? $self->publish_notice() is failing (in ways that are also impossible) and putting us into the catch block. We log that we're at point 9.9, and we log the "Notice failed" message. That same message should be thrown as an exception by logdie(), and we don't get to point 9.92 so it looks like the die probably happened. However, up around line 78 where we catch that exception, we get an empty string.

    It may be relevant that $self->publish_notice is from a Moose::Role, and that $logger calls in there (it fetches its own logger, and checks it's non-null) don't result in log output.

    Anybody have any thoughts? I've gone through our previous discussions of Try::Tiny and I don't see any of the problematic things happening here.

Extract JSON data
3 direct replies — Read more / Contribute
by omegaweaponZ
on May 18, 2013 at 16:39
    I'm looking for an effective way to extract data from a JSON feed. Feeds are separated line by line instead of an over-encompassing array, so it's throwing me off. Here is an example:
    {"1": {"subject1": "value", "subject2": [{"subject3": "value", "subjec +t4": "value"}], "subject5": "value", "subject6": value, "subject7": " +value"}, {"2": {"subject1": "value", "subject2": [{"subject3": "value", "subjec +t4": "value"}], "subject5": "value", "subject6": value, "subject7": " +value"}, {"3": {"subject1": "value", "subject2": [{"subject3": "value", "subjec +t4": "value"}], "subject5": "value", "subject6": value, "subject7": " +value"},
    etc.... So instead of something that says DATA [ .......json output....], it is just separated essentially by unique numbers line by line from {} which are not detecting the array. My older code had said something along the lines of:
    foreach my $stuff(@{$json->{DATA}}){ my %hash = (); $hash{subject1} = $stuff->{subject1}; }
    Dumping out the hash after pulling all the subjects headers I want NORMALLY would display the value that I can use, but this doesn't work in this format. What is the best way to be able to dump these into a value, the numerical first array header (1, 2, 3, etc), the subjects and their respective values? Thanks!
Continuations in Perl - Returning to an arbitrary level up the call stack
5 direct replies — Read more / Contribute
by unlinker
on May 18, 2013 at 16:22

    Greetings Venerable Monks,

    I have a need to return from a deeply nested function, not to the caller, but to a higher point up the call stack. After grovelling at the feet of Google, I understand that, what I need are "Continuations" - functions that not only specify what to return, but also specify where to return to, up the call stack.

    The only way I can think of doing this is by throwing exceptions (say at nesting level 4) and catching them at a specified position up the call chain (say at nesting level 1). But it somehow seems wrong (what if a function between 1 and 4 catches it? What if some other legitimate exceptions are thrown?).

    Can I implement continuations in Perl in a more disciplined/organized fashion? If yes, can the wise monks here educate me with an example please?

string manipulation
7 direct replies — Read more / Contribute
by kimlid2810
on May 18, 2013 at 15:03

    hi monks. so i have a simple i think question, but i cant find any easy or fast solution, so it is not so simple to me. :p Say, there is this scenario, where you have a variable which contains a string full of ones and zeroes like:

    $string = "010011100001110110100110111000001";

    What do you think would be the quickest, not only for the cpu but for writing too, way to create another string, with the number of continual zeroes in it. Let's say for the above $string, to create a new one with this format:

    $string = "010011100001110110100110111000001"; $newString = "1, 2, 4, 1, 1, 2, 1, 5";

    where 1 is for the first zero in $string, 2 for the 2 zeroes in third and fourth place, 4 for the 4 zeroes in 8th, 9th, 10th, and 11th place. etc... I m really sorry if you cant really make out what i mean, but english is not my native language. Any help would be appreciated.


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


  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    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 making s'mores by the fire in the courtyard of the Monastery: (8)
    As of 2013-05-19 22:36 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best material for plates (tableware) is:









      Results (397 votes), past polls