Loading ...
Sorry, an error occurred while loading the content.
 

Re: [PBML] why use Strict?

Expand Messages
  • raezorblaedz
    Wow. That makes perfect sense. Why don t you revise some of the crappy (I m a hardcore coder) documentation, and write books for O Reilly. Even their Beginning
    Message 1 of 9 , Jan 2, 2002
      Wow. That makes perfect sense. Why don't you revise some of the
      crappy (I'm a hardcore coder) documentation, and write books for
      O'Reilly. Even their Beginning Perl book doesn't state things that
      clearly.
      thank you,
      John

      --- In perl-beginner@y..., Greg Webster <greg@g...> wrote:
      > On Wed, 2 Jan 2002, raezorblaedz wrote:
      > > Sorry to sound so ignorant, but what I believe you just said was
      the
      > > following:
      >
      > No worries, we all have to learn in this world. Plus, this wouldn't
      be
      > called the Perl-Beginners Mailing List if we weren't able to answer
      > questions. :)
      >
      > > I declared:
      > >
      > > $MyOutFile = "/tmp/harvey@w...";
      > >
      > > but using strict, I should delclare the following:
      > >
      > > my ($MyOutFile = "/tmp/harvey@w...");
      > >
      > > right?
      >
      > In the case of a single variable declaration like this you don't
      need the
      > brackets.
      >
      > ie.
      > my $variable = "some data";
      >
      > Remember, you only need to use 'my' once for each variable.
      >
      > What you can do to get by a lot of this, especially in converting a
      > already-written script to use strict, is have a variable
      declaration
      > section at the top of your script. For example:
      >
      > my ($variable1, $var2, @foo, @bar, $var3);
      >
      > ....
      > further down in the script...
      > ....
      >
      > $var2 = "some text";
      >
      > I can use all the things I declared in the 'my' at the top of the
      script
      > throughout the script without re-declaring them. This is the
      simplest
      > example of using 'my'.
      >
      > Where the real power comes in though is in what is called "scope".
      Meaning
      > that you can delcare a variable only for a specific use. Take this
      > example:
      >
      > use strict;
      >
      > my ($var1, $var2);
      >
      > $var1 = "text for variable 1";
      >
      > sub this_subroutine {
      > my $var3 = "text for variable 3";
      > $var2 = "text for variable 2";
      > print "$var1\n";
      > print "$var2\n";
      > print "$var3\n";
      > }
      >
      > &this_subroutine;
      >
      > $var1 and $var2 are declared 'globally' (meaning they are
      > accessible anywhere in the script). $var3 is declared locally in a
      > subroutine, and so is only accessible inside that subroutine. Once
      the
      > subroutine is over, $var3 disappears from memory. If I tried to
      put '$var3
      > = "some new text for variable 3";' outside the subroutine without
      > declaring it globally I'd get errors. This prevents from from
      making
      > mistakes about how I am using the variables in my script.
      >
      > Understand?
      >
      > GregW
    • Greg Webster
      ... I ve actually thought about this quite a bit. I m currently working on a sci-fi novel (yeah, seriously), but maybe I should take a break and write some
      Message 2 of 9 , Jan 2, 2002
        On Wed, 2 Jan 2002, raezorblaedz wrote:

        > Wow. That makes perfect sense. Why don't you revise some of the
        > crappy (I'm a hardcore coder) documentation, and write books for
        > O'Reilly. Even their Beginning Perl book doesn't state things that
        > clearly.
        > thank you,
        > John

        I've actually thought about this quite a bit. I'm currently working on a
        sci-fi novel (yeah, seriously), but maybe I should take a break and write
        some easy-to-read perl docs. Probably pay better anyway :)

        GregW
      Your message has been successfully submitted and would be delivered to recipients shortly.