|
Posted by Uri Guttman on July 18, 2008, 12:11 pm
Please log in for more thread options
BM> guaranteed: since explicitly passing a scalar ref to a function is
BM> rather unusual (though not unknown), I would wager that is the case.
passing scalar refs is very useful and not just to modify things. if you
are passing around large strings/buffers, passing by ref is much more
efficient (assuming you copy args out of @_). you may not use scalar
refs as often a hash/array refs but they should be part of your
toolkit. you just need to be creative to find times where they help out.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
|
|
Posted by Uri Guttman on July 17, 2008, 11:30 pm
Please log in for more thread options
>> Huh? The sub defaults to operating on $_ if no argument is supplied.
>> That's hardly an 'edge case'.
JD> What sub? Here's the original code:
JD> my($text) = @_;
JD> if ($text)
JD> {
JD> $$text =~ s/^\s+//;
JD> $$text =~ s/\s+$//;
JD> }
JD> else
JD> {
JD> s/^\s+//;
JD> s/\s+$//;
JD> }
JD> This means that if the first argument evaluates to "false" (which is
JD> not at all the same as "not supplying an argument"), the else clause is
JD> executed and $_ is modified. IMHO that seems like very sloppy and
JD> unusual coding.
but notice $text is supposed to be a scalar ref. so if no arg is passed
in, the sub works on $_. if $text is set, it works on the scalar
value. if $text isn't a scalar ref it dies.
this is the guts from some kiddie's trim sub which take a ref to a
scalar to trim or defaults to working on $_. not something i would ever
write like that. well, in general i don't use $_ unless i have to. i
like named variables.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
|
| Similar Threads | Posted | | Identify if a scalar is int, double or text | May 11, 2007, 5:03 am |
| Can not add a scalar variable of path into @INC? | May 8, 2007, 10:52 pm |
| how to "spell out" the elements in a scalar variable? | May 23, 2005, 2:10 pm |
| assigning array to a scalar variable | March 14, 2008, 4:39 am |
| Hash keys to scalar variable conversion. | May 18, 2006, 4:41 pm |
| Scalar variable in void context before a loop | October 20, 2008, 1:49 am |
| How to extract all link from web page (loaded in variable $content) to scalar @links? | September 11, 2006, 4:47 am |
| Replace scalar in another scalar | January 27, 2005, 3:21 pm |
| hex to double | December 15, 2005, 9:07 am |
| double eval? | July 18, 2004, 4:28 pm |
|