Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: Using the perl debugger to look at a renaming files function

by Aldebaran (Curate)
on Feb 05, 2021 at 08:50 UTC ( [id://11127916]=note: print w/replies, xml ) Need Help??


in reply to Re: Using the perl debugger to look at a renaming files function
in thread Using the perl debugger to look at a renaming files function

The Perl debugger is a wonderful tool, and I use it quite often - whenever I find it is the appropriate tool for the task. Before I fire up the debugger, I always stare at the messages, the code and at the docs, and in your example this turns out to be sufficient to see what's happening.

I simply thought that this problem might be a good way for me to get my feet wet with the debugger. I thought it might bring me close to perl's machine model.

That's also quite obvious: You're calling the sub without any parameters, but it expects one which can be used as a HASH reference in line 35 of debug1.pm:

Yes indeed. I needed to change that line with caller:

my $rvars = \%vars; ## this function is to be debugged using the perl debugger my $return2 = make_initial_captions($rvars); say "return2 is $return2";
Keep your code concise: Eliminate stuff which isn't relevant to your question. For example, I'm not going to install Net::SFTP::Foreign to get it even compiled.

Let's consider this. I know that we're encouraged to come up with an SSCCE, but this is not, nor can it easily be made so. It's an html templating system that has evolved according to my needs. I've tried to make a distribution of it, but I couldn't manage how to deal with filesystems on github. What would you use instead of Net::SFTP::Foreign?

Keep your own example complete. utils1.pm is missing.

This was my effort to be concise. I don't see how the use statements in subroutines affects other modules:

Update: put module source between readmore tags

package utils1; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw( invert_aoa print_hash print_aoa highest_number ); sub invert_aoa{ use strict; use warnings; use 5.010; my $a = shift; my @AoA = @$a; my $k = $#AoA; #say "k is $k"; my @BoB; for my $i ( 0 .. $#AoA ) { my $aref = $AoA[$i]; my $x = $#{$aref}; #say "x is $x"; for my $j ( 0 .. $#{$aref} ) { $BoB[$j][$i]= $AoA[$i][$j]; } } my $b = \@BoB; return $b; } sub print_aoa{ use warnings; use 5.011; my $a = shift; my @AoA = @$a; for my $i ( 0 .. $#AoA ) { my $aref = $AoA[$i]; for my $j ( 0 .. $#{$aref} ) { print "elt $i $j is $AoA[$i][$j]\n"; } } return $a; } sub highest_number{ use strict; use File::Basename; use Cwd; my ($aref, $filetype, $word) = @_; my $number; my @matching; my $ext = ".".$filetype; push (@matching, 0); #min returned value for my $file (@{$aref}) { #print "file is $file\n"; if ($file =~ /^$word(\d+)$ext$/){ #print "matching is $file\n"; push (@matching, $1); } } @matching = sort @matching; my $winner = pop @matching; return $winner } sub print_hash{ use 5.011; my $hash_ref = shift; ## 10-15-18 adding sorted keys print "subroutine says this is your hash: \n"; my %hash = %$hash_ref; foreach my $name (sort keys %hash) { say "key: $name, value: $hash{$name}\n"; } } 1;

Question: what use statements can be removed from everything I've posted?

This routine has a breakpoint now:

sub make_initial_captions { use 5.016; use warnings; use POSIX; use Path::Tiny; use Encode; use open OUT => ':encoding(UTF-8)', ':std'; use Data::Dumper; my $rvars = shift; my %vars = %$rvars; #print Dumper $rvars; my $image_path = $vars{"to_images"}; my $caption_path = $vars{"eng_captions"}; # put a break point here $DB::single = 1; return "nothing yet"; }

, and, I got to see some values at this mark:

$ perl -d 1.debug.11.pl Loading DB routines from perl5db.pl version 1.55 Editor support available. Enter h or 'h h' for help, or 'man perldebug' for more help. Subroutine debug1::getcwd redefined at /usr/share/perl/5.30/Exporter.p +m line 66. at template_stuff/debug1.pm line 400. debug1::BEGIN() called at template_stuff/debug1.pm line 400 eval {...} called at template_stuff/debug1.pm line 400 require debug1.pm called at 1.debug.11.pl line 4 main::BEGIN() called at template_stuff/debug1.pm line 400 eval {...} called at template_stuff/debug1.pm line 400 main::(1.debug.11.pl:12): my $ts = "template_stuff"; DB<1> n + main::(1.debug.11.pl:13): my $images = "aimages"; DB<1> c + title is 1.debug.1 path1 is /home/hogan/6.scripts/1.debug.1 abs is /home/hogan/6.scripts/1.debug.1/1.debug.11.pl debug1::make_initial_captions(template_stuff/debug1.pm:43): 43: return "nothing yet"; ... DB<3> p $vars{to_images} + /home/hogan/6.scripts/1.debug.1/template_stuff/aimages DB<4> p $vars{eng_captions} + /home/hogan/6.scripts/1.debug.1/template_stuff/captions DB<5> c + return2 is nothing yet ini path is /home/hogan/Documents/html_template_data/6.values.ini ...[normal execution] return is 1.debug.13.html http://www.merrillpjensen.com/perlmonks/1.debug.13.html Debugged program terminated. Use q to quit or R to restart, use o inhibit_exit to avoid stopping after program termination, h q, h R or h o to get additional info. DB<5> q + $

It then executed to the end and behaved.

Replies are listed 'Best First'.
Re^3: Using the perl debugger to look at a renaming files function
by hippo (Bishop) on Feb 05, 2021 at 09:45 UTC
    Let's consider this. I know that we're encouraged to come up with an SSCCE, but this is not, nor can it easily be made so. It's an html templating system that has evolved according to my needs.

    Your questions were not about HTML templating systems, they were about manipulating lists of filenames. It would have been easy enough for you to construct an SSCCE which demonstrated a single, isolated problem. If your SSCCE did not exhibit the same problem as your larger code then the problem is somewhere else in your larger code and you need to determine that first.

    Question: what use statements can be removed from everything I've posted?

    Let's just look at this one package. You have use 5.010; and  use 5.011; in package utils1; - the former is quite obviously superfluous and should be removed (it does nothing and if read in isolation is potentially misleading).

    In some of the subroutines you use strict, in others warnings and in still others both (or neither). This inconsistency is perplexing to say the least. Far better to use both throughout: either within your package or in the scope enclosing it.

    In subroutine invert_aoa you use 5.010; but there is apparently nothing in this sub which requires that version. What is the statement doing here? Remove this.

    In subroutine highest_number you use both File::Basename and Cwd but then do absolutely nothing with them. Remove these.

    In subroutines print_aoa and print_hash you use 5.011; but there is apparently nothing in these subs which requires that version. What is the statement doing here? Remove this.

    Other things which aren't helping you: inconsistent indenting, use of the special variables $a and $b as lexicals, using a package name with all lowercase when it isn't a pragma and exporting everything.

    If you had created an SSCCE then it is probable that some or perhaps even all of these issues would not appear in it. That would make the work of everyone reading the SSCCE much easier and therefore make them (ie. me) much more likely to offer help. There are therefore 2 benefits. Firstly the construction of the SSCCE helps you to hone down the problem better and in doing so may even solve it for you. Secondly, it allows others to see the actual problem you are having without wading through hundreds of lines of unnecessary code.


    🦛

Re^3: Using the perl debugger to look at a renaming files function
by haj (Vicar) on Feb 06, 2021 at 13:00 UTC
    I simply thought that this problem might be a good way for me to get my feet wet with the debugger.

    Fair enough. But then - I couldn't find a question related to how to use the debugger in your post.

    I thought it might bring me close to perl's machine model.

    Oh, I don't think the debugger will help here. The Perl debugger may help to understand Perl code you're currently maintaining, or maybe understand the interaction with a CPAN module in cases where its documentation isn't clear enough - but it doesn't go into Perl's guts.

    I know that we're encouraged to come up with an SSCCE, but this is not, nor can it easily be made so.

    Then, why show the code? What do you expect us to do with it?

    Question: what use statements can be removed from everything I've posted?

    That question has already been answered by other monks, so let me add just a general remark: It is of little use to have use statements within a subroutine. Typically you see them near the top of the file so that it is easy to spot the dependencies of your code.

    This routine has a breakpoint now:

    Good - that's one of the methods to get a breakpoint into the source. Since you've already read the docs about the Perl debugger, you should be aware that you could also have done it like that:

    $ perl -d 1.debug.11.pl DB<1>f debug1.pm DB<2>b 42

    You could, of course, also b debug1::make_initial_captions and then step to line 42.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 23:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found