in reply to Re: Regular Expression Question
in thread What happens with empty $1 in regular expressions? (was: Regular Expression Question)

I looked up exactly what 'strict' is supposed to do, and the Camel book says its supposed to disallow "unsafe" code. My question to anyone else is what is considered "unsafe"?

I don't know whether you're looking at Camel II or III, but they both provide full documentation on strict. The Second edition documents the strict pragma beginning on page 500, and the Third edition beginning on page 858.

Of course, the Camel book is not the only source of documentation. Each core module and pragma also comes with built-in documentation. You can read the standard documentation for strict with perldoc strict (or the equivalent on Windows [the HTML-ized docs] or Mac [Shuck]).

The docs from 5.005_02 are even available on this site, including the docs for strict.

To answer your question, three things are considered 'unsafe'. Each one is controlled by a separate part of strict. strict 'refs' prevents the use of symbolic references. strict 'vars' prevents the use of variables which are not pre-declared or fully qualified. strict 'subs' prevents the use of barewords.

Replies are listed 'Best First'.
Re: Re: Re: Regular Expression Question
by dsb (Chaplain) on Mar 01, 2001 at 00:47 UTC
    That much I got(didn't mean to sound like a d**khead). What I don't get is why that is unsafe. Is it because bareword could be confused with a built in function or something like that? Why are undeclared variables unsafe? Same kind of thing?

    Excuse my ignorance. I am starting to really get interested in the theories of programming and things like that and I would really like to get a handle on this stuff. I taught myself Perl about a year ago and I've had no one to ask these questions to. They are all sort of flooding out now.

    Thanks for your help.

    Amel - f.k.a. - kel

      I see, I misunderstood your question. My apologies.

      One of the main reasons these practices are unsafe is that they often occur by accident.

      Here are some quick examples of mistakes that will cause errors with strict:

      my $variable = 7; print $varaible; # typo # error from strict 'vars' my $hash = { name => 'value' }; my $key = 'name'; print $key->{'name'} # derefenced wrong variable # error from strict 'refs' sub subroutine { } print subrotine; # typo # errors from strict 'subs'
      Using strict also encourages good coding practices such as controlling the scope of your variables (strict 'vars') and using hard references instead of soft references (strict 'refs').

      In strict.pm I go over why barewords can be a problem. I don't agree with the term "unsafe" here. strict.pm helps Perl find simple mistakes for you.

      Undeclared variables are only a problem in that they prevent Perl from (reliably) telling you when you put a typo in a variable name.

              - tye (but my friends call me "Tye")