How can I use switch -s and 'use strict' at the same time

Discussion in 'Perl Misc' started by Ting Wang, Oct 6, 2005.

  1. Ting Wang

    Ting Wang Guest

    Hi,
    I am learning to use switches of perl and I met
    following question. I hope someone can help me.


    I am trying to use switch -s with use strict, i
    wrote two scripts like:

    #--- script 1 ----
    #!/usr/bin/perl -s
    use strict;
    print $xyz;

    #--- script 2 ----
    #!/usr/bin/perl -s
    use strict;
    my $xyz;
    print $xyz;

    Both of the codes can not be run.
    Is there a chance using both -s and 'use strict'
    at the same time?


    Thanks
     
    Ting Wang, Oct 6, 2005
    #1
    1. Advertising

  2. Ting Wang

    Paul Lalli Guest

    Ting Wang wrote:
    > I am trying to use switch -s with use strict, i
    > wrote two scripts like:
    >
    > #--- script 1 ----
    > #!/usr/bin/perl -s
    > use strict;
    > print $xyz;
    >
    > #--- script 2 ----
    > #!/usr/bin/perl -s
    > use strict;
    > my $xyz;
    > print $xyz;
    >
    > Both of the codes can not be run.


    This is a remarkably poor error description. How can they not be run?
    Syntax errors? Incorrect output? Your mouse takes on a life of its
    own and restrains you from typing at the keyboard?

    Have you read the Posting Guidelines for this group?

    > Is there a chance using both -s and 'use strict'
    > at the same time?


    The -s option sets *global* variables, not lexical. In your script 2
    above, -s sets the variable $main::xyz equal to whatever value -xyz was
    given on the command line. Then you declare a lexical variable my
    $xyz. The two variables are completely unrelated.

    Two options:
    1) fully qualify the package variable:
    print $main::xyz
    2) Declare the ability to use the package variable without qualifying,
    instead of declaring a new lexical:
    our $xyz;
    print $xyz;

    Paul Lalli
     
    Paul Lalli, Oct 6, 2005
    #2
    1. Advertising

  3. Ting Wang

    John Bokma Guest

    "Paul Lalli" <> wrote:

    > Two options:
    > 1) fully qualify the package variable:
    > print $main::xyz
    > 2) Declare the ability to use the package variable without qualifying,
    > instead of declaring a new lexical:
    > our $xyz;
    > print $xyz;


    Now and then I try to think up the answer, and don't post it, because
    either I am not sure, or I have no time to verify my solution (or both).

    I am happy to see that mine equals 2), and thanks for reminding me of 1 :)

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
     
    John Bokma, Oct 6, 2005
    #3
  4. Ting Wang

    Paul Lalli Guest

    John Bokma wrote:
    > "Paul Lalli" <> wrote:
    >
    > > Two options:
    > > 1) fully qualify the package variable:
    > > print $main::xyz
    > > 2) Declare the ability to use the package variable without qualifying,
    > > instead of declaring a new lexical:
    > > our $xyz;
    > > print $xyz;

    >
    > Now and then I try to think up the answer, and don't post it, because
    > either I am not sure, or I have no time to verify my solution (or both).
    >
    > I am happy to see that mine equals 2), and thanks for reminding me of 1 :)


    Heh. I had actually never even heard of -s before. `perldoc perlrun`
    wasn't explicit about what kind of variables are set, so I more or less
    guessed that it would set globals, and a quick test proved me right.
    :)

    Paul Lalli
     
    Paul Lalli, Oct 6, 2005
    #4
  5. Ting Wang

    Ting Wang Guest

    Sorry for my poor error description I will
    find the guideline to read.

    Thanks for you antwort.
     
    Ting Wang, Oct 6, 2005
    #5
  6. Ting Wang

    John Bokma Guest

    "Paul Lalli" <> wrote:

    >
    > John Bokma wrote:
    >> "Paul Lalli" <> wrote:
    >>
    >> > Two options:
    >> > 1) fully qualify the package variable:
    >> > print $main::xyz
    >> > 2) Declare the ability to use the package variable without
    >> > qualifying, instead of declaring a new lexical:
    >> > our $xyz;
    >> > print $xyz;

    >>
    >> Now and then I try to think up the answer, and don't post it, because
    >> either I am not sure, or I have no time to verify my solution (or
    >> both).
    >>
    >> I am happy to see that mine equals 2), and thanks for reminding me of
    >> 1 :)

    >
    > Heh. I had actually never even heard of -s before.


    I did, because of Usenet. Some time ago there was a kind of Perl golf
    thingy going on (Dutch ng), and I read up again on all those funny
    switches.

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
     
    John Bokma, Oct 6, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Krick
    Replies:
    1
    Views:
    1,463
    John C. Bollinger
    Aug 15, 2003
  2. flamesrock
    Replies:
    8
    Views:
    469
    Hendrik van Rooyen
    Nov 24, 2006
  3. StephaneLeFou
    Replies:
    3
    Views:
    462
    Arne Vajhøj
    Dec 11, 2007
  4. Marco Devillers

    Circumventing the -fno-strict-aliasing switch

    Marco Devillers, May 25, 2011, in forum: C Programming
    Replies:
    26
    Views:
    974
    J. J. Farrell
    May 31, 2011
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    109
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page