I’m developing a small Perl script to automate some server tasks and when I test it on my Windows machine everything works, but when I upload it to my Linux server the script returns multiple:
Use of uninitialized value $line in chomp at CreateEntity.pl line 52, <HANDLE> line 5.
Use of uninitialized value $line in string ne at CreateEntity.pl line 52, <HANDLE> line 5.
Use of uninitialized value $string in substitution (s///) at CreateEntity.pl line 67, <HANDLE>
Use of uninitialized value $string in substitution (s///) at CreateEntity.pl line 68, <HANDLE>
Use of uninitialized value $string in substitution (s///) at CreateEntity.pl line 67, <HANDLE>
Use of uninitialized value $string in substitution (s///) at CreateEntity.pl line 68, <HANDLE>
And here is the code itself:
#!/usr/bin/env perl
# This fils SHOULD go in the root of Symfony... put it anywhere else AT YOUR OWN RISK !!!
use warnings;
# Declare the subroutines
sub trim;
if(!defined $ARGV[0])
{
die( "No FilePath Specified!\n" );
}
my $entityFile = $ARGV[0];
open( HANDLE, $entityFile ) or die( "The file could not be open!" );
my $entityCmd = "php app/console doctrine:generate:entity --entity=\"rapliqBundle:";
my $entityName = "";
chomp( my $line = ( <HANDLE> ) );
$line = trim( $line );
while( $line ne "END" )
{
if( trim( $line ) ~~ "NAME:" )
{
chomp( $line = ( <HANDLE> ) );
$entityName = trim( $line );
$entityCmd = $entityCmd . $entityName;
$entityCmd = $entityCmd . "\" --fields=\"";
}
elsif( trim( $line) ~~ "FIELDS:" )
{
chomp( $line = ( <HANDLE> ) );
my @data = split( '=', $line );
foreach my $val (@data)
{
$val = trim( $val );
if( $val ~~ lc "string" )
{
$entityCmd = $entityCmd . $val . "(255) ";
}
else
{
$entityCmd = $entityCmd . $val . ":";
}
}
}
52: chomp( $line = ( <HANDLE> ) );
}
close( HANDLE );
$entityCmd = $entityCmd . "\"";
#print $entityCmd;
system( $entityCmd );
system( "php app/console doctrine:generate:entities rapliqBundle:" . $entityName );
system( "php app/console doctrine:schema:update --force");
sub trim
{
my $string = $_[0];
67:$string =~ s/^\s+//;
68:$string =~ s/\s+$//;
return $string;
}
Thanks for any idea or comments 🙂
EDIT: I put the line number for the concerned line in front of the line of code.
It all comes down to handling the end of your input file better.
Use of uninitialized value $line in chomp at CreateEntity.pl line 52, line 5.
<HANDLE>returns undef to indicate you’ve reached the end of the file. You ignore this and attempt to chomp the returned value.Use of uninitialized value $line in string ne at CreateEntity.pl line 52, line 5.
Misreported line number. The warning is actually coming from
$line ne "END".$lineis still undefined from having reached the end of the file.Use of uninitialized value $string in substitution (s///) at CreateEntity.pl line 67,
Use of uninitialized value $string in substitution (s///) at CreateEntity.pl line 68,
trim( $line ) ~~ "NAME:"$lineis still undefined from having reached the end of the file.Use of uninitialized value $string in substitution (s///) at CreateEntity.pl line 67,
Use of uninitialized value $string in substitution (s///) at CreateEntity.pl line 68,
trim( $line) ~~ "FIELDS:"$lineis still undefined from having reached the end of the file.You should trim a line only once, and there’s no need to chomp if you’re going to trim.
In a flash of brilliance, I think I know what you’re underlying problem is 🙂
Your data file has Windows line endings. You chomp the line, leaving you with
"END\r"instead of"END". Thedos2unixcommand line tool would fix that, and so wouldtrimming.