[ILUG] Silly perl question

Braun Brelin bbrelin at gmail.com
Thu Aug 28 11:25:50 IST 2008


Cian,

Yes, but my point is that I wouldn't expect the program
to run at all.  I would have expected that within myswap, y and x would be
local variables, and thus, with the use strict pragma enabled, that perl
would require x and y to be declared within myswap with the 'my'
declaration.   Otherwise, what happens to the concept of scoping?

Braun


On Thu, Aug 28, 2008 at 11:18 AM, Cian Brennan <lil_cain at redbrick.dcu.ie>wrote:

> 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/
> >
>
> --
>
> --
>



More information about the ILUG mailing list