473,564 Members | 2,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Perl problem, possible bug?

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.
Jul 19 '05 #1
1 1965
Earlier today, in comp.lang.perl, I <no****@mail.co m> 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*******@myrea lbox.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='xxxxxxxxxxx xxxxxxxxxxxxxxx xxx';
my $x = \substr($s,10,1 0); # Ref to SV with substr magic
$s = '0123456789Wier d, 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.

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
4044
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. I'm not interested on which language is better in *general*, just for my purpose. My area of research is in CAD algorithms, and I'm sensing the...
9
1917
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. The implementation is really simple, but the key is to understand what the function should be. I'll post Perl and Python codes tomorrow for those...
0
2171
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 some data and transfer them to an ftp server (at the moment this is a local directory). However, not all the files are encrypted and copied. As a...
9
4506
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 purposes. I have to admit that, although I'm very familiar with Python, I'm complete Perl noob (and I hope to stay one) which is reflected in my questions....
10
6505
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 = "User name: ".$rec1; the line of code just before that is: @rec1 = split(/@/, $autotrack);
0
9736
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. I need it to be compiled with threads. Anyone have any wisdom on how best to do this? Here's a transcript of my latest attempt. It's long; you...
5
1481
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 could achieve the functionality I need by simply stripping namespace information.
0
5232
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 other noobs as well. Ok, my problem is: Windows Server 2003 Plesk for Windows 7.5.6 X-Cart Software (ecommerce solution)
13
3238
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 fails in a rather dramatic way, spewing out thousands of "relocation remains"... I'm somewhat lost on what to do next, the documentation that came...
1
47385
KevinADC
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 on a link and after a moment or two a file download dialog box pops-up in your web browser and prompts you for some instructions, such as “open” or...
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.