Click here to get back home

understading double scalar variable $$

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
understading double scalar variable $$ Slickuser 07-17-2008
Posted by Slickuser on July 17, 2008, 7:55 pm
Please log in for more thread options
I understand the code is removing spaces but I don't get with using
the double scalar.

$$text ??
And why we need an else?

my($text) = @_;
if ($text)
{
$$text =~ s/^\s+//;
$$text =~ s/\s+$//;
}
else
{
s/^\s+//;
s/\s+$//;
}


Posted by Joost Diepenmaat on July 17, 2008, 8:17 pm
Please log in for more thread options

> I understand the code is removing spaces but I don't get with using
> the double scalar.
>
> $$text ??

see perdoc perlref


> And why we need an else?

i seriously doubt that you do. Though there may be edge cases.

> my($text) = @_;
> if ($text)
> {
> $$text =~ s/^\s+//;
> $$text =~ s/\s+$//;
> }
> else
> {
> s/^\s+//;
> s/\s+$//;
> }
>

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

Posted by Ben Morrow on July 17, 2008, 9:26 pm
Please log in for more thread options

>
> > I understand the code is removing spaces but I don't get with using
> > the double scalar.
> >
> > $$text ??
>
> see perdoc perlref
>
>
> > And why we need an else?
>
> i seriously doubt that you do. Though there may be edge cases.

Huh? The sub defaults to operating on $_ if no argument is supplied.
That's hardly an 'edge case'.

> > else
> > {
> > s/^\s+//;
> > s/\s+$//;
> > }

Ben

--
"Faith has you at a disadvantage, Buffy."
"'Cause I'm not crazy, or 'cause I don't kill people?"
"Both, actually."
[ben@morrow.me.uk]

Posted by Joost Diepenmaat on July 17, 2008, 9:51 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'.

What sub? Here's the original code:

my($text) = @_;
if ($text)
{
$$text =~ s/^\s+//;
$$text =~ s/\s+$//;
}
else
{
s/^\s+//;
s/\s+$//;
}

This means that if the first argument evaluates to "false" (which is
not at all the same as "not supplying an argument"), the else clause is
executed and $_ is modified. IMHO that seems like very sloppy and
unusual coding.


--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

Posted by Ben Morrow on July 17, 2008, 10:46 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'.
>
> What sub? Here's the original code:
>
> my($text) = @_;
> if ($text)
> {
> $$text =~ s/^\s+//;
> $$text =~ s/\s+$//;
> }
> else
> {
> s/^\s+//;
> s/\s+$//;
> }
>
> This means that if the first argument evaluates to "false" (which is
> not at all the same as "not supplying an argument"), the else clause is
> executed and $_ is modified. IMHO that seems like very sloppy and
> unusual coding.

Well, yes, the code posted is obviously incomplete. Bad OP, no cookie.
However, it is obviously expected that the argument will be a ref, which
is always true, so there's no need to check for anything more
complicated. Indeed, if the sub were prototyped (;$) this would be
guaranteed: since explicitly passing a scalar ref to a function is
rather unusual (though not unknown), I would wager that is the case.

Ben

--
Musica Dei donum optimi, trahit homines, trahit deos. |
Musica truces mollit animos, tristesque mentes erigit. | ben@morrow.me.uk
Musica vel ipsas arbores et horridas movet feras. |

Similar ThreadsPosted
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
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
How to remove double quotes March 21, 2005, 8:10 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap