my( $var ) vs my $var 
Author Message
 my( $var ) vs my $var

I posted some code on the perl-xml list, and got a coding tip about my
that says:

<quote>
When working with scalar values you will find that it is actually
quicker to write my $variable instead of my( $variable ).  The reason

my() instead of just my can break conditional testing ways  . . .
</quote>

The second part about "conditional testing ways" I dont even
understand, Ive used my in conditionals like:

if ( my($fname, $lname) = $sth->fetchrow_array() ) {
  # record exsists
  ...

Quote:
}

In that instance, I _have_ to parenthesize the vars I pass to my(),
correct?

On the issue of speed, what type of expense are we talking about here?
The environment is mod_perl.

Im a parenthesize everything guy, for the same reason Im a perl guy. I
dont like thinking about memory management, and I dont like thinking
about precedence.

opinions?

Todd W.



Tue, 15 Mar 2005 12:55:33 GMT  
 my( $var ) vs my $var

Quote:

>When working with scalar values you will find that it is actually
>quicker to write my $variable instead of my( $variable ).  The reason
>for this is that when you do my() = something ... Perl does internal

></quote>

>The second part about "conditional testing ways" I dont even
>understand, Ive used my in conditionals like:

>if ( my($fname, $lname) = $sth->fetchrow_array() ) {
>  # record exsists
>  ...
>}

>In that instance, I _have_ to parenthesize the vars I pass to my(),
>correct?

Right.  If you write it without the parentheses:

        if ( my $fname, $lname = $sth->fetchrow_array() ) {
          # record exsists
          ...
        }

Then it means something very different (and peculiar), as if you had
written this:

        if ( (my $fname), ($lname = $sth->fetchrow_array()) ) {
          # record exsists
          ...
        }

This declares $fname as a new 'my' variable, but without initializing
it, and assigns the first field of the database record into the
*global* variable $lname.  ',' has lower precedence than '='.

Quote:
>On the issue of speed, what type of expense are we talking about here?

It's completely trivial.  Forget about it.  

Quote:
>Im a parenthesize everything guy, for the same reason Im a perl guy. I
>dont like thinking about memory management, and I dont like thinking
>about precedence.

>opinions?

If you 'parenthesize everything', you're going to get shafted one day.
(So is the author of the paragraph you quoted.)  Perl uses parentheses
for more than just precedence; it also uses them for context, and you
must think about context whether you like it or not.  Try this:




        print "$s1 $s2\n";

        my $t1   = ('Pie', 'is', 'good');
        my ($t2) = ('Pie', 'is', 'good');
        print "$t1 $t2\n";

The paragraph you quoted also says:

        Also the use of my() instead of just my can break conditional
        testing ways . . .

That is bullshit; it's also FUD.  Notice he doesn't say *how* it can
'break conditional testing ways'; those are the words of someone who
doesn't understand what is going on, got an unpleasant surprise in the
past, and has resolved to avoid what he perceives as the cause of the
surprise, but *still* doesn't understand what is going on.  These
people are sure to get more unpleasant surprises in the future,
because true understanding is the only effective method for avoiding
unpleasant surprises.

The simple truth is that "my($x)" and "my $x" simply mean different
things, and they only way things can 'break' is if you use the wrong
one when you wanted the other one.  The quoted statement above sounds
as peculiar to me as if it had said:

        Also the use of "..." instead of just '...' can break
        conditional testing ways . . .

For some more information, you should see the section 'context' in the
'perldata' manual, or do web search for 'perl context'.

Hope this helps.



Tue, 15 Mar 2005 13:23:34 GMT  
 my( $var ) vs my $var

Quote:

>    if ( (my $fname), ($lname = $sth->fetchrow_array()) ) {
>      ...

> This declares $fname as a new 'my' variable, but without initializing
> it, and assigns the first field of the database record into the
> *global* variable $lname.  ',' has lower precedence than '='.

The first field? The last one, I'd say. (From a Perl Contexts view.) Or
does fetchrow_array() "wantarray" checking internally?

lg,
daniel
--
Black holes are where God divided by zero.



Tue, 15 Mar 2005 13:40:10 GMT  
 my( $var ) vs my $var


Quote:
> Im a parenthesize everything guy, for the same reason Im a perl guy. I
> dont like thinking about memory management, and I dont like thinking
> about precedence.

In addition to the excellent advice previous posters gave on this, you
should also probably read:

 perldoc perlfaq7
   'Why doesn't "my($foo) = ;" work right?'

cp



Tue, 15 Mar 2005 15:26:16 GMT  
 my( $var ) vs my $var

Quote:

> Subject: my( $var ) vs my $var

Your question isn't really about my() at all, it is about context.

   ( $var ) vs $var

is the Important Part.

Adding "my" does not affect context.

Quote:
> if ( my($fname, $lname) = $sth->fetchrow_array() ) {
>   # record exsists
>   ...
> }

> In that instance, I _have_ to parenthesize the vars I pass to my(),
> correct?

Yes. Factor out the "my" since it does not affect context.

   if ( ($fname, $lname) = $sth->fetchrow_array() ) {

fetchrow_array() called in list context.

   if ( $fname, $lname = $sth->fetchrow_array() ) {

fetchrow_array() called in scalar context.

Since you _want_ the list context behavior, you must use parenthesis
in order to get it (here).

The "here" caveat is because we are speaking of literal lists,
you can get list context without parenthesis otherwise, eg:





_All_ of those put list context on the RHS.

Quote:
> On the issue of speed, what type of expense are we talking about here?

Speed is not a consideration if the alternatives (scalar vs list context)
do not do the same thing.  :-)

In that case you _have to_ use the one that gets you the "right thing".

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Tue, 15 Mar 2005 16:10:53 GMT  
 my( $var ) vs my $var


Quote:

>>    if ( (my $fname), ($lname = $sth->fetchrow_array()) ) {
>>      ...

>> This declares $fname as a new 'my' variable, but without initializing
>> it, and assigns the first field of the database record into the
>> *global* variable $lname.  ',' has lower precedence than '='.

>The first field? The last one, I'd say.

The manual says it returns the first field.  Have you tried it and
found it to return the last field instead, or do you have some other
factual basis for suggesting that it returns the last field?


Tue, 15 Mar 2005 22:42:20 GMT  
 my( $var ) vs my $var

Quote:

> In article


The MID is allready in your Message Header, please omit it in the body,
thanx.

Quote:

>>The first field? The last one, I'd say.

> The manual says it returns the first field.  Have you tried it and
> found it to return the last field instead, or do you have some other
> factual basis for suggesting that it returns the last field?

Well, as i said, as far as Perl context is concerned, a List returns the

cause it "knows" it's in scalar context and therefor returns the number
of elements.)
That's why i asked whether fetchrow_array checks for the context, e.g.
by calling "wantarray". By what you said now, I guess it does.

lg,
daniel
--
Top 10 Things to say, when you run out of good arguments:
No 2)  You used to program in Pascal, didn't you?



Tue, 15 Mar 2005 23:41:49 GMT  
 my( $var ) vs my $var

Quote:


> > In article

> The MID is allready in your Message Header, please omit it in the body,
> thanx.


> >>The first field? The last one, I'd say.

> > The manual says it returns the first field.  Have you tried it and
> > found it to return the last field instead, or do you have some other
> > factual basis for suggesting that it returns the last field?

> Well, as i said, as far as Perl context is concerned, a List returns the

> cause it "knows" it's in scalar context and therefor returns the number
> of elements.)

  % perl -MDBI -le 'print $DBI::VERSION'
  1.30
  % perldoc DBI

  [...]

        ""fetchrow_array""

  [...]

           If called in a scalar context for a statement handle
           that has more than one column, it is undefined whether
           the driver will return the value of the first column
           or the last. So don't do that.  Also, in a scalar con-
           text, an "undef" is returned if there are no more rows
           or if an error occurred.

--
Joe Schaefer       "There are two times in a man's life when he should not
                    speculate: when he can't afford it, and when he can."
                                               --Mark Twain



Wed, 16 Mar 2005 00:29:53 GMT  
 my( $var ) vs my $var


Quote:

>> In article

>The MID is allready in your Message Header, please omit it in the body,
>thanx.

Since I'm not planning to omit it, I've no need to be thanked.

Quote:


>>>The first field? The last one, I'd say.

>> The manual says it returns the first field.  Have you tried it and
>> found it to return the last field instead, or do you have some other
>> factual basis for suggesting that it returns the last field?

>Well, as i said, as far as Perl context is concerned, a List returns the
>_last_ entry in scalar context.

You are mistaken.  There is no such thing as a list in scalar context.

Quote:

>cause it "knows" it's in scalar context and therefor returns the number
>of elements.)

An array is a special case, but in the same sense, *every* other Perl
expression is a special case.  For example, 'split' in scalar context
does not return the last field; it returns the number of fields, and

does not return the last field; it returns the third field.  'reverse'
in scalar context does not return the last field; it returns the
reversed concatenation of the fields.

Quote:
>That's why i asked whether fetchrow_array checks for the context, e.g.
>by calling "wantarray". By what you said now, I guess it does.

You need to read the section of the 'perlfunc' manual that begins:

       Remember the following important rule: There is no rule
       that relates the behavior of an expression in list context
       to its behavior in scalar context, or vice versa.  



Wed, 16 Mar 2005 03:24:17 GMT  
 my( $var ) vs my $var


Quote:
>  % perl -MDBI -le 'print $DBI::VERSION'
>  1.30
>  % perldoc DBI

>  [...]

>        ""fetchrow_array""

>  [...]

>           If called in a scalar context for a statement handle
>           that has more than one column, it is undefined whether
>           the driver will return the value of the first column
>           or the last. So don't do that.  

Oho.  I only have version 1.14 installed, which says:

        In a scalar context, `fetchrow_array' returns the value of the
        first field.

In my opinion, it is a violation of the contract with the user to
renege on a promise like the one that was made by 1.14, but Tim didn't
ask me.

Thanks for the correction.



Wed, 16 Mar 2005 03:27:15 GMT  
 my( $var ) vs my $var

Quote:

>   % perl -MDBI -le 'print $DBI::VERSION'
>   1.30
>   % perldoc DBI

>   [...]

>         ""fetchrow_array""

>   [...]

>            If called in a scalar context for a statement handle
>            that has more than one column, it is undefined whether
>            the driver will return the value of the first column
>            or the last. So don't do that.  Also, in a scalar con-
>            text, an "undef" is returned if there are no more rows
>            or if an error occurred.

Well damn, that's gonna suck.  Version 1.20 promises to return the first
field.  Guess I'll be in no hurry to upgrade.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
                    Usenet Newsgroup Service



Wed, 16 Mar 2005 04:24:45 GMT  
 my( $var ) vs my $var

Quote:
> >> The manual says it returns the first field.  Have you tried it and
> >> found it to return the last field instead, or do you have some other
> >> factual basis for suggesting that it returns the last field?

> >Well, as i said, as far as Perl context is concerned, a List returns the
> >_last_ entry in scalar context.

> You are mistaken.  There is no such thing as a list in scalar context.

Of course there is such a thing as a list in scalar context.



the latter will return the last element in the list only.



Wed, 16 Mar 2005 14:41:22 GMT  
 my( $var ) vs my $var
Also sprach Jeff Mott:

Quote:
>> >> The manual says it returns the first field.  Have you tried it and
>> >> found it to return the last field instead, or do you have some other
>> >> factual basis for suggesting that it returns the last field?

>> >Well, as i said, as far as Perl context is concerned, a List returns the
>> >_last_ entry in scalar context.

>> You are mistaken.  There is no such thing as a list in scalar context.

> Of course there is such a thing as a list in scalar context.



> the latter will return the last element in the list only.

Nonethelesse this is no list. So let us ask perl:


Useless use of a constant in void context at -e line 1.
Useless use of a constant in void context at -e line 1.

-e syntax OK

So, all but the last element of this 'list' are actually just constants
in void context: They are evaluated and simply thrown away.

Also, read this:

    Found in /usr/share/perl/5.6.1/pod/perlfaq4.pod
        What is the difference between a list and an array?

This should show how was right (MJD) and who not (you). :-)

Tassilo
--

pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;



Wed, 16 Mar 2005 14:59:04 GMT  
 my( $var ) vs my $var

Quote:

> Nonethelesse this is no list.

Let me say it this way: If you put a List into scalar konetxt, it
evaluates as a scalar with the value of the last element oft the list.

Quote:
> So, all but the last element of this 'list' are actually just
> constants in void context: They are evaluated and simply thrown away.

IMHO this doesn't contradict with what i said originally, but it seems
I'm wrong with that. (See below)

Quote:
>     Found in /usr/share/perl/5.6.1/pod/perlfaq4.pod
>         What is the difference between a list and an array?

Ah.

| There never was a list there at all!

Ok. And now i see, that the camel (3rd edition, german) actually
explains that too, I thought it doesn't. I'll have to check with the
english version too. It think this section was a little bit confusing
me. Hmm.

Quote:
> This should show how was right (MJD) and who not (you). :-)

Well, my mistake was to adress the list instead of the operator. (",")

lg,
daniel
--
Only wimps use tape backup, _real_ men just upload their important stuff
on ftp, and let the rest of the world mirror it.
                -- Linus Torvalds



Wed, 16 Mar 2005 19:46:18 GMT  
 my( $var ) vs my $var


Quote:
>> Nonethelesse this is no list.

Daniel> Let me say it this way: If you put a List into scalar konetxt, it
Daniel> evaluates as a scalar with the value of the last element oft the list.

And it's still wrong.  You cannot put a list into a scalar context.
The construct

        $x = (7, 8, 9);

is not a list.  It's a comma-operator (used twice).  The role of the
comma operator is to evaluate the left operand, throw it away, then
evaluate the right operand, returning it.  So 7 gets evaluated, and
discarded, then 8 gets evaluated, then discarded, and finally 9 gets
evaluated, and then discarded.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Wed, 16 Mar 2005 21:52:36 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. $$var[xxx] vs ${var}[xxx]

2. $var->$method vs $var->$method()

3. Copying /var/spool/mail and /var/spool/news with perl

4. Expand ENV var within another ENV var

5. undef ($var) the same as $var = undef ?

6. Evaluating a var within a var from read file

7. Win32 perl how to do system(set var= $var)

8. *var{FILEHANDLE} and *var{DIRHANDLE}

9. "use vars qw(var)" -vs- "my $var"

10. Difference between "$var" and $var?

11. '$var =~ ///g' equals '$var =~ m// divided by g'

12. use lib $var?

 

 
Powered by phpBB® Forum Software