Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Use of uninitialized value in numeric lt (<)

by calypso (Initiate)
on Apr 04, 2005 at 13:33 UTC ( [id://444667]=perlquestion: print w/replies, xml ) Need Help??

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

I keep getting this error when trying to run the script below. Use of uninitialized value in numeric lt (<). Is there something wrong with this statement "if($numArgs < 3)"?
#!/usr/bin/perl -w use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard/; use File::Glob ':glob'; use File::Basename; use ImageMagick; my $q = new CGI; print $q->header(); if($numArgs < 3) { print "Usage\n"; print "convertimages.pl widthxheight outputdir \"filepattern\" [qu +ality percent]\n"; exit(1); } # Get the width and height my $dimensions = $ARGV[0]; # get the output dir my $outputdir = $ARGV[1]; # get the file mask my $filepattern = $ARGV[2]; # Get the quality my $quality = $ARGV[3] || 0; print "Requested Size: $dimensions\n"; print "Output Directory: $outputdir\n"; print "Filepattern: $filepattern\n"; my @filelist = bsd_glob($filepattern); my $numFiles = @filelist; print "Processing $numFiles files\n"; my $count = 0; my $command = ""; foreach my $filename ( @filelist ) { #print "Processing $filename\n"; my $fileBaseName = basename($filename); my $outputFile = "$outputdir/$fileBaseName"; $command = "convert -size $dimensions " . (($quality) ? "-quality +$quality " : "") . "$filename -resize $dimensions $outputFile"; `$command`; $count++; print "$count "; } print "\n";

Replies are listed 'Best First'.
Re: Use of uninitialized value in numeric lt (<)
by maa (Pilgrim) on Apr 04, 2005 at 13:39 UTC

    Hi, calypso

    where is $numArgs declared/initialised? What are you expecting to be in it? ... if it isn't declared or assigned it will be undef.

    if you use strict; it should point that out...

Re: Use of uninitialized value in numeric lt (<)
by ikegami (Patriarch) on Apr 04, 2005 at 14:55 UTC

    I'd like to point out that CGI scripts do not receive their arguments via @ARVG, so even if you fix if ($numArgs), your script still won't work. Look at CGI's param method.

    Your script itself is extremely unsafe. The user could specify an arbitrary input or output directory name. But that's nothing compared to the user's ability to inject shell commands in $command.

    Finally, why shell out to use ImageMagick when it provides a Perl interface? You're forking needlessly, and you're not even checking for success.

    Your program needs much work.

Re: Use of uninitialized value in numeric lt (<)
by g0n (Priest) on Apr 04, 2005 at 13:41 UTC
    Am I missing something? I don't see where $numArgs is defined. Try use strict;

    g0n, backpropagated monk
Re: Use of uninitialized value in numeric lt (<)
by deibyz (Hermit) on Apr 04, 2005 at 13:45 UTC
    In adition to the other replies, only say that if you want the number of arguments to the script you can use @ARGV in scalar context, ie:

    if(@ARGV < 3) { # Blah }

    And I agree with the rest of the monks, ALLWAYS use strict. (And warnings).

Re: Use of uninitialized value in numeric lt (<)
by ysth (Canon) on Apr 04, 2005 at 13:58 UTC
    Yes. $numArgs isn't a variable that Perl sets for you. Check the @ARGV array instead: if (@ARGV < 3). See perlvar for a list of all the variables that perl might sometimes set for you.
Re: Use of uninitialized value in numeric lt (<)
by Joost (Canon) on Apr 04, 2005 at 13:48 UTC
Re: Use of uninitialized value in numeric lt (<)
by calypso (Initiate) on Apr 04, 2005 at 14:16 UTC
    Thank you so much. I corrected it. Works fine.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-24 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found