Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Invalid sub ignored on use strict;

by wil (Priest)
on Apr 30, 2002 at 10:57 UTC ( [id://163022]=perlquestion: print w/replies, xml ) Need Help??

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

Hi

I was going back over very old code the other day, and came across the following block:

if ($subscribe) { my $dbh = $mysql->connect; my $sth = $dbh->prepare("INSERT INTO wyb_news VALUES(?,?,?)") or die("Can't execute SQL query: [$DBI::err] $DBI::errstr"); $sth->execute ('',"$name","$email") or dir("Can't execute SQL query: [$DBI::err] $DBI::errstr"); $dbh->disconnect; }

Note the typo in the line line or dir("Can't exe... This is actually run under use strict;.

Now what's got me really confused is that this code has been working perfectly for months, and never once returned an error - and yes, the block has been evaluated and proven true many times.

How did this escape the strictness of use strict;? I would of thought that using strict would of picked up on this typo and wouldn't allow the script to run?

It would be most appreciated if anyone can shed any light on the matter! I'm baffled.

- wil

Replies are listed 'Best First'.
Re: Invalid sub ignored on use strict;
by Dog and Pony (Priest) on Apr 30, 2002 at 13:59 UTC
    The paranthesis makes it pass the strict test. I guess strict figures that if you use paranthesis, you know what you are doing. It only detects barewords outside of curlies and not in front of the => operator, if I recall correctly. And if it finds one, it checks if it knows about any such sub (but it only checks backwards in the script). Thus, many place their main code last, after all the subs to be sure, and others (like me) place it first, and use &sub, which I think is the best. But I am drifting...

    Here are some examples on what passes strict and not:

    #!/usr/bin/perl -w use strict; # With a subroutine defined: foo(); # Prints "Stuff" &foo; # This too. foo; # Blows up, no such sub... yet. sub foo { print "Stuff\n"; } foo; # Is also ok, prints "Stuff" # With no subroutine defined: bar(); # Compiles, blows up at runtime *if* executed &bar; # This one too bar; # Blows up at compile time
    So what has happened is that your solid code has not ever been evaluated to false... yet. ;-)
    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.
      Thank you for the explanation! That actually makes sense for what I'm doing. The evaluation has never been false, or it has never died (thankfully my database is preety solid). Still seems wrong, though, for some reason..

      - wil
Re: Invalid sub ignored on use strict;
by Sifmole (Chaplain) on Apr 30, 2002 at 13:02 UTC
    Is your code using Net::FTP anywhere? There is a dir() method in that library.

    In the same vein, it could possibly that somewhere along the line you are using a module and importing, or which exports, a function named dir.

      Hi

      Good point! I'm not actually using any module apart from CGI.pm here and I'm 99% sure that there's no dir function or sub within CGI.pm - anyway, doesn't it import them into it's own namespace?

      Thanks for your suggestion!

      - wil
Re: Invalid sub ignored on use strict;
by strat (Canon) on Apr 30, 2002 at 11:24 UTC
    I've just run the following tests:
    H:\>perl -Mstrict -w (1==1) or dir "hello"; String found where operator expected at - line 1, near "dir "hello"" (Do you need to predeclare dir?) ^Z syntax error at - line 1, near "dir "hello"" Execution of - aborted due to compilation errors.
    or:
    H:\>perl -Mstrict -w (1==0) or dir "hello"; String found where operator expected at - line 1, near "dir "hello"" (Do you need to predeclare dir?) ^Z syntax error at - line 1, near "dir "hello"" Execution of - aborted due to compilation errors.

    Is it possible that this code you posted comes from a module or the like where strict is not loaded?

    (Btw: I tried it with Win2k and ActivestatePerl 631)

    Best regards,
    perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

      true. but look at the snippet again and try your code:

      $ perl -Mstrict -w (1==1) or dir( "hello" ); $ perl -Mstrict -w (1==0) or dir("hello"); Undefined subroutine &main::dir called at - line 1.

      Luck's played a big part here and the conditional statement has never been exercised (unless of course dir is defined somewhere). The parens counter the 'use strict' which checks for the use of barewords as subs.

      -derby

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Invalid sub ignored on use strict;
by chromatic (Archbishop) on Apr 30, 2002 at 19:39 UTC
    If strict complained about things that looked like valid sub calls (at compile time) but weren't actually valid (at compile time), it would break AUTOLOAD and, quite possibly, inherited methods.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://163022]
Approved by strat
Front-paged by gmax
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found