in reply to Re^2: Tell me how it works!
in thread Tell me how it works!
I looked at this inplace_uc code and I did add "$text =~" in front of the tr, which seemed appropriate.
Here, there is really no performance to be gained that I can see. The inplace_uc() call passes the whole string on the stack and there is some stuff to get the address of it. If we are doing an "inplace" replacement, why not just pass the addresss of the string instead of the whole string? My inplace_uc2() uses the address of the string in a very straightforward way.
As a perfence of style, I would always expect an inplace modification to to take place on an address or in Perl lingo a referece to something. Ok, mileage varies as they say.
#!/usr/bin/perl -w use strict; my $lc_msg = 'message'; inplace_uc ($lc_msg); print "$lc_msg\n"; sub inplace_uc { our $text; local *text = \$_[0]; # equivalent to: # local *text = \shift; $text =~ tr/a-z/A-Z/; # added $text =~ } my $lc_msg_y = 'another_message'; inplace_uc2 (\$lc_msg_y); print "$lc_msg_y\n"; sub inplace_uc2 { my $str_ref = shift; $$str_ref =~ tr/a-z/A-Z/; } # prints: #MESSAGE #ANOTHER_MESSAGE
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Tell me how it works!
by ikegami (Patriarch) on Jan 28, 2009 at 21:59 UTC |