padawan_linuxero has asked for the wisdom of the Perl Monks concerning the following question:

Hello I am newbie to Perl and DBI I have been working on
this code wish is a sample on how to view all data
from a mysql but here is the cuestion, everywhere they
told me to use strict, but everytime I try to run this program
it fail giving me this error message:


Global symbol "@row" requires explicit package name at conexionMySql2.pl line 13.
Global symbol "@row" requires explicit package name at conexionMySql2.pl line 14.


so I did the unthinkable I put a #on use strict and it
WORK!!!!

Can someone help me understanding why this is like this?
TIA
#!/usr/bin/perl use strict; use DBI(); my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost", "root", "123xyz", {'RaiseError' => 1}); my $sth = $dbh->prepare("SELECT * FROM regtest"); $sth->execute(); while ((@row) = $sth->fetchrow_array()) { print "@row\n"; } $sth->finish(); $dbh->disconnect();

Replies are listed 'Best First'.
Re: "use strict" and undeclared variables
by moritz (Cardinal) on Oct 11, 2007 at 14:31 UTC
    With use strict in effect you have to declare variables.

    You can change your code to read while (my @row = $sth->fetchrow_array()){ to fix it and still use strict;.

Re: "use strict" and undeclared variables
by toolic (Bishop) on Oct 11, 2007 at 14:36 UTC
    In addition to what mortiz said, if you haven't already done so, read strict and my. That should give you a better understanding of why you should use strict and why you get this error.

    Also, you should: use warnings;

Re: "use strict" and undeclared variables
by roboticus (Chancellor) on Oct 11, 2007 at 14:33 UTC
Re: "use strict" and undeclared variables
by ikegami (Patriarch) on Oct 11, 2007 at 19:46 UTC

    so I did the unthinkable I put a # on use strict and it WORK!!!!

    It works in the sense that removing your shoe laces from your shoes will prevent you from tripping on them, but it'll make walking trickier.

Re: "use strict" and undeclared variables
by leocharre (Priest) on Oct 11, 2007 at 19:39 UTC

    omfg.. yours is the funniest post i've seen in a long time..

    It's because @row is not being declared.. that is.. it's not being told 'row exists in this area of the program'.

    keep using strict! Trust the strict- Strict is good- Strict is your friend. It's your girlfriend who loves you but won't put up with any of your 5h1+.

    If she whines and complains.. it means you need to stop that- or your code 'looks' like it's working.. but it's *not*.

    Like moritz suggested, this should work:

    while ( my @row = $sth->fetchrow_array ) { print "@row\n"; }

    Again, what is happening is that you don't say anywhere 'my @row'.

    You could also have done this.. (althought bad form and possibly buggy):

    my @row; while ( @row = $sth->fetchrow_array ) { print "@row\n"; }

    updateikegami suggests these snipplets are essentially the same, and that the second is not bad form or possibly buggy. And it was the # on strict that cracked me up. It's like when my two year old goges 'mommy i'm hot' so she pours a glass of cold organge juice all over herself and says 'now everything is great!'- that it's funny doesn't mean you have ill feelings for the situation.

      No need to be rude about it - as poster stated, they are a newbie to Perl. After all, asking questions, after having at least made an attempt, is how people LEARN.
      What's bad/buggy about the latter? I always do that (hangover from C I guess).
      I prefer not to mix declarations and usage. Also, it means I can comment the declarations...

      Cheers
      Chris

        As a rule of thumb, you should try to restrict the scope of a variable as much as possible. The former restricts @row life to the while block, while the other goes beyond. Anyway, I wouldn't classify this neither as "bad" nor as "buggy".

        Flavio
        perl -ple'$_=reverse' <<<ti.xittelop@oivalf

        Io ho capito... ma tu che hai detto?