substr() as subroutine argument -> weird behaviour

Discussion in 'Perl Misc' started by nobull@mail.com, Aug 14, 2003.

  1. Guest

    Earlier today, in comp.lang.perl, I <> 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.

    > (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.
     
    , Aug 14, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Collins

    Unexpected subroutine behaviour without &

    Kevin Collins, Jun 2, 2004, in forum: Perl Misc
    Replies:
    16
    Views:
    173
    Gunnar Hjalmarsson
    Jun 2, 2004
  2. Andrew
    Replies:
    15
    Views:
    189
    John W. Krahn
    Dec 1, 2005
  3. king
    Replies:
    5
    Views:
    189
  4. pacmac

    scope subroutine argument with "our"

    pacmac, Oct 1, 2009, in forum: Perl Misc
    Replies:
    3
    Views:
    102
    Uri Guttman
    Oct 2, 2009
  5. Patrick Hartman

    weird behavior when passing hashes to subroutine

    Patrick Hartman, Feb 19, 2010, in forum: Perl Misc
    Replies:
    9
    Views:
    118
    Jens Thoms Toerring
    Feb 19, 2010
Loading...

Share This Page