[ILUG] Silly perl question

Braun Brelin bbrelin at gmail.com
Thu Aug 28 11:14:00 IST 2008


The other odd thing is that the perl I'm using doesn't seem to have local
vs. global variable scoping...


--   Perl script --
#!/usr/bin/perl
use strict;

my $x=1;
my $y=10;

myswap();

sub myswap {
    $y = $x;
}

print "y = ",$y,"\n";

-------------------------------------------------------------------------

Not only doesn't it complain about x and y not being
defined in myswap, it goes ahead and changes the
x and y defined outside the myswap function...

Braun



On Thu, Aug 28, 2008 at 11:10 AM, Braun Brelin <bbrelin at gmail.com> wrote:

> Doesn't use strict require that all variables be declared before they are
> used regardless of whether they're global or local?
>
> Braun
>
>
>
> On Thu, Aug 28, 2008 at 11:06 AM, Jimmy O'Regan <joregan at gmail.com> wrote:
>
>> 2008/8/28 Braun Brelin <bbrelin at gmail.com>:
>> > Hello all,
>> >
>> > I have a silly Perl question.  I'm running an Ubuntu Linux system
>> (8.04),
>> > with Perl 5.8.8.
>> >
>> > However, the 'use strict' pragma seems not to be working.  I.e. I have
>> the
>> > following
>> > Perl script:
>> >
>> > #!/usr/bin/perl
>> >
>> > use strict;
>> >
>> > $a=10;
>> >
>> > print "a = ",$a,"\n";
>> >
>> > This should fail miserably with an error about not having $a declared,
>> but,
>> > unfortunately, it doesn't.  It just prints '10'.
>> >
>> > This isn't my normal machine so I'm not sure how Perl was built,
>> although I
>> > suspect
>> > that it isn't anything more than a standard install.  perl -V didn't
>> really
>> > give me any clues.  Anything stupidly obvious that I'm missing?
>>
>> You've declared it as a global variable; 'my' is used to declare a
>> local variable
>>
>> #!/usr/bin/perl
>>
>> use warnings;
>> use strict;
>>
>> foo();
>>
>> sub foo
>> {
>>        $a=10;
>>
>>        print "a = ",$a,"\n";
>> }
>> print $a;
>>
>> gives:
>> a = 10
>> 10
>> --
>> Irish Linux Users' Group mailing list
>> About this list : http://mail.linux.ie/mailman/listinfo/ilug
>> Who we are : http://www.linux.ie/
>> Where we are : http://www.linux.ie/map/
>>
>
>



More information about the ILUG mailing list