Perl has no global variables. What is Perl:
- Package variables.
- Variables with vocabulary.
A package is a namespace. In Perl, the namespace is sometimes called a package. Your default name is main . For instance. This is completely legal:
use strict; use warnings; $main::variable = "What? Where my 'our' or 'my' declaration?"; print "Look, I can print $main::variable without using 'my' or 'our'!";
I just prefix the variable names of the package with the package and wham! They exist!
This makes me horrified:
use strict; use warnings; $variable = "What? Where my 'our' or 'my' declaration?"; print "I'm not going to print 'cause you're going to get a compilation error";
Using use strict; you must either declare the variable as our , or my , or prefix it with the name of the package in which it is located.
Batch variables are easiest to understand. Package variables are actually stored in the Perl variable structure, so they are always available after the declaration:
use strict; use warnings; if ( 1 == 1 ) {
Lexically restricted variables are harder to understand. Basically, a variable with a lexical scope is in scope in a specific block, but out of scope as soon as you leave that block. It is also available in sub-blocks:
use strict; use warnings; my $foo = "Foo has a value"; if ( $foo ) {
Here are some suggestions:
- You do not need to prescribe routines. It just asks for trouble.
- If you define your variables at the beginning of your program, you can use the
my variables, and they will be available in your routines, as they will still be in scope. - Leave
& turn off routines. They cause subtle changes in the way the routine works, and these subtle changes are probably not what you want. A standard is just a subroutine call.- Avoid
? ... : ... especially if you don't use spaces. This makes it difficult to read your program and does not save runtime. - Compute a subroutine parameter into variables as soon as you call your subroutine.
- Perl interpolates variables for you. There are many problems with Perl. It does not have a real object orientation. This is not an exception oriented language. It has a lot of coolness. One of the big advantages is that you do not need to go through all kinds of frauds to print the value of a variable. Use it and keep your head proud when you hang Python fans.
- Use spaces to make your code easier to read.
- Perhaps you really need constants.
my will work, but the constants guarantee that these values โโwill not be accidentally changed in your program.
Your code is rewritten here. Note that constants do not have a sigil in front. They usually cannot be interpolated in rows. However, if you surround them with @{[...]} , you can also interpolate them. I did this in two ways:
use 5.16.3; use strict; use Getopt::Long; use constant { SQL_USER => "root", SQL_PASS => "mypassword", }; Getopt::Long::Configure qw(bundling no_getopt_compat); sub ArgParser { print "Username is " SQL_USER . " Password is " . SQL_PASS . "\n"; my $crt; my $delete; GetOptions ( 'create|c=s' => \$crt, 'delete|d=s' => \$delete, ); if ( $crt ) { DatabaseExec( "create", $crt ); } elsif ( $delete ) { DatabaseExec( "delete", $delete ); } else { print "No options chosen\n"; } } sub DatabaseExec { use DBI; my $comand = shift; my $dbname = shift; print "Username is @{[SQL_USER]} Password is @{[SQL_PASS]}\n"; my $dbh = DBI->connect( "dbi:mysql:", SQL_USER, SQL_PASS ); if ( $command eq "create" ) { my $db_com = "create database $dbname"; if ( $dbh->do( $db_com ) ) { print "Database created\n" } else { print "An error occured while creating database. Maybe it exists?\n"; } } elsif ( $command eq "delete" ) { my $db_com = "DROP DATABASE $dbname";