Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: Parsing Problems

by Anonymous Monk
on May 14, 2019 at 14:42 UTC ( #1233764=note: print w/replies, xml ) Need Help??


in reply to Re^2: Parsing Problems
in thread Parsing Problems

I would really, really suggest replacing the eval() with something like

my $name_space = "modes::$ARGV[2]"; my $code = $name_space->can( 'flex' ) or die "$name_space does not implement flex()"; $temp = $code->( $line );

This is on the hypothesis that in fat-fingering in your example you reversed the slope of a back slash.

When obscure code fails it is really hard to debug. I infer from your eval() example that what you are trying to do is this:

  1. Run a script whose second argument is the name of a processor for your input.
  2. Process each line of the file using a subroutine named flex() in a Perl module named "modes::$ARGV[2]"
  3. This module has already been loaded.

If these are correct, the above code should implement it in an easier-to-read-and-debug manner. The three statements compute the name of the module, get the address of the subroutine in that module (failing if it can not be found), and call the subroutine, passing it the line from the file, and storing the result in $temp.

This is off-topic for your question, but all your Perl should have use strict; and use warnings; near the top of the file. If your script does not do this, all sorts of bugs could lurk. Of course, if you just slam these into a legacy script it may find all sorts of things -- more than you are able to fix in one sitting. But it's worth a try -- one of the warnings/errors may tell you what your problem is.

Replies are listed 'Best First'.
Re^4: Parsing Problems
by ftherese (Novice) on May 15, 2019 at 17:47 UTC

    Thank you for your reply. This is helpful as I've traced the error to how Perl is handling the modules, @INC, and referencing subroutines.

    What my code is supposed to do is apply different sets of transformations to a text file based on options specified from the command-line:

     modes.pl [rawtext] [style] [mode](one, two, etc.) [1st variation](a, a_prime, b, etc.) [2nd variation]

    "style" is one of several directories containing nine .pm files. Each .pm file has at least the subroutines "first" and "flex." The other subroutines are variations.

    Here is the code that I'm using to load the modules:

    package modes; my $lP; my $modCount = 0; BEGIN{ $lP = "$ARGV[1]"; } use lib $lP; if(opendir(LIB, $lP)){ foreach my $l (readdir(LIB)){ unless ($l !~ /^(.*)\.pm$/){ eval qq~require ~ . $ARGV[1] . qq~::~ . $1 . qq~;~; print $@; } } }

    The error that code produces (given: ./modes.pl "filename" english three a b) in Perl 5.26.1 (error does not occur in 5.18.2) is the following:

    Can't locate english/eight.pm in @INC (you may need to install the eng +lish::eight module) (@INC contains: english /home/******/perl5/lib/pe +rl5/5.26.1/x86_64-linux-gnu-thread-multi /home/*********/perl5/lib/pe +rl5/5.26.1 /home/***/perl5/lib/perl5/x86_64-linux-gnu-thread-multi /h +ome/******/perl5/lib/perl5 /etc/perl /usr/local/lib/x86_64-linux-gnu/ +perl/5.26.1 /usr/local/share/perl/5.26.1 /usr/lib/x86_64-linux-gnu/pe +rl5/5.26 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.26 /usr/sh +are/perl/5.26 /home/****/perl5/lib/perl5/5.26.0 /home/*****/perl5/lib +/perl5/5.26.0/x86_64-linux-gnu-thread-multi /usr/local/lib/site_perl +/usr/lib/x86_64-linux-gnu/perl-base) at (eval 5) line 1.

    Setting $lP to the fully qualified PWD (or simply "." since the "english" directory is located here) fixes that error, but the subroutines are still undefined:

    Undefined subroutine &modes::three::flex

    I'm not sure why this is. I will change my code based on your recommendations, but I don't understand it well enough myself to do so yet. I borrowed the module loading code from another programmer years ago simply because it did what I needed - I never fully understood it.

      Based on what you've posed here, you definitely don't need to use the eval with require BAREWORD;, require "/path/to/filename.pm"; would be much easier in your case. Also, note that despite its several caveats, a glob might be easier than opendir in this case. Here's an example, where $stylespath is an absolute pathname (see do for the rules on when @INC will be searched and when it won't):

      use File::Glob 'bsd_glob'; my $style = $ARGV[1]; die "bad style" unless $style=~/\A\w+\z/; require $_ for bsd_glob("$stylespath/$style/*.pm");
      the subroutines are still undefined: Undefined subroutine &modes::three::flex I'm not sure why this is.

      What package statement (if any) does english/eight.pm start with? If it's e.g. package eight;, then a sub flex in that file declares a subroutine whose full name is &eight::flex.

      Note that the AM post does not name one caveat of UNIVERSAL's can method, which is that it also searches the package's ancestry via @ISA for that method - if you don't set @ISA in your packages, it's fine, but if you do, then you need to be aware of this. If you want to avoid that, then this is IMO one of those rare cases where symbolic refs can help: my $package = 'eight'; my $subref = do { no strict 'refs'; \&{$package.'::flex'} };.

      Update: That you've traced it down to this issue also helps explain what you described in your root node, that you see a difference between 5.18 and 5.26: Removal of the current directory (".") from @INC may explain your issue. But my suggestions above still stand!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2022-05-25 03:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (84 votes). Check out past polls.

    Notices?