Operating System - HP-UX
1820594 Members
1649 Online
109626 Solutions
New Discussion юеВ

Problem in running perl script

 
SOLVED
Go to solution
chinnaga
Advisor

Problem in running perl script

Hi all,

I have a problem in running the perl script. Its throwing these error.

"Use of uninitialized value in scalar chomp at"

and

Name "main::OUT" used only once: possible typo

can u pls help me resolve these errors.
12 REPLIES 12
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Problem in running perl script

> Name "main::OUT" used only once: possible typo

probably you meant STDOUT where you have something like

print OUT "Warning\n";

> "Use of uninitialized value in scalar chomp at"

in your code you have either

chomp;

or

chomp $foo;

the default arg to chomp is $_. The variable that chomp is seeing has no value, or is undef, which causes the warning.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
chinnaga
Advisor

Re: Problem in running perl script

Thanks a lot Merijn,

This is the part of the script. I have assigned the $value to chomp, and I am printing the value also , can u please tell me wht is wrong in this .

open(F,"$currentenvroot/load.config") or die "cannot open
$currentenvroot/load.config config file";
while($line = ) {
($name,$value) = split("=",$line);
chomp ($value);
print "NAME $name VAL $value \n" ;

$nv{$name} = $value;
}
Dennis Handly
Acclaimed Contributor

Re: Problem in running perl script

>I have assigned the $value to chomp

Only if you go through the while loop.
chinnaga
Advisor

Re: Problem in running perl script

It is inside the while loop.
H.Merijn Brand (procura
Honored Contributor

Re: Problem in running perl script

use strict;
my %nv;
my $fn = "$currentenvroot/load.config";
open my $fh, "<", $fn or die "cannot open config file '$fn': $!";
while (<$fh>) {
chomp;
my ($name, $value) = split m/=/;
print "NAME $name VAL $value \n" ;
$nv{$name} = $value;
}

Several issues addressed.

1. while ($line = ) {

this will stop is the line consists of a single 0. If you want to assign to a variable, use something like

while (defined (my $line = )) {

2. On modern perls, it is better to use lexical file handles. They have a better (more restricted) scope, and are way more easy to pass to functions and methods that take handles. Compare

$csv->getline ($fh);

to

$csv->getline (*FH);

3. split ("=", $line)

Does NOT do what you expect. Besides some exceptions, the first argument to split is a regex, not a string.

4. chomp on a variable

When needing to chomp lines, do that as soon as possible for readability. First reject the lines you don't want, then chomp, then parse.

5. use strict;

Use strict; Always. And thus also use lexical variables. When you now make a typo or use a variable out of it' scope, it'll warn.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
chinnaga
Advisor

Re: Problem in running perl script

Thanks,

I have modified the changes u have told me ...but still I am getting error. I have declared my %nv;

Global symbol "$nv" requires explicit package name at /opt_apps/comcat_itg/r_curr/be/loads/patsy_discount/driver/ftp_patsy.pl.

I am sending u the script.


#!usr/bin/perl
use warnings;
use strict;

my $var;
my $inpath;
my $value;
my $line;
my @line;
my $name;
#############################################################
# FTP Script to transfer data files from 649 to hpcc547
#Ramkumar Balasubramanian
#############################################################
my $currentenvroot=`/opt_apps/comcat_root/bin/getEnvRootPath.sh SH`;
$currentenvroot =~ s/[\n\r]//g;
my $currentftpenvroot=`/opt_apps/comcat_root/bin/getEnvRootPath.sh FTP`;
$currentftpenvroot =~ s/[\n\r]//g;
my $cmd;
my $cmdprd;
my $lcddir;
my $cdir;
my %nv;
my $fn = "$currentenvroot/r_curr/be/loads/load.config";

#Now Start FTP to Send the PATSY Data to htx6048 #

$lcddir="lcd $currentftpenvroot/data/output/patsy_discount\n";


open my $fh, "<", $fn or die "cannot open config file '$fn': $!";
while (<$fh>) {
chomp;
my ($name, $value) = split m/=/;
print "NAME $name VAL $value \n" ;
$nv{$name} = $value;
}


ANd this is the error I am getting

Global symbol "$nv" requires explicit package name at /opt_apps/comcat_itg/r_curr/be/loads/patsy_discount/driver/ftp_patsy.pl line 57.
Execution of /opt_apps/comcat_itg/r_curr/be/loads/patsy_discount/driver/ftp_patsy.pl aborted due to compilation errors.
Ralph Grothe
Honored Contributor

Re: Problem in running perl script

There is nothing to add to Merijn's recommendations.

Maybe, you could change the split criteria to include possible whitespace around the equals sign, as to have stripped it off from you key value pairs?

e.g.

my ($key, $val) = split /\s*=\s*/, $line;

$nv{$key} = $val if defined $key;

As this looks like parsing one of those notorious Win ini files,
there plenty of modules on CPAN for those
(probably too bewildering not to have it parsed by ones own parsing)
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: Problem in running perl script

Is this the whole script?
Looks these are fewer than 57 lines.
There must be another occurrence of $nv.
Madness, thy name is system administration
H.Merijn Brand (procura
Honored Contributor

Re: Problem in running perl script

probably another typo somewhere. There error is about the SCALAR $nv, whereas in the code you've sown us so far, you were only using the HASH %nv. Addressing elements in the hash is

$nv{$key}

Look for occurances of $nv that are not followed by an opening brace.

And I bet the warning/error comes with a line numer. Should be clear enough.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Hein van den Heuvel
Honored Contributor

Re: Problem in running perl script

Are you still seeing the original error?

Clearly that means that the 'split' line did not produce a $values, which probably means there was no '=' on the line.

>> my ($name, $value) = split m/=/;

A simply debugging for that suspected problem would be a print line like

print STDERR "Bad input line # $. : $_\n" unless defined($value);

And, as indicated the split uses a regular expression which you may want to exploit, for example to suck up whitespace around the '='
my ($name, $value) = split m/\s*=\s*/;

Depending on the exact parsing requirements You may also want to consider NOT to use a split but a match. For example, you may want to skip lines which start with a # or find new sections in the input, or skip leading whitespace

while (<$fh>) {
if (/\s*(\S+)\s*=\s*(.*)$/) {
$name = $l;
$values = $2;
:
} else {
if (other acceptable input) {
...
} else {
print STDERR "Bad input line # $. : $_\n";
}
}
}


fwiw,
Hein.
chinnaga
Advisor

Re: Problem in running perl script

Thanks a lot to everybody , its working fine now.
H.Merijn Brand (procura
Honored Contributor

Re: Problem in running perl script

Thanks can be expressed in assigning points to the given answers. This will help future readers in finding the answers.

An anser the "solved" your problem is usually rated 9..10, and answer that helped greatle 5..8, an informative answer that didn't bring you closer but helped you understand things better 2..4 and all else 0..1

http://forums11.itrc.hp.com/service/forums/pageList.do?userId=CA1462614&listType=unassigned&forumId=1

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn