That's because they weren't defined in myswap. You're confusing local with my.
variables created with local are local to the sub/package/module/loop/whatever
esle they're created in. You seem to want to declare $y and $x as local within
myswap()
On Thu, Aug 28, 2008 at 11:14:00AM +0100, Braun Brelin wrote:
> 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/> >>
> >
> >
> --
> 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/>
--
--
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!