Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Check For null Arguments

by rfleisch (Initiate)
on Mar 13, 2013 at 15:21 UTC ( #1023230=perlquestion: print w/ replies, xml ) Need Help??
rfleisch has asked for the wisdom of the Perl Monks concerning the following question:

Hello all, I can't seem to get the correct syntak to check for NO items on the command line.
#!/usr/local/ActivePerl-5.16/bin/perl use POSIX qw/strftime/; sub fixdir { my $dir = shift; opendir my $DH, $dir or die "Not Found....$dir: $!"; while (my $f = readdir $DH) { next if grep $_ eq $f, qw/. ../; (my $new = $f) =~ s/[^a-zA-Z0-9_.]/_/g; next if ($new eq $f); while ( -e "$dir/$new") { $new .= "1"; } print STDERR "Renaming: $f -> $new\n"; rename "$dir/$f", "$dir/$new"; fixdir("$dir/$new") if -d "$dir/$new"; } } $Do_Dir = @ARGV[0]; print("ARG=", "$Do_Dir", "<<<<<<\n"); if ("$Do_Dir" == null ) { print ("No Folder Name given!\n"); exit; } print( "The Folder Name is: ", "$Do_Dir", "\n" ); fixdir($Do_Dir); print "Done..." produces this.... 9858-215-rfleisch:test_it p00015282$ ./Change_FName2.pl these ARG=these<<<<<< No Folder Name given! 9858-215-rfleisch:test_it p00015282$ ./Change_FName2.pl ARG=<<<<<< No Folder Name given! 9858-215-rfleisch:test_it p00015282$ Notice the SAME results ????????
Thank.... -Ray

Comment on Check For null Arguments
Download Code
Re: Check For null Arguments
by rnewsham (Hermit) on Mar 13, 2013 at 15:32 UTC

    I don't have access to activeperl for a true test but for me on linux this tweak of an extract of your code seems to work.

    use strict; use warnings; my $dir = $ARGV[0]; print("ARG=", "$dir", "<<<<<<\n"); if (!$dir) { print ("No Folder Name given!\n"); exit; } print( "The Folder Name is: ", "$dir", "\n" ); #fixdir($dir); print "Done..."
Re: Check For null Arguments
by choroba (Abbot) on Mar 13, 2013 at 15:34 UTC
    This is Perl. See perlop for details: == is used to compare numbers. Also, null has no special meaning in Perl (are you using strict?). To represent an empty string, use '', "" or q(), for undefined value, use undef.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Check For null Arguments
by toolic (Chancellor) on Mar 13, 2013 at 15:34 UTC
      Thank You EVERYONE for taking the time to help me. Tweaking the "IF" statement to if (!$dir) did the trick :-) And YES, I'm reading/looking at "Basic debugging checklist" -Ray
Re: Check For null Arguments
by kcott (Abbot) on Mar 13, 2013 at 15:40 UTC

    G'day rfleisch,

    Put "use strict;" and "use warnings;" at the start of your code and let Perl tell you where you've gone wrong. See strict and warnings.

    While there may be others, one obvious problem that leaps out at me is "("$Do_Dir" == null )":

    • "null" is not a keyword in Perl - you're possibly thinking of undef.
    • "==" is an operator for testing numeric equality; use "eq" for string equality. See perlop: Equality Operators for more complete details.
    • The whole of that condition should probably be written as "(defined $Do_Dir)". See defined.

    -- Ken

Re: Check For null Arguments
by ramlight (Friar) on Mar 13, 2013 at 15:50 UTC
    If all you are interested in knowing is whether or not there are ANY arguments on the command line, then you might just as well look at the size of @ARGV:
    my $nargs = @ARGV; print "Number of command line arguments is $nargs\n";
Re: Check For null Arguments
by AnomalousMonk (Abbot) on Mar 13, 2013 at 16:25 UTC

    Further to ramlight's post: One could also write something like this very early in the code:
        die "no command line arguments given" unless @ARGV;

      Thanks for these TWO tips. I have included them in my little perl script to have as a reference. Best regards to All -Ray
Re: Check For null Arguments
by nvivek (Vicar) on Mar 14, 2013 at 04:52 UTC

    A better way to check for particular value is defined or not, use defined function in perl.

    Syntax: defined EXPR

    defined function will return true if EXPR has a value other than the undefined value "undef".
    Defined function will return true even if the value of particular scalar is null string. To check whether value of scalar is defined and not null, use following code.

    my $defined_string="Defined_value"; print "Value of defined_string is defined and its value:$defined_strin +g" if(defined $defined_string && $defined_string ne '');

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2014-11-28 06:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (193 votes), past polls