Perl strict and "my"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • incd
    New Member
    • Feb 2008
    • 1

    Perl strict and "my"

    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:


    Code:
    #!/usr/bin/perl -w
    # v1.pl
    # Works as supposed and connects to the database.
    use strict;
    use DBI;
    use DBD::mysql
    
    my $Username = "a";
    my $Password = "b";
    my $Hostname = "c";
    my $Database = "d";
    
    my $dsn		= "dbi:mysql:$Database:$Hostname:3306";
    my $connect	= DBI->connect($dsn, my($Username), $Password);
    $ perl -c v1.pl
    v1.pl syntax OK

    but this doesn't work at all:

    Code:
    #!/usr/bin/perl -w
    # v2.pl
    # This one fails
    use strict;
    use DBI;
    use DBD::mysql
    
    my $Username = "a";
    my $Password = "b";
    my $Hostname = "c";
    my $Database = "d";
    
    my $dsn		= "dbi:mysql:$Database:$Hostname:3306";
    my $connect	= DBI->connect($dsn, $Username, $Password);
    $ 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.
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    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.

    Comment

    Working...