Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl problem, possible bug?

Reply
Thread Tools

Perl problem, possible bug?

 
 
Jim Dawson
Guest
Posts: n/a
 
      08-14-2003
I was writing a subroutine to extract fields from lines of text when I
ran into an issue. I have reproduced this error on Perl 5.8 on AIX,
5.8 on Linux and 5.6 on Windows.

############### CUT HERE ###############
#!/usr/bin/perl -w

my @list = ("field1 field2 field3");

sub stripws($)
{
$_[0] =~ s/\s//g;
return $_[0];
}

foreach (@list)
{
my $x = stripws(substr($_,10,10));
print "$x\n";
}
############### CUT HERE ###############

Here 'field2' represents a variable-length field. I want to strip out
that column, remove whitespace from it, and assign it to $x. You would
expect $x to be equal to 'field2', but instead $x is 'field2fiel', as
if it is stripping the whitespace before calling the stripws()
function.

Is there something I am missing here or is this a bug?

Thanks in advance.
 
Reply With Quote
 
 
 
 
nobull@mail.com
Guest
Posts: n/a
 
      08-14-2003
Earlier today, in comp.lang.perl, I <(E-Mail Removed)> wrote:

> This newsgroup does not exist (see FAQ). Please do not start threads
> here. Particuarly, not ones asking really interesting questions since
> it means most people don't get a chance to see them.


On refection, since the question was really interesting I've decided
to follow-up myself and cross-post to comp.lang.perl.misc where people
using correctly configured newsspools will be able to see it.

> http://www.velocityreviews.com/forums/(E-Mail Removed) (Jim Dawson) wrote:
>
> > my @list = ("field1 field2 field3");
> >
> > sub stripws($)
> > {
> > $_[0] =~ s/\s//g;
> > return $_[0];
> > }
> >
> > foreach (@list)
> > {
> > my $x = stripws(substr($_,10,10));
> > print "$x\n";
> > }

>
> > You would expect $x to be equal to 'field2',

>
> No I wouldn't.
>
> > but instead $x is 'field2fiel'

>
> Yep, that is correct.
>
> > Is there something I am missing here or is this a bug?

>
> Excellent question!
>
> You are missing two totally separate things.
>
> The first is pretty basic. The elements of @_ are *aliases* not
> *copies* of the arguments passed to a subroutine.
>
> sub foo { $_[0] = 'Cooked' };
> my $q='Raw';
> foo($q);
> print "$q\n"; # Prints 'Cooked'
>
> The second is much more subtle. The substr() function in Perl does
> not, in fact, return a string. It returns a special thing - an SV
> with substr magic. Usually if you use substr() in a rvalue context
> you can ignore this subtlty.
>
> But if you make a reference or an alais to the value returned by
> substr() you cannot ignore it or, as you have found, strange things
> happen.
>
> my $s='xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
> my $x = \substr($s,10,10); # Ref to SV with substr magic
> $s = '0123456789Wierd, eh??';
> print "$$x\n"; # Prints 'Wierd, eh?';
> $$x= 'Just totally crazy';
> print "$s\n"; # Prints '0123456789Just totally crazy?'
>
> $s = "field1 field2 field3";
> $$x =~ s/\s//g;
> print "$$x\n"; # Prints 'field2fiel'
>
> Weird, but not a bug.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 02-27-2011 11:00 PM
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
Is it possible to run a perl script in a machine where Active perl is not installed. hara Perl Misc 6 05-27-2006 12:07 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments