Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re(2): What Is 'my' Interested In?

by Ovid (Cardinal)
on Jan 30, 2004 at 15:56 UTC ( [id://325263]=note: print w/replies, xml ) Need Help??


in reply to Re: What Are Your Live Journal Friends Interested In?
in thread What Are Your Live Journal Friends Interested In?

Thanks for the kind words.

The my trick is an old one that relies on a feature of my that is not documented in perldoc -f my (though my is so ubiquitous that I suspect many people have not even read that perldoc). You see, my, like most Perl functions, has a return value. It returns variable it's declaring. That allows you to do stuff like this:

chomp(my $data = <FH>); while ((my $foo = some_func()) eq 'bar') { ... }

You can also use it to assign values to more than one variable:

perl -MData::Dumper -e 'my @a = my ($x,$y,$z) = localtime;print Dumper \@a'

In short, were it not for this feature, Perl would lose many nifty timesavers.

Cheers,
Ovid

New address of my CGI Course.

Replies are listed 'Best First'.
Re: Re(2): What Is 'my' Interested In?
by theorbtwo (Prior) on Jan 30, 2004 at 17:49 UTC

    Actualy, the last trick has nothing to do with the return of the my function -- an empty () would work just as well, and not effect what goes in @a at all.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

      Either you are mistaken or I misunderstand you.
      $perl -MData::Dumper -we 'my @a = my ($x,$y,$z) = localtime;print Dump +er \@a' $VAR1 = [ 38, 27, 10 ]; $perl -MData::Dumper -we 'my @a = () = localtime;print Dumper \@a' $VAR1 = [];
      In both cases @a is assigned the result of the ...=localtime list assignment. And as perlop says, "a list assignment in list context produces the list of lvalues assigned to". In the my() case, the lvalues are the ones returned by my(). Though there seems to be a bug here:
      $perl -MData::Dumper -we 'my @a = my (undef,$y,$z) = localtime ;print Dumper \@a' $VAR1 = [ 49, 32, 10 ];
      I would have expected [undef,32,10] there.
Re: Re(2): What Is 'my' Interested In?
by benizi (Hermit) on Feb 02, 2004 at 20:24 UTC

    For Getopt::Long, I like to go one step further and have the defaults 'inlined'. Where Ovid wrote:

    GetOptions( ... 'verbose=i' => \my $VERBOSE, ... 'minimum=i' => \my $minimum, ); + $minimum ||= 1; $VERBOSE ||= 0;

    I like to write:

    GetOptions( ... 'verbose=i' => \(my $VERBOSE = 0), ... 'minimum=i' => \(my $minimum = 1), );

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 22:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found