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

Handling uninitialized values

by THRAK (Monk)
on May 23, 2001 at 00:36 UTC ( [id://82382]=perlquestion: print w/replies, xml ) Need Help??

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

I have a script that loops through a directory and it's sub-dir's. To get the files in the directory root I either make the sub-dir undefined or a null string. My question is a style and usage issue as "-w" complains if it's undefined. Is there a better way to do this sort of thing? Here's a quick test script of what I'm talking about:
#!/usr/local/bin/perl -w use strict; my $dir = '/tmp/'; my $sub_dir; my $file = 'foo.txt'; my $path = "${dir}${sub_dir}${file}"; print "$path\n";
This causes the "Use of uninitialized value in concatenation" error. But if you set $sub_dir = ""; it halts the warnings. Just looked kind of odd/ugly, thus why you are reading this now.

-THRAK
www.polarlava.com

Replies are listed 'Best First'.
Re: Handling uninitialized values
by Sifmole (Chaplain) on May 23, 2001 at 00:56 UTC
    Or if $sub_dir is always going to be empty, then why are you using it?

    I am assuming that you have some kind of loop going on where $sub_dir may or may-not be assigned. In that case I would try something like:

    my $path = join('', $dir, ($foo || ''), $file);
    This will fail if $foo == 0 so if that is a possibility then you could use..
    my $path = join('', $dir, (defined $foo ? $foo : ''), $file);

      Very good point. If a var is going to be empty... toss it.

      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      Peter L. Berghold --- Peter@Berghold.Net
      "Those who fail to learn from history are condemned to repeat it."
      
      If you read the question again, you will see the first thing I mentioned was that this was in a loop and the value is undefined when accessing files in the root location. This was only a small snippet to illustrate the point of the question.

      -THRAK
      www.polarlava.com
Re: Handling uninitialized values
by blue_cowdawg (Monsignor) on May 23, 2001 at 00:47 UTC

    Well... right off the bat without giving this much thought the first thing I can think of is

    my $dir="/tmp"; my $sub_dir="/";

    instead of putting the trailing slash in $dir put it in $sub_dir.

    FWIW anyway...

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    Peter L. Berghold --- Peter@Berghold.Net
    "Those who fail to learn from history are condemned to repeat it."
    
Re: Handling uninitialized values
by rucker (Scribe) on May 23, 2001 at 00:57 UTC
    Well this isn't going to make it any prettier (in fact, it is uglier), but if you have potentially undefined variables and you want to suppress the warnings, you should check to see if they're defined and then use them or not. Here's one example... obviously there are other ways to do it.
    my $path = defined($dir) ? $dir : '' . defined($sub_dir) ? $sub_dir : +'' . defined($file) ? $file : '';
      I've seen this sort of thing and would agree that is is uglier. I guess in a more complex application this might be good approach.

      -THRAK
      www.polarlava.com
Re: Handling uninitialized values
by bwana147 (Pilgrim) on May 23, 2001 at 16:41 UTC

    Yet Another Way Of Doing It

    I would rather have the slashes not be part of the dirs or subdirs. Instead, consider that the full path merely consists of $dir, $sub_dir and $file, join'ed together with slashes. Then, grep should remove empty/undefined entries:

    my $dir = '/tmp' my $sub_dir; my $file = 'foo.txt' my $path = join('/', grep { defined && $_ ne '' } ($dir, $sub_dir, $fi +le)); print "$path\n";

    --bwana147

Re: Handling uninitialized values
by bwana147 (Pilgrim) on May 23, 2001 at 12:51 UTC

    You may disable warnings locally:

    { no warnings qw( uninitialized ); my $path = "${dir}${sub_dir}${file}"; } print "$path\n";

    Just my € 0.02
    --bwana147

      I like this approach, but I guess a basic $sub_dir = '' is the quickest/cleanest solution here. Again, this is another example that would be very useful for a more complex piece of code.

      -THRAK
      www.polarlava.com

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-20 03:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found