By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,002 Members | 2,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,002 IT Pros & Developers. It's quick & easy.

Perl strict and "my"

P: 1
Hello,

I started out learning Perl today, but strict mode is just *weird* and not usefull? Why should I use it?

What makes $Username different than the other variables?
I was wondering, why I have to use "my ($Username)" to get this working:


Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. # v1.pl
  3. # Works as supposed and connects to the database.
  4. use strict;
  5. use DBI;
  6. use DBD::mysql
  7.  
  8. my $Username = "a";
  9. my $Password = "b";
  10. my $Hostname = "c";
  11. my $Database = "d";
  12.  
  13. my $dsn        = "dbi:mysql:$Database:$Hostname:3306";
  14. my $connect    = DBI->connect($dsn, my($Username), $Password);
  15.  
  16.  
$ perl -c v1.pl
v1.pl syntax OK

but this doesn't work at all:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. # v2.pl
  3. # This one fails
  4. use strict;
  5. use DBI;
  6. use DBD::mysql
  7.  
  8. my $Username = "a";
  9. my $Password = "b";
  10. my $Hostname = "c";
  11. my $Database = "d";
  12.  
  13. my $dsn        = "dbi:mysql:$Database:$Hostname:3306";
  14. my $connect    = DBI->connect($dsn, $Username, $Password);
  15.  
  16.  
$ perl -c v2.pl
Global symbol "$Username" requires explicit package name at v2.pl line 14.
v2.pl had compilation errors.


$ perl -v
This is perl, v5.8.8 built for x86_64-linux-gnu-thread-multi



edit: it was the missing ; after "use DBD::mysql". Sorry, my mistake.
Feb 26 '08 #1
Share this Question
Share on Google+
1 Reply


KevinADC
Expert 2.5K+
P: 4,059
using "strict" will help you to write good perl code. It may be hard to see how or why it does, but keep using it.
Feb 26 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.