Do you really want that shebang line?!?#! perl -w scipt
Good! But then no need for -w.use strict; use warnings;
Not that bad, but in Perl one seldom really needs to predeclare a long list of variables; it's better to declare them as close as possible to where they're actually needed.my @files; my @List_of_commands; my $line; my $file;
Huh?!? Why notwhile (<*.sql>) { push (@files,$_); }
my @files=<*.sql>;
So why not avoiding to use the intermediate variable @files and just doforeach (@files) {
for (<*.sql>) { # I never use C<foreach> ;-)
So why not limiting $file to this this lexical scope? Also it would be better to$file = $_;
for my $file (<*.sql>) { # I still prefer C<for> orver C<foreach>!!
Didn't mummy told you toopen (FILE, "<$file");
Ditto as above!!while (<FILE>){ my $line = $_;
Hmmm sounds Perl6-ish. No, let me see better... oh, it's just a misplaced parenthesis. No, let me see better... they're two misplaced parens. Seems we've found the problem.if $line not =~ (/INSERT INTO Message_pool)/{ #Do nothing }
Incidentally, if #Do nothing is just a placeholder, then fine. But if you think you want to keep it like that, which is not totally unprobable, taking into account your coding {style,experience} as of what I could see above, then just know that eliminating the whole if statement will do just fine.
All in all I recommend reading some introductory Perl tutorial before going on...
Update: I gave a more comprehensive peek into the rest of your script and I think that it could be rewritten much more simply e.g. along this way:
Please note that I tried to keep as close as possible to your logic, but if it were just this functionality that you need, than I just wouldn't open the output file myself and use shell redirection instead. If this is to evolve into something bigger, then plainly ignore this comment! If it is not, then I would leave to the user to specify input and output on the cmd line:#!/usr/bin/perl # UNTESTED! use strict; use warnings; open my $out, '>', "outfile.txt" or die "Can't open output file: $!\n"; select $out; @ARGV=<*.sql>; while (<>) { next unless /^.{0,50}CREATE TABLE \s ([A-Z|a-z|_]{5,40}) \s .{1,100}/x; print "if exists (select 1 from INFORMATION_SCHEMA.tables where ta +ble_name = '$1') DROP TABLE '$1'\n"; } __END__
#!/usr/bin/perl -ln use strict; use warnings; BEGIN {@ARGV=map glob, @ARGV} # You're under Win*, aren't you? next unless /$whatever (\w+) $whatever/; print "whatever: $1"; __END__
In reply to Re: if expression
by blazar
in thread if expression
by Win
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |