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

uninitialized value in concatenation (.)

by philosophia (Sexton)
on Jun 06, 2006 at 20:03 UTC ( [id://553903]=perlquestion: print w/replies, xml ) Need Help??

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

I'm having trouble with this subroutine. It worked when the script was strictly procedural, but now that I'm trying to divide it into subroutines I get the error
[Tue Jun 06 14:53:09 2006] [error] [client x.x.x.x] [Tue Jun 6 14:53: +09 2006] bulkmailers-request.pl: Use of uninitialized value in concat +enation (.) or string at /usr/lib/cgi-bin/bulkmailers-request.pl line + 56., referer: http://tangerine/cgi-bin/bulkmailers-request.pl [Tue Jun 06 14:53:09 2006] [error] [client x.x.x.x] [Tue Jun 6 14:53: +09 2006] bulkmailers-request.pl: readline() on unopened filehandle at + /usr/lib/cgi-bin/bulkmailers-request.pl line 57., referer: http://ta +ngerine/cgi-bin/bulkmailers-request.pl
-the error msgs reference these lines
$filename=~m/^.*(\\|\/)(.*)/; # strip the remote path and keep the fil +ename my ($newfilename) = $1; open(LOCAL, ">/var/www/html/upload/".$newfilename);
sub process_form { my ($from_address) = $query->param('from'); my ($reply_to_address) = $query->param('reply_to'); my ($subject) = $query->param('subject'); my ($message) = $query->param('message'); my ($description) = $query->param('description'); my ($msg_month) = $query->param('msg_month'); my ($msg_day) = $query->param('msg_day'); my ($msg_year) = $query->param('msg_year'); my ($from_address2) = 'rjm@uchicago.edu'; my ($to_address) = 'rjm@uchicago.edu'; my ($mail_host) = 'smtp.uchicago.edu'; if ( validate_form ( $from_address, $reply_to_address, $subject, $mes +sage, $description, $msg_month, $msg_day, $msg_year ) ) { my ($filename) = $query->param('email_list'); $filename=~m/^.*(\\|\/)(.*)/; # strip the remote path and keep the fil +ename my ($newfilename) = $1; open(LOCAL, ">/var/www/html/upload/".$newfilename); while(<$filename>) { print LOCAL $_; } my ($mm) = new File::MMagic; # use internal magic file my ($res) = $mm->checktype_contents("/var/www/html/upload/".$filename) +; # Adjust subject and body message my ($message_body) = "Request submission\rFrom: ".$from_address."\rRep +ly_to: ".$reply_to_address."\rSubject: ".$subject."\rMessage:\r\r".$m +essage."\r\rRecipients: ".$description."\r\rDate message is to be sen +t: ".$msg_month."/".$msg_day."/".$msg_year; # Create the multipart c +ontainer my ($msg) = MIME::Lite->new ( From => $from_address2, To + => $to_address, Subject => $subject, Type =>'multipart/mixed' ) +or die "Error creating multipart container: $!\n"; # Add the text me +ssage part $msg->attach ( Type => 'TEXT', Data => $message_body ) + or die "Error adding the text message part: $!\n"; # Add the file $ +msg->attach ( Type => $res, Path => '/var/www/html/upload', +Filename => $filename, Disposition => 'attachment' ) or die "Error + adding $filename: $!\n"; # Send the Message MIME::Lite->send('smtp' +, $mail_host, Timeout=>60); $msg->send; print $query->header; print +"<b>Thank you!</b><br>Confirmation of your submission will be emailed + to you."; } }

Replies are listed 'Best First'.
Re: uninitialized value in concatenation (.)
by graff (Chancellor) on Jun 06, 2006 at 21:00 UTC
    Regarding the first error (uninitialized value in concatenation at line 56), the previous replies are good, and I can suggest some other ways to do what you want:
    # like this: (my $newfile = $filename ) =~ s{.*[\\/]}{}; # if there is no (back)slash, $newfile will be same as $filename # or like this: my @path = split /[\/\\]/, $filename; my $newfile = pop @path; # or use File::Basename, if that's appropriate to your needs
    As for the second error (readline() on unopened filehandle at ... line 57) I would guess that line 57 is this one:
    while(<$filename>) {
    You are supposed to put an open file handle inside the "diamond" operator -- not a scalar string that holds the file name. Do something like this instead:
    open( IN, $filename ) or die "$filename: $!"; while (<IN>) {
Re: uninitialized value in concatenation (.)
by dsheroh (Monsignor) on Jun 06, 2006 at 20:40 UTC
    As written, $1 will receive the directory separator from (\\|\/) rather than the filename. Also, I find it a good practice to avoid using .* where possible. Finally, you need some sort of error handling for when the match fails.

    Taking these three things into account, I would rewrite the two lines which extract the filename as:

    $filename =~ m/^.*(\\|\/)([^\\\/]*)$/; my $newfilename = $2 || die "Filename $filename failed to match!\n";

    (Although in the real world, you'd probably want to attempt to recover gracefully from the failed match rather than dying...)

Re: uninitialized value in concatenation (.)
by socketdave (Curate) on Jun 06, 2006 at 20:18 UTC
    Make sure that your regex is actually matching and putting something into $newfilename. The error is pretty descriptive.

    Try printing out $filename inside your sub to make sure that it has what you expect in it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-25 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found