Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

odd =>behavior

by tcf03 (Deacon)
on Dec 04, 2007 at 17:15 UTC ( [id://654868]=perlquestion: print w/replies, xml ) Need Help??

tcf03 has asked for the wisdom of the Perl Monks concerning the following question:

Odd to me anyway...
perl -Mstrict -e'my $num = 1; print 1 if $num >= 1; print 2 if $num => + 1;'
Works as I'd expect. As does The following code does what Id expect.
#!/usr/bin/perl use strict; use warnings; use Carp; use DBI; use Data::Dumper; # Connection my $dsn = qq|dbi:Informix:sysmaster|; my $dbh = DBI->connect($dsn) or confess DBI->errstr(); $dbh->{'RaiseError'} = 1; $dbh->{'ChopBlanks'} = 1; # Hash to hold all info about tables... my %dbinfo; # Get info on extents... { my $query = qq|select count(*), tabname from sysextents group by tabname|; my $sth = $dbh->prepare($query) or confess DBI->errstr(); $sth->execute or confess DBI->errstr(); my ( $count, $table ); $sth->bind_col(1, \$count); $sth->bind_col(2, \$table); while ( $sth->fetchrow ) { next unless $count >= 8; $dbinfo{$table}{'extents'} = $count; } } print Dumper %dbinfo; END { $dbh->disconnect }
which outputs
__snip__
$VAR77 = 'abcde'; $VAR78 = { 'extents' => 14 }; $VAR79 = 'abcdef'; $VAR80 = { 'extents' => 33 }; $VAR81 = 'abcdefg'; $VAR82 = { 'extents' => 93 }; $VAR83 = 'abcdefgh'; $VAR84 = { 'extents' => 17 };
but when I change line 35 to
next unless $count => 8;
I get all table extents returned.
__snip__
$VAR2949 = 'abcdefgh'; $VAR2950 = { 'extents' => '4' }; $VAR2951 = 'abcdefghi'; $VAR2952 = { 'extents' => '1' }; $VAR2953 = 'abcdefghij'; $VAR2954 = { 'extents' => '1' }; $VAR2955 = 'abcdefghz'; $VAR2956 = { 'extents' => '1' }; $VAR2957 = 'abcdefghp'; $VAR2958 = { 'extents' => '1' }; $VAR2959 = 'abcde'; $VAR2960 = { 'extents' => '1' }; $VAR2961 = 'abcderr'; $VAR2962 = { 'extents' => '1' }; $VAR2963 = 'abcdewe'; $VAR2964 = { 'extents' => '1' };
Just curious as to the why of this.

perl is old ( nothing I can do about it either, upgrades are scheduled ), Output of perl -v is
This is perl, v5.8.3 built for x86_64-linux-thread-multi


updateupdated mis-spelled node title...

update2
The command line example is actually the odd behavior. So to restate my question, why does that output 12?

update3If I could only -- myself, I would, on this one. Whats the point of use warnings; if you don't read them?
Ted
--
"That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
  --Ralph Waldo Emerson

Replies are listed 'Best First'.
Re: odd =>behavior
by Fletch (Bishop) on Dec 04, 2007 at 17:23 UTC

    Erm, is it just me or do you seem to be confuzzling the fat comma => with the numeric lessgreater-than-or-equals operator >=? You're basically asking if a LIST in boolean context is true (which your list is, so you get all the elements).

    Update: Lesser, greater. Something about alligators. Meh. :)

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      I think I have in fact done that. In my original code, I had fat fingered => ( in lieu of >= ) I had a few moments and was playing with => vs >= on the command line. My first example, using => and >=, outputs 12. So based on that behavior, I would have expected the same to happen in my code.

      Ted
      --
      "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
        --Ralph Waldo Emerson

        In your first example you have $num which is greater-than-or-equal to 1, so that prints the 1; you also have (in effect) print 2 if 2 which again is always true so it's always going to print.

        When you change your conditional in the later code to print unless $count => 8 you effectively have print unless 8, which will always be true hence you get all elements.

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

Re: odd =>behavior
by toolic (Bishop) on Dec 04, 2007 at 17:32 UTC
    Do you not get the following kind of warning when you use the fat comma in your code?
    Useless use of private variable in void context at...
    I get the warning when I run this code (v5.8.0 built for x86_64-linux-thread-multi):
    #!/usr/bin/env perl use warnings; use strict; my $count = 9; my $i = 0; while ($i<5) { next unless $count => 8; $i++; } print "end\n";
    The "=>" operator is described in perlop as:
    The => operator is a synonym for the comma, but forces any word (consisting entirely of word characters) to its left to be interpreted as a string (as of 5.001). This includes words that might otherwise be considered a constant or function call.
      Yes, I am seeing the warning now. after adjusting the size of my scroll back buffer, I see it fine. I feel like a n00b
      Ted
      --
      "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
        --Ralph Waldo Emerson
Re: odd =>behavior
by grep (Monsignor) on Dec 04, 2007 at 17:24 UTC
     => is the Fat Comma - basically a comma that quotes the LHS.
     >= is Greater than or equal to

    Check out perlop

    UPDATE:
    Just remember it's consistent between > and <. The > or < always comes before the equals(=) sign.

    grep
    One dead unjugged rabbit fish later...
Re: odd =>behavior
by jrsimmon (Hermit) on Dec 04, 2007 at 17:26 UTC
    I don't quite understand your use of the => operator.
    if($var => digit) is always undefined does not evaluate $var in relation to digit. If you mean, "greater than or equal to", then your first example of >= is the correct syntax.

    Update: Corrected statement above, as pointed out by Fletch.

      No, if( $var => 2 ) { ... } is the same as if( $var, 2 ) { ... }, and a LIST in scalar context is the last element from the list. There's nothing undefined about it.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        a LIST in scalar context is the last element from the list.

        Not always:

        sub return_list { return 3, 2, 1 } my $result = return_list(); print "$result\n"; $result = 3, 2, 1; print "$result\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://654868]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2025-06-13 05:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.