tgt4Perl has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks and gurus! I have a problem in Perl here that I've been trying to solve using different techniques that I came across but it does not seem to work. :( I have a '.rc' file, 'sample1.rc' that contains just this code:

# Use -*- perl -*- mode in Emacs. #--------------------------------------------------------------------- +----------- $RC{configs} = ['dnl', 'abme', 'bet', 'khae', ], $RC{model_root} = '/nfs/fm/disks/fm_aaaa_00011/users/me/repotest/model +/tools/someproject/logs' 1;

the '$RC{model_root}' line contains the path to a file that I'm trying to read from my perl script, called 'sample.pl'. This is the complete code in 'sample.pl'..the problem is I'm unable to fetch the path from 'sample1.rc'

#!/usr/5.14.1/bin/perl -w use strict; use warnings; use FileHandle; use File::Find; use Getopt::Long; use Data::Dumper; use File::Path; use Safe; use vars qw($ProgDir $ProgName $debug $quiet %RC %OPTS %OPTS_DEF); main (); sub main { parse_cmdline (); read_file($OPTS{rcfile}) if exists $OPTS{rcfile}; dynamo_check (); } my $configs = $RC{configs}; my $model_root = $RC{model_root}; my $outputfile = 'output.txt'; my %results; myPrint("Executing the script: $ProgName"); myPrint("$ProgName executed successfully"); #--------------------------------------------------------------------- +---------- sub dynamo_check { #look for 'Errors: 0' string here my $log_file = $model_root; while ( <$log_file> ) { my ( $varname, $value ) = ( m/\A(\S+) = \'(\S+)\'/ ); if ( $varname eq '$RC{model_root}' ) { $model_root = $value +; } } #print $model_root; foreach my $log_file ( glob "/*" ) { info ("Opening file: $log_file"); my $fh = open_for_reading ($log_file); if ($log_file eq "dynamo."){ while(my $line = <$fh>) { last if ($line =~ m/s*=\s+Exiting Dynamo\W/); chomp $line; open (my $ofh, '>' , $outputfile) or printError ("Could not op +en this file $!"); if ($line =~ m/Errors:\s+0/) { print $ofh "Dynamo run status: PASS\n"; $results{'Dynamo'} = "PASS"; }else { print $ofh "Dynamo run status: FAILED\n"; $results{'Dynamo'} = "FAILED"; } close $fh; } } #add this file to the 'reports' directory } } info ("done..check 1");

Please help! Looks like something you can follow easily, but pls let me know if you need more details. Thanks in advance!

Replies are listed 'Best First'.
Re: Fetching a file path from a Perl 'rc' file
by GrandFather (Saint) on Feb 12, 2015 at 20:06 UTC

    We need less detail. Looks to me that whatever your coding problem is, you could have boiled the issue down to about 6 lines of code. Why don't you go do that and show us the result? We'll just get on with other more productive stuff while you do that and save ourselves 20 minutes digging through your code. See I know what I mean. Why don't you? for some tips on cleaning up your question.

    Perl is the programming world's equivalent of English
Re: Fetching a file path from a Perl 'rc' file
by RonW (Parson) on Feb 12, 2015 at 21:14 UTC

    Disclamer: Not tested.

    sub getfilefromrc { open my $rch, '<', $_[0] or croak("Can't open $_[0]: $!"); my $fn; while (<$rch>) { if (/^\s*\$RC\{model_root\}/) { if (/'([^']+)'\s*$/) { $fn = $1; last; } } } defined($fn) or croak("Can't find filename in RC file ($_[0])"); return $fn; }
Re: Fetching a file path from a Perl 'rc' file
by Anonymous Monk on Feb 12, 2015 at 21:02 UTC
    What exactly is your question? You program doesn't work at all because of gazillions of things; it's completely non-functional. My recommendation is just to start from scratch. Test it as you go, and ask questions on Perlmonks. Oh, and don't use vars, it doesn't really help (as you can see).
Re: Fetching a file path from a Perl 'rc' file
by tgt4Perl (Initiate) on Feb 12, 2015 at 21:59 UTC

    Cleaned up the code to include just the required part as suggested. So the question is: how would I fetch the contents of my 'rc' or configuration file which contains the path of the files I need to read from my Perl script? Thanks!

      Now you have cleaned it up too much.

      • What does the subroutine parse_cmdline () do? Does it work as expected and what variables are set by it? What is in those variables?
      • What does read_file($OPTS{rcfile}) do? what's in $OPTS{rcfile}? Where does this data come from?
      • The sub dynamo_check relies on a number of global variables. Where are these variables set? What is their content? Is their content as expected? You are reading from $log_file as if that is a file-handle. Is it indeed a valid filehandle (and not simply a file-path)? Did you check the open function that set the filehandle for errors?
      • I have the same questions about the open_for_reading subroutine.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics
Re: Fetching a file path from a Perl 'rc' file
by Anonymous Monk on Feb 13, 2015 at 05:24 UTC

    Hi,

    What happens if you try to change directory to
    '/nfs/fm/disks/fm_aaaa_00011/users/me/repotest/model/tools/someproject/logs'
    from your command line?

    What if you try to get a list of its contents?

    J.C.