Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: using strict and functions

by talexb (Chancellor)
on Jan 17, 2002 at 22:16 UTC ( [id://139575]=note: print w/replies, xml ) Need Help??


in reply to using strict and functions

In addition to the excellent technical answers you're getting, let me also add a style comment.

Only declare variables when you need them. If you need a loop variable, don't declare it away at the beginning of the routine or (horrors!) at the top of the file. Just declare it where you're gonna need it, like this:

for ( my $Index = 0; $Index < 5; $Index++ ) { ... # code here that uses $Index }
If you're going to need two variables somewhere in the middle of a block of code, declare them there. In some languages you do have to declare all variables at the beginning (Pascal, and I think BASIC) .. but C and Perl give you more flexibility. So you could write
{ # Start black of code .. .. { # Start another block of code my ( $Index, $NodeNumber ); .. .. } .. .. }
This just declares the two variables where you need them. As soon as the scope (the code between the braces) ends, the variables disappear. Very neat, very clean.

--t. alex

"Of course, you realize that this means war." -- Bugs Bunny.

Replies are listed 'Best First'.
Re: Re: using strict and functions
by rob_au (Abbot) on Jan 18, 2002 at 04:27 UTC
    One point here ... Your use of the loop construct of for (;;) is very C-stylish and possibly not the best way to perform a simple forward iteration as your suggest. A more straight-forward way would be to make use of the range operator. eg.
    for (0..5) { # stuff goes here }

    If you require lexical scoping of the iteration variable, you can do so with the variable name and the my keyword.

    This syntax and the range operator are discussed in perlsyn and perlop respectively.

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Re: Re: using strict and functions
by metadoktor (Hermit) on Jan 18, 2002 at 00:58 UTC
    In some languages you do have to declare all variables at the beginning (Pascal, and I think BASIC) .. but C and Perl give you more flexibility.

    Hmm...I think you meant to say C++ here. C doesn't give you that kind of flexibility; however, C++ does.

    metadoktor

    "The doktor is in."

      Hmm...I think you meant to say C++ here. C doesn't give you that kind of flexibility; however, C++ does.
      #include <stdio.h> int main ( int iArgC, char * apsqArgV[] ) { printf ( "Hello, world! This is cygwqin speaking.\n" ); { char szMsg[] = "Not so!"; printf ( "Now inside scope, msg is %s.\n", szMsg ); } return ( 0 ); // Success is zero in Windows. }
      This is the kind of scope that I was talking about. The char array szMsg is declared at the start of the scope; this code compiles and runs correctly under CygWin (Win98).

      You may have been talking about the following:

      void FooBar ( void ) { int iBeer, iVodka; float fCredit = 20.0; .. /* Code here */ iBeer++; fCredit -= 2.50; .. /* More code */ char *apszVarious[]; /* ILLEGAL in C */ }
      I will grant that this kind of variable declaration is valid in Perl but not valid in C.

      --t. alex

      "Of course, you realize that this means war." -- Bugs Bunny.

        When I have been studying C it was illegal C code. Well, it was long time ago. Maybe standart have been changed. Or it is just a gcc extension to C (gcc has plenty of them).

        BTW function prototypes was not always part of C. IIRC some time ago you had to rewrite it as:

        #include <stdio.h> int main ( iArgC, apsqArgV ) int iArgC; char * apsqArgV[]; { char szMsg[] = "Not so!"; printf ( "Hello, world! This is cygwqin speaking.\n" ); { printf ( "Now inside scope, msg is %s.\n", szMsg ); } return ( 0 ); // Success is zero in Windows. }

        --
        Ilya Martynov (http://martynov.org/)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://139575]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-29 13:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found