Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: Way to do a "scalar ref"?

by AnomalousMonk (Monsignor)
on Sep 26, 2011 at 18:45 UTC ( #927926=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Way to do a "scalar ref"?
in thread Way to do a "scalar ref"?

You can even save one more large, expensive copy operation by returning a reference to the scalar 'content' in the first place rather than the actual content (if you have editorial control over these functions) and just pass around the scalar reference thereafter. In the example below, the only copy performed is the absolutely necessary one (if you do not want to change the original content) associated with the first processing operation. (Update: Of course, you still have to come to terms with all the copying that goes on in all those  s/// operations, which may make the higher-level question moot.)

>perl -wMstrict -le "my $article = { content => 'FEE FIE FOE FUM' }; ;; print qq{'$article->{content}'}; my $sr = process_content($article); print qq{'$$sr'}; process_content_some_more($sr); print qq{'$$sr'}; ;; sub process_content { my ($hashref) = @_; (my $processed = $hashref->{content}) =~ s{(\w+)}{\L$1}xmsg; return \$processed; } ;; sub process_content_some_more { my ($scalarref) = @_; $$scalarref =~ s{(\w+)}{\u$1}xmsg; } " 'FEE FIE FOE FUM' 'fee fie foe fum' 'Fee Fie Foe Fum'


Comment on Re^3: Way to do a "scalar ref"?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-08-28 14:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (263 votes), past polls