Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
laziness, impatience, and hubris
 
PerlMonks  

Use of uninitialized value for file name

by Win (Novice)
on Apr 13, 2006 at 11:49 UTC ( [id://543124]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

Monk TIME,


The following section of code is giving me grief:
opendir(DIR_A, "$version_location_programs") or die $!; while(my $job =readdir(DIR_A)){ print "I am here\n"; print "$job";sleep 10; if ($job =~ /20_October_2004.{3,}/) { push (@files_20_October_2004, "$directory_location"."$job"); } }
It says Use of uninitialized value for $job.

Replies are listed 'Best First'.
Re: Use of uninitialized value for file name
by moklevat (Priest) on Apr 13, 2006 at 11:59 UTC
    When readdir runs out of entries in the directory it returns an undefined value in scalar context.

    Update:...but that won't explain why you get an initialization error.

    For example

    #!/usr/bin/perl use warnings; use strict; opendir(DIR_A, './testdir') or die $!; while (my $job = readdir(DIR_A)) { print "$job\n"; if ($job =~ /something/) { print "Something was seen\n"; } }
    prints

    . ..
    when testdir is empty, and prints

    . .. something.pl Something was seen
    when testdir contains the file something.pl.
      is
      while(my $job =readdir(DIR_A)){ print "I am here\n"; print "$job";sleep 10; if ($job =~ /20_October_2004.{3,}/) { push (@files_20_October_2004, $directory_location.$job); } }
      the same as
      while(<DIR_A>)){ my $job = $_; print "I am here\n"; print "$job";sleep 10; if ($job =~ /20_October_2004.{3,}/) { push (@files_20_October_2004, $directory_location.$job); } }
      ?
        Have you tried this yourself?

        #!/usr/bin/perl use warnings; use strict; opendir(DIR_A, './testdir') or die $!; while(<DIR_A>){ my $job = $_; print "$job\n"; }

        Gives this error:

        readline() on unopened filehandle DIR_A at ./readdir.pl line 8. (Are you trying to call readline() on dirhandle DIR_A?)
Re: Use of uninitialized value for file name
by duff (Parson) on Apr 13, 2006 at 12:00 UTC
    It says Use of uninitialized value for $job.

    Are you sure? What is the exact error message? How do you know it's not complaining about $directory_location?

    BTW, there's no need to quote your variables when concatenating them. A simple $directory_location.$job will suffice.

      Use of uninitialized value in string at C:/Perl_activate/Run_this.pl l +ine 83

        And I'm guessing that line 83 is the one where you push onto the array? Check all of the variables on that line.

        Or, if line 83 is one of the lines after the while loop, then what moklevat said comes into play.

        ok.. so what's on line 83? Is it maybe $directory_location that's undef instead? Add some debugging print/warn/Data::Dumper statements to see what's not what you expect.
Re: Use of uninitialized value for file name
by wfsp (Abbot) on Apr 13, 2006 at 12:54 UTC
    When I'm stumped I often try reducing the script to the bare bones to get something working and gradually add things back in.

    Something like:

    #!/usr/bin/perl use warnings; use strict; my $dir = 'c:/Perl'; opendir DIR_A, $dir or die $!; while(my $job = readdir(DIR_A)){ print "$job\n"; } __DATA__ ---------- Capture Output ---------- > "C:\Perl\bin\perl.exe" _new.pl . .. bin Desktop.ini eg html lib myperl site Tidy > Terminated with exit code 0.
    When you come up against the error you'll have a better chance of narrowing down the source of your grief.

    Hope that helps.

      Thanks, I followed your advice and then realised that I was doing multiple whiles and forgetting to close file handles. Basic error I'm afraid.

        Had you been using lexical handles as it must have certainly been suggested to you (at least I *think* I did and I'm *confident* others did too), and properly scoped blocks, which should be natural in any case, then you wouldn't have needed explicit closes and you would have avoided this annoying error.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://543124]
Approved by davido
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.