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

T

Ting Wang

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
 
P

Paul Lalli

Ting said:
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
 
J

John Bokma

Paul Lalli said:
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 :)
 
P

Paul Lalli

John said:
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
 
T

Ting Wang

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

Thanks for you antwort.
 
J

John Bokma

Paul Lalli said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top