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

Re: [RFC] Module code and POD for CPAN

by jwkrahn (Abbot)
on Apr 14, 2021 at 03:42 UTC ( [id://11131244]=note: print w/replies, xml ) Need Help??


in reply to [RFC] Module code and POD for CPAN

for (my $i = 0; $i < scalar @{$self->{'trolley'}}; $i++) { if (${$self->{'trolley'}}[$i]->{'id'} eq $id) { $self->{'intent'} = undef; splice $self->{'trolley'}, $i, 1; return scalar @{$self->{'trolley'}}; } }

That will not work correctly if you have more than one deletion.

Also, splice on an array reference will not work with all versions of perl.

This should be better:

for ( my $i = $#{ $self->{ trolley } }; $i >= 0; --$i ) { if ( ${ $self->{ trolley } }[ $i ]->{ id } eq $id ) { $self->{ intent } = undef; splice @{ $self->{ trolley } }, $i, 1; return scalar @{ $self->{ trolley } }; } }

Replies are listed 'Best First'.
Re^2: [RFC] Module code and POD for CPAN
by Bod (Parson) on Apr 14, 2021 at 11:20 UTC

    Thank you for pointing that out. It shows a gap in my testing...I hadn't tried deleting a second product!

    I had planned to convert this loop into a more Perl-ish version rather than the current C-ish version:

    for my $i(0...@{$self->{'trolley'}})

      This isn't python, the last element in the range is not excluded!
      for my $i (0 .. $#{ $self->{trolley} }) {

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        This isn't python, the last element in the range is not excluded!

        I wouldn't know...my knowledge of python is non-existent. I wouldn't recognise the code unless the file had a .py extension!

        I actually originally wrote:

        for my $i (0 .. $#{ $self->{trolley} }) { # ... }
        but thought it would be better to omit the code block completely in providing the example.

      The problem is not the type of for loop, the problem is starting at the beginning of the array when changing the number of elements in the array.

      If you are going to remove elements of an array in a for loop you have to start at the end of the array.

      So this might work UNTESTED:

      for my $i ( reverse 0 .. $#{ $self->{ trolley } } )

        Sorry, I wasn't thinking that changing the loop would cure the issue. Just noting that I intended to review that bit of code anyway.

        However...there doesn't seem to be a problem! I set up this test code to observe the problem highlighted and to be sure I have cured it.

        #!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser); use cPanelUserConfig; use Bod::Stripe; use Data::Dumper; use strict; print "Content-type: text/plain\n\n"; my $stripe = Bod::Stripe->new( 'api-secret' => 'sk_test_5xxxxxxxnChHzDo', 'api-public' => 'pk_test_5xxxxxxxxmbegPN', ); if ($stripe->success) { my $res = $stripe->add_product( 'id' => '4E', 'name' => 'Dog food', 'description' => 'Boomer\'s favourite variety', 'qty' => 4, 'price' => 69, ); $stripe->add_product( 'id' => 2, 'name' => 'Dog treats', 'qty' => 2, 'price' => 150, ); $stripe->add_product( 'id' => 8, 'name' => 'Bod treats', 'qty' => 10, 'price' => 650, ); list('A'); $stripe->delete_product('2'); list('B'); $stripe->delete_product('4E'); list('C'); } else { print "FAILED: " . $stripe->error; } sub list { print "-- $_[0] --\n"; foreach ($stripe->list_products) { print "-> $_\n"; } print "\n"; }
        and it behaves exactly as I would expect giving this output:
        -- A -- -> 4E -> 2 -> 8 -- B -- -> 4E -> 8 -- C -- -> 8
        So deleting more than one product doesn't appear to be a problem.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11131244]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-04-23 11:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found