Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: simply appending to a scalar...

by davido (Cardinal)
on Jun 25, 2006 at 16:36 UTC ( #557443=note: print w/replies, xml ) Need Help??

in reply to simply appending to a scalar...

There are seven ways I can immediately think of, and you've picked one of the clearest and easiest. Here is the list I can come up with:

  • The . (dot) operator: concatenation.
  • The .= operator: append.
  • substr: Substring manipulation.
  • join: Joining two or more strings.
  • s/(...)/$1$string/: Substitution.
  • The qq/...../ or "...." operator: interpolation.
  • open: open my $fh, '>+', \$variable or die $!;: Print to an in-memory filehandle.

I'm sure I've missed a few, but these ways jump to mind immediately. The last method I listed; open, is pretty obfuscatory in nature. There aren't many situations I can think of where it would be a favorable approach, especially if simple concatenation is your goal. But it's there, so I mentioned it.

dot (.) and dot-equals (.=) are definitely the simplest and clearest approaches.


Replies are listed 'Best First'.
Re^2: simply appending to a scalar...
by ikegami (Patriarch) on Jun 28, 2006 at 05:10 UTC

    Examples of mentioned solutions:

    • $data = $data . $more;
    • $data .= $more;
    • substr($data, length($data), 0, $more);
    • substr($data, length($data)) = $more;
    • $data = join('', $data, $more);
    • $data =~ s/\z/$more/;
    • $data =~ "$data$more";
    • { open(my $fh, '>+', \$data); print $fh $more; } (Requires 5.8)

    Other useful solutions:

    • $data = sprintf('%s%s', $data, $more);
    • $data = pack('a*a*', $data, $more);
    • $data = pack('(a*)*', $data, $more); (Requires 5.8)
    • $data = do { local $"; my @array = ($data, $more); "@array" };

    Other text formatting functions:

    • format formats text sent to a file handle.
    • formline is the guts of format.

    And that's only using tools meant for text manipulation and/or formatting. You could do weird stuff like:

    • $data = reverse scalar reverse $data, $more;
    • $data = do { local $;; my %hash; $hash{$data, $more}++; (keys(%hash))[0] };
Re^2: simply appending to a scalar...
by abachus (Monk) on Jun 27, 2006 at 23:53 UTC
    The example of the open function you've mentioned has become of some interest also.

    I am able to open and print to the in-memory filehandle, though unable to syswrite to it and i don't know why :(

    thank you for your patience,


      Because syswrite operates at the system level, bypassing Perl's higher-level file IO.

      Let me just say this however: Of the seven ways I listed for appending information to a string, in-memory filehandles are the most difficult, least Perlish one; the one that I strongly urge you to consider not using. There are three ways down from the top floor of the Sears Tower building: You can take the elevator, you can take the staircase, or you can jump off the roof. I would advise against jumping off the roof. But doing so will get you to the bottom, pronto.

      There is a very limited set of problems for which using in-memory filehandles will be the optimal choice. ...very limited, and you rarely see them in run-of-the-mill everyday code. It's a little like symbolic references (though even less useful); they exist, they have uses, but you will rarely see them in code, and will probably never actually need to implement them yourself.

      You may have one of those fairly uncommon situations where the in-memory filehandle leads to better code. If you do, I'd love to see what that application is. Then the next time someone asks where it's a good idea to use them, I'll have at least one good example. lol


        Ah, is it an absolute rule not to use open with sysread/syswrite ?
        The reason I ask is that i've had no obvious problems before using something like :

        open(HANDLE,"+>$name") or die $!;

        Then say a four way select() & sysread() until sysread returns 0. Same can be said for syswrite() but without the select() and zero byte check.

        I'm aware that print and read are buffered, but i'm aware of little more than that ;-)

        To conclude, i reckon i will implement what was first decided on Sunday, and that is using sysread() to fill up the target scalar, it has been interesting however to have seen the other options, TMTOWTDI indeed.



Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://557443]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2022-10-01 18:10 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (3 votes). Check out past polls.