SPUG: re: 'my' question
Alyssa Harding
axhard2 at uswest.com
Fri May 19 13:34:00 CDT 2000
I am quite sure it was changing the script to strict that caused the
problem with the 'my $pair(@pairs)'.
I found out the OS and stuff. It was FreeBSD, Perl 5.005_03 on
Apache.
I'm glad I wasn't crazy when I was thinking that should work - don't
know why it didn't but thank goodness TMTOWTDI!
A.
Adrian Hands wrote:
>
> Are you sure ? This seems to give the expected result:
>
> #!/usr/bin/perl -w
>
> use strict;
>
> my @pairs = qw{ one&uno two&dos three&tres };
>
> #...
> my %hash;
> foreach my $pair( @pairs ){
> my($name, $value) = split(/&/, $pair);
> $hash{$name} = $value;
> }
>
> use Data::Dumper;
> print Dumper(\%hash);
>
> $ ./foo.pl
> $VAR1 = {
> 'three' => 'tres',
> 'two' => 'dos',
> 'one' => 'uno'
> };
> $
>
> Alyssa Harding wrote:
> >
> > Okay, sorry, there was a typo in there, I was trying to split $pair,
> > not $buffer in the first foreach loop, the quoted text below is now
> > correct.
> >
> > A.
> >
> > Alyssa Harding wrote:
> > >
> > > I encountered something wierd yesterday and I wonder if someone
> > > could explain why this happens.
> > >
> > > I had a script which was doing something like this:
> > > #!/usr/bin/perl -w
> > >
> > > ...
> > >
> > > foreach $pair (@pairs){
> > > ($name, $value) = split(/&/, $pair);
> > > $hash{$name} = $value;
> > > }
> > >
> > > ...
> > > This got me a nice hash with values and everything.
> > >
> > > Okay, not too complicated or anything, but it wasn't using strict
> > > (and it had no reason for not doing so). So I changed it to use
> > > strict;
> > >
> > > #!/usr/bin/perl -w
> > >
> > > use strict;
> > >
> > > ...
> > > my %hash;
> > > foreach my $pair( @pairs ){
> > > my($name, $value) = split(/&/, $pair);
> > > $hash{$name} = $value;
> > > }
> > >
> > >
> > > The resulting hash gets only one key/value pair - $pairs[0] - and
> > > nothing else.
> > > Can someone please explain why this happens? Shouldn't it work
> > > just as well?
> > >
> > > I changed it to:
> > >
> > > #!/usr/bin/perl
> > >
> > > use strict;
> > > ...
> > > my(%hash, $pair);
> > > foreach $pair( @pairs ){
> > > etc. etc.
> > > }
> > >
> > > and it worked just fine.
> > >
> > > A.
> > > --
> > > Alyssa W. Harding
> > > Web Programmer
> > > U S WEST Creative Services
> > > phon: (206) 345-4073
> > > cell: (206) 769-8735
> > > -------------------------------------------------------------------
> > > People who didn't need people needed people around to know that they
> > > were
> > > the kind of people who didn't need people.
> > > -- (Terry Pratchett, Maskerade)
> > >
> > > - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> > > POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
> > > Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
> > > For Subscriptions, Email to majordomo at pm.org: ACTION spug-list EMAIL
> > > Replace ACTION by subscribe or unsubscribe, EMAIL by your Email address
> >
> > --
> > Alyssa W. Harding
> > Web Programmer
> > U S WEST Creative Services
> > phon: (206) 345-4073
> > cell: (206) 769-8735
> > -------------------------------------------------------------------
> > Shadwell hated all southerners and, by inference, was standing at
> > the
> > North Pole.
> > -- (Terry Pratchett & Neil Gaiman, Good Omens)
> >
> > - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> > POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
> > Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
> > For Subscriptions, Email to majordomo at pm.org: ACTION spug-list EMAIL
> > Replace ACTION by subscribe or unsubscribe, EMAIL by your Email address
--
Alyssa W. Harding
Web Programmer
U S WEST Creative Services
phon: (206) 345-4073
cell: (206) 769-8735
-------------------------------------------------------------------
It was the sort of thing you expected in the Street of Alchemists.
The
neighbours *preferred* explosions, which were at least identifiable
and
soon over. They were better than the smells, which crept up on you.
-- (Terry Pratchett, Moving Pictures)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
For Subscriptions, Email to majordomo at pm.org: ACTION spug-list EMAIL
Replace ACTION by subscribe or unsubscribe, EMAIL by your Email address
More information about the spug-list
mailing list