Professional Web Applications Themes

question about strict - PERL Beginners

Dear All, I have two perl scripts: - The first one: sub Tt { $TTT = uc($TTT); if(($TTT ne "ON") && ($TTT ne "OFF")){ print "ERROR: Check variable TTT";} else { print "It is a Test...";} } # ---MAIN--- $TTT = "OFF"; Tt(); If I use $TTT = "OFF", I get 'It is a Test...' and if I use $TTT="O" I get 'ERROR: Check variable TTT'. This, it is normal for me. - The second script using 'strict' this time: use strict; sub Tt { my $TTT; $TTT = uc($TTT); if(($TTT ne "ON") && ($TTT ne "OFF")){ print "ERROR: Check ...

  1. #1

    Default question about strict

    Dear All,

    I have two perl scripts:
    - The first one:
    sub Tt {
    $TTT = uc($TTT);
    if(($TTT ne "ON") && ($TTT ne "OFF")){ print "ERROR: Check variable TTT";}
    else { print "It is a Test...";}
    }
    # ---MAIN---
    $TTT = "OFF";
    Tt();

    If I use $TTT = "OFF", I get 'It is a Test...' and if I use $TTT="O" I get
    'ERROR: Check variable TTT'. This, it is normal for me.

    - The second script using 'strict' this time:
    use strict;
    sub Tt {
    my $TTT; $TTT = uc($TTT);
    if(($TTT ne "ON") && ($TTT ne "OFF")){ print "ERROR: Check variable TTT";}
    else { print "It is a Test...";}
    }
    # ---MAIN---
    my $TTT = "OFF";
    Tt();

    If I use $TTT = "OFF" I get time 'ERROR: Check variable TTT', Why ?

    Thanks, Francois




    Fyd Guest

  2. #2

    Default Re: question about strict

    FyD wrote: 

    Because $TTT is undef.
    When you declare $TTT i Tt perl creates a new variable and hides away the $TTT
    from the "main" part of your script to be restored when Tt returns.

    Change 'my $TTT;' in Tt to my '$TTT = shift;' and call Tt like this 'Tt($TTT)'.

    You should *always* use, use strict;.

    --
    Flemming Greve Skovengaard The killer's breed or the Demon's seed,
    a.k.a Greven, TuxPower The glamour, the fortune, the pain,
    <cybercity.dk> Go to war again, blood is freedom's stain,
    4112.38 BogoMIPS Don't you pray for my soul anymore.

    Flemming Guest

  3. #3

    Default Re: question about strict

    On 7/21/2004 8:20 PM, FyD wrote: 

    In the first example, you are setting a _package_ variable named $TTT to
    a value in your main script, and then you are calling a function which
    references that _package_ variable.

    In the second example, you are creating and setting a _lexical_ variable
    in your main script. You then call a function which declares a new
    lexical variable in the new scope which hides the lexical in the
    surrounding scope.

    BTW, I'm assuming this is just a contrived example. Normally you would
    not set a variable and access it across a function boundary. The proper
    way, of course, would be to pass the variable as an argument to the
    function.

    Randy Guest

Similar Threads

  1. using strict
    By DBSMITH@OhioHealth.com in forum PERL Beginners
    Replies: 18
    Last Post: April 5th, 06:26 PM
  2. use strict and filehandles
    By B McKee in forum PERL Beginners
    Replies: 2
    Last Post: January 21st, 05:04 PM
  3. How to propagate -w and use strict?
    By Steve in forum PERL Miscellaneous
    Replies: 10
    Last Post: October 21st, 03:53 PM
  4. Using strict references
    By Todd Bair in forum PERL Miscellaneous
    Replies: 7
    Last Post: August 21st, 09:18 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139