Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Use of uninitialized value $site in concatenation (.) when excute the perl script

by laksh (Novice)
on Aug 31, 2012 at 08:18 UTC ( #990940=perlquestion: print w/ replies, xml ) Need Help??
laksh has asked for the wisdom of the Perl Monks concerning the following question:

when I am running the perl script where I user foreach to read the file line by line I got the print message. "Use of uninitialized value $site in concatenation (.) or string at logfile.pl line 13, <LOGFILE> line 19."
#!/usr/bin/perl -w use strict; use warnings; my ($time, $site, $logName, $fullName, $date, $gmt, $req, $file, $prot +o, $status, $length); $site; my $LOGFILE = "config.txt"; open LOGFILE,"$LOGFILE" or die "cannot open file : $!"; foreach my $line (<LOGFILE>) { ($site, $logName, $fullName, $date, $gmt, $req, $file, $proto, $status, $length) = split(' ',$line); my $a = "$site\n"; print "--$a\n"; my $b = "$logName\n"; print "==$b\n"; } close(LOGFILE);
Please let me know where I am doing the mistake.

Comment on Use of uninitialized value $site in concatenation (.) when excute the perl script
Download Code
Re: Use of uninitialized value $site in concatenation (.) when excute the perl script
by Corion (Pope) on Aug 31, 2012 at 08:49 UTC

    Have you read the error message? It tells you to look at line 13. It tells you that the variable $site in line 13 is undefined.

    Maybe you should look at where $site gets set, and find out why it doesn't seem to have the value you expect.

Re: Use of uninitialized value $site in concatenation (.) when excute the perl script
by nemesdani (Friar) on Aug 31, 2012 at 08:53 UTC
    Line 13 is where you set the variable $a (choose another name by the way), and you concatenate the variable $site with a newline.
    -> There is something wrong with the $site variable, thus with your input(Logfile). Check it.

    I'm too lazy to be proud of being impatient.
Re: Use of uninitialized value $site in concatenation (.) when excute the perl script
by kcott (Abbot) on Aug 31, 2012 at 09:21 UTC

    You probably have blank lines in your input.

    In the split doco, under "As another special case, ...", you'll see how your pattern (' ') is handled specially.

    Whenever $line equals "\n", split(' ',$line) will return (): all of those 10 variables ($site, ..., $length) will be undef (i.e. "uninitialized value").

    If you want to ignore them, you can do something like this:

    foreach my $line (<LOGFILE>) { next if $line =~ /\A\Z/; ($site, ... ... }

    [Aside: $a and $b are not good choices for variable names as they have special meaning to Perl - see perlvar]

    -- Ken

      And, foreach my $line (<LOGFILE>) reads the whole LOGFILE straight into memory before starting to enumerate through the lines. (Unless Perl has special-case handling for that case. Which I don't recall Perl having.) The OP should use the iterator form: while (my $line = <LOGFILE>) {

Re: Use of uninitialized value $site in concatenation (.) when excute the perl script
by MidLifeXis (Prior) on Aug 31, 2012 at 12:51 UTC

    In addition to pointing you at line 13 in your code (as has already been mentioned), the error message also tells you exactly which line in config.txt it is having problems with. Read the log file, compare line 13 to a different line that is not reported, and see what the differences are.

    --MidLifeXis

Re: Use of uninitialized value $site in concatenation (.) when excute the perl script (an aside - code review)
by MidLifeXis (Prior) on Aug 31, 2012 at 13:07 UTC

    Take this or leave it, but there are some practices in your code that have a faint whiff of cargo culting or things that may cause problems later on. I understand that this code may just be an example.

    --MidLifeXis

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://990940]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2014-09-23 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (216 votes), past polls