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. 1 1955
Earlier today, in comp.lang.perl, I <no****@mail.com> 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. ji*******@myrealbox.com (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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Fred Ma |
last post by:
Hello,
This is not a troll posting, and I've refrained from
asking because I've seen similar threads get all
nitter-nattery. But I really want to make a decision
on how best to invest my time....
|
by: Xah Lee |
last post by:
here's a interesting real-world algoritm to have fun with.
attached below is the Perl documentation that i wrote for a function
called "reduce", which is really the heart of a larger software.
...
|
by: Terry |
last post by:
Hi all,
I am helping someone to investigate a problem with a perl tool that we
are using. The problem is completely random and it does not produce
any errors.
The tool is supposed to encrypt...
|
by: Dieter Vanderelst |
last post by:
Dear all,
I'm currently comparing Python versus Perl to use in a project that
involved a lot of text processing. I'm trying to determine what the most
efficient language would be for our...
|
by: Hendry Taylor |
last post by:
I wrote a perl script and tested it on windows and it works fine. I then
moved it onto a solaris machine and now it says that there is a syntax
error with the following line of code:
$user1 =...
|
by: Kirt Loki Dankmyer |
last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile
it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of
different switches, but I can't get it to compile....
|
by: bugbear |
last post by:
I need to do some fairly simple processing
of XML files; I would like to be able to do this
in perl.
However, the XML I'm handling uses namespaces.
In practice, the tags do not overlap,
so I...
|
by: gedsta |
last post by:
Hi all
I am a total noob and its my 2nd post, first of all thank you for letting me use these forums as a source of information, and hopefully once i get my head round perl i maybe able to help...
|
by: Otto J. Makela |
last post by:
I'm trying to install to php the Perl-1.0.0.tgz package (from
http://pecl.php.net/package/perl, enabling one to call perl libraries)
to a pre-existing Solaris system.
Unfortunately, the attempt...
|
by: KevinADC |
last post by:
Note: You may skip to the end of the article if all you want is the perl code.
Introduction
Many websites have a form or a link you can use to download a file. You click a form button or click...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| | |