Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Declaring my $var versus declaring my($var)

by moritz (Cardinal)
on Jun 10, 2011 at 14:08 UTC ( [id://909127]=note: print w/replies, xml ) Need Help??


in reply to Declaring my $var versus declaring my($var)

If you initialize your variables, the () makes a difference: context.
use strict; use warnings; sub print_context { say wantarray ? 'list' : 'scalar'; } my $x = print_context; my ($y) = print_context; __END__ scalar list

Examples where this makes a difference:

my $time1 = localtime; my ($time2) = localtime; my $rev1 = reverse $some_string; my ($rev2) = reverse $some_string;

Replies are listed 'Best First'.
Re^2: Declaring my $var versus declaring my($var)
by dh1760 (Acolyte) on Jun 10, 2011 at 14:25 UTC

    Now I understand why "in scalar context returns ..." has sometimes not returned what I expected. Amazing that I've probably coded tens of thousands of lines of perl over the years and have rarely been bitten by this difference. I suspect it's because any time it has caused an unexpected result, I've just figured out a different way to get what I needed.

    I guess we're never too old to learn ... thanks for the lesson!

    --
    DaveH (dh1760)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2026-04-21 13:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.